IrfanAlam123

Forking twice avoids zombie

Mar 24th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.49 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4.  
  5. int main()
  6. {
  7.     int pid;
  8.     pid = fork();
  9.  
  10.     if (pid == 0) {
  11.         // First child
  12.         pid = fork();
  13.         if (pid == 0) {
  14.             // Second child
  15.             sleep(1);
  16.             printf("Second child: My parent PID is %d\n", getppid());
  17.         }
  18.     }
  19.     else {
  20.         // Parent process
  21.         wait(NULL);
  22.         sleep(2);
  23.         system("ps -o pid,ppid,state,tty,command");
  24.     }
  25.     return 0;
  26. }
Add Comment
Please, Sign In to add comment