Skip to content

Instantly share code, notes, and snippets.

@ltriant
Created March 7, 2019 03:34
Show Gist options
  • Save ltriant/22afe5d97c1fad98c5eb3d3f987001f5 to your computer and use it in GitHub Desktop.
Save ltriant/22afe5d97c1fad98c5eb3d3f987001f5 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
int main(void)
{
pid_t pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
}
if (pid == 0) {
// Child process, just do nothing
while (1) sleep(1);
exit(0);
}
// Parent process, wait on the child proc forever
printf("waiting on child pid: %d\n", pid);
int status;
waitpid(pid, &status, 0);
printf("status: %d\n", status);
if (WIFSIGNALED(status)) {
printf(" SIGCHLD\n");
if (WTERMSIG(status) == SIGTERM) {
printf(" child rx'd SIGTERM\n");
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment