Last active
September 19, 2020 21:35
-
-
Save leehambley/5589953 to your computer and use it in GitHub Desktop.
Handle SIGCHLD in C to be notified when a child process has exited. via http://www.linuxquestions.org/questions/programming-9/how-a-father-process-know-which-child-process-send-the-signal-sigchld-266290/
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
// Via http://www.linuxquestions.org/questions/programming-9/how-a-father-process-know-which-child-process-send-the-signal | |
#include <stdio.h> | |
#include <signal.h> | |
#include <unistd.h> | |
#include <sys/types.h> | |
#include <sys/wait.h> | |
void handler(int sig) | |
{ | |
pid_t pid; | |
pid = wait(NULL); | |
printf("Pid %d exited.\n", pid); | |
} | |
int main(void) | |
{ | |
signal(SIGCHLD, handler); | |
if(!fork()) | |
{ | |
printf("Child pid is %d\n", getpid()); | |
exit(0); | |
} | |
printf("Parent pid is %d\n", getpid()); | |
getchar(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
True, it also saves about
100b
and a compiler warning.