Skip to content

Instantly share code, notes, and snippets.

@Cyberax
Created November 17, 2019 07:41
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 Cyberax/9bd485bb07a90c66dbf9186034de0433 to your computer and use it in GitHub Desktop.
Save Cyberax/9bd485bb07a90c66dbf9186034de0433 to your computer and use it in GitHub Desktop.
Demo
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
int main() {
pid_t pid1;
pid_t pid2;
int status;
if (pid1 = fork()) {
/* parent process A */
waitpid(pid1, &status, 0);
} else if (!pid1) {
/* child process B */
if (pid2 = fork()) {
exit(0);
} else if (!pid2) {
/* child process C */
sleep(100);
} else {
/* error */
}
} else {
/* error */
abort();
}
return 0;
}
/*
cyberax@archive:/tmp$ ps -aef | grep a.out
cyberax 7952 1 0 23:40 pts/0 00:00:00 ./a.out
cyberax 7966 395 0 23:41 pts/0 00:00:00 grep --color=auto a.out
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment