Facebook
From WCY, 1 Month ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 144
  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. int main(void)
  5. {
  6.     if(fork()==0) // P2
  7.     {
  8.         printf("2 PID: %d, PPID: %d\n", getpid(), getppid());
  9.         if(fork()==0) // P3
  10.         {
  11.             printf("3 PID: %d, PPID: %d\n", getpid(), getppid());
  12.             if(fork()==0) // P4
  13.             {
  14.                 printf("4 PID: %d, PPID: %d\n", getpid(), getppid());
  15.                 sleep(120);
  16.                 return 0;
  17.             }
  18.             sleep(120);
  19.             return 0;
  20.         }
  21.     }
  22.     if(fork()==0) // P1
  23.     {
  24.         printf("2 PID: %d, PPID: %d\n", getpid(), getppid());
  25.         if(fork()==0)
  26.         {
  27.             printf("2 PID: %d, PPID: %d\n", getpid(), getppid());
  28.             if(fork()==0)
  29.             {
  30.                 printf("4 PID: %d, PPID: %d\n", getpid(), getppid());
  31.                 sleep(120);
  32.                 return 0;
  33.             }
  34.             sleep(120);
  35.             return 0;
  36.             printf("2 PID: %d, PPID: %d\n", getpid(), getppid());
  37.             if(fork()==0)
  38.             {
  39.                 printf("4 PID: %d, PPID: %d\n", getpid(), getppid());
  40.                 sleep(120);
  41.                 return 0;
  42.             }
  43.             sleep(120);
  44.             return 0;
  45.         }
  46.         sleep(120);
  47.         return 0;
  48.  
  49.     }
  50.     if(fork()==0) // P1
  51.     {
  52.         printf("3 PID: %d, PPID: %d\n", getpid(), getppid());
  53.         if(fork()==0) // P4
  54.         {
  55.             printf("4 PID: %d, PPID: %d\n", getpid(), getppid());
  56.             sleep(120);
  57.             return 0;
  58.         }
  59.         if(fork()==0) // P4
  60.         {
  61.             printf("4 PID: %d, PPID: %d\n", getpid(), getppid());
  62.             sleep(120);
  63.             return 0;
  64.         }
  65.         if(fork()==0) // P4
  66.         {
  67.             printf("4 PID: %d, PPID: %d\n", getpid(), getppid());
  68.             sleep(120);
  69.             return 0;
  70.         }
  71.         sleep(120);
  72.         return 0;
  73.  
  74.     }
  75.     sleep(120);
  76.     return 0;
  77. }
  78.