Last active
February 13, 2023 02:09
-
-
Save dulimarta/633f0fae30ae2eb8deb1a4434175d2cc to your computer and use it in GitHub Desktop.
CS452 Lab03 - fork() and signal()
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
#include <signal.h> | |
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdbool.h> | |
#include <sys/types.h> | |
#include <sys/wait.h> | |
bool running = true; | |
void sigHandler(int number) { | |
printf ("Signal handler of process %d\n", getpid()); | |
running = false; | |
} | |
int main() { | |
struct sigaction my_action; | |
my_action.sa_handler = sigHandler; | |
sigemptyset(&my_action.sa_mask); | |
sigaction(SIGUSR1, &my_action, NULL); | |
pid_t who = fork(); | |
if (who == 0) { | |
while (running) { | |
printf ("I'm the child PID:%d, my parent is %d\n", getpid(), getppid()); | |
sleep(2); | |
} | |
printf ("Out of the child loop\n"); | |
return 73; | |
} | |
else { | |
int code; | |
while (running) { | |
printf ("I'm the parent PID:%d\n", getpid()); | |
sleep(1); | |
} | |
printf ("out of the parent loop\n"); | |
wait(&code); | |
printf ("Child exit code is %d\n", WEXITSTATUS(code)); | |
} | |
printf ("Out of main()\n"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment