Facebook
From WCY, 1 Month ago, written in C.
Embed
Download Paste or View Raw
Hits: 131
  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) // P2
  23.     {
  24.         printf("2 PID: %d, PPID: %d\n", getpid(), getppid());
  25.         if(fork()==0) // P3
  26.         {
  27.             printf("3 PID: %d, PPID: %d\n", getpid(), getppid());
  28.  
  29.             sleep(120);
  30.             return 0;
  31.         }
  32.         if(fork()==0) // P3
  33.         {
  34.             printf("3 PID: %d, PPID: %d\n", getpid(), getppid());
  35.  
  36.             sleep(120);
  37.             return 0;
  38.         }
  39.         sleep(120);
  40.         return 0;
  41.     }
  42.     if(fork()==0) // P1
  43.     {
  44.         printf("3 PID: %d, PPID: %d\n", getpid(), getppid());
  45.         if(fork()==0) // P4
  46.         {
  47.             printf("4 PID: %d, PPID: %d\n", getpid(), getppid());
  48.             sleep(120);
  49.             return 0;
  50.         }
  51.         if(fork()==0) // P4
  52.         {
  53.             printf("4 PID: %d, PPID: %d\n", getpid(), getppid());
  54.             if(fork()==0)
  55.             {
  56.                 printf("4 PID: %d, PPID: %d\n", getpid(), getppid());
  57.                 execlp("pstree", "pstree","-c",(char *)NULL);
  58.  
  59.                 sleep(120);
  60.                 return 0;
  61.             }
  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.