Skip to content

Instantly share code, notes, and snippets.

@dulimarta
Last active February 13, 2023 02:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dulimarta/633f0fae30ae2eb8deb1a4434175d2cc to your computer and use it in GitHub Desktop.
Save dulimarta/633f0fae30ae2eb8deb1a4434175d2cc to your computer and use it in GitHub Desktop.
CS452 Lab03 - fork() and signal()
#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