Created
November 17, 2019 07:41
-
-
Save Cyberax/9bd485bb07a90c66dbf9186034de0433 to your computer and use it in GitHub Desktop.
Demo
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 <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