-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72b16c7
commit ff8424f
Showing
2 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |