Skip to content

Commit

Permalink
Source Code
Browse files Browse the repository at this point in the history
  • Loading branch information
HasanYahya101 authored Nov 29, 2024
1 parent 72b16c7 commit ff8424f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Lab_13/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
all: main

main: main.c
gcc -Wall -g -o main main.c

clean:
rm -f main
62 changes: 62 additions & 0 deletions Lab_13/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/wait.h>

void handle_sigchld(int sig)
{
int status;
wait(&status);
printf("Received SIGCHLD\n");
printf("Total Time in Seconds: %d\n", WEXITSTATUS(status));
}

int main()
{
int laps, lap_time;

printf("Enter Number of Laps: ");
scanf("%d", &laps);
while (laps < 1)
{
printf("Number of Laps must be greater than 0\n");
printf("Enter Number of Laps: ");
scanf("%d", &laps);
}
printf("Enter Lap Time: ");
scanf("%d", &lap_time);

while (lap_time < 1)
{
printf("Lap Time must be greater than 0\n");
printf("Enter Lap Time: ");
scanf("%d", &lap_time);
}

signal(SIGCHLD, handle_sigchld);

pid_t pid = fork();
if (pid == -1)
{
printf("Failed to fork\n");
return 1;
}
else if (pid == 0)
{
for (int i = 1; i <= laps; i++)
{
sleep(lap_time);
printf("Lap: %d Completed\n", i);
}
exit(laps * lap_time);
}
else
{ // Parent process
// Parent waits for the signal (handled by the signal handler)
pause();
}

return 0;
}

0 comments on commit ff8424f

Please sign in to comment.