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