Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.27 KB | None | 0 0
  1. #include <sys/types.h>
  2. #include <unistd.h>
  3. #include <stdio.h>
  4. int main(void)
  5. {
  6.     char pid[10];
  7.     sprintf(pid, "%d", getpid());
  8.     int myPID = getpid();
  9.         printf("Parent PID: %d\n", getpid());
  10.     if(fork()==0) // P1
  11.     {
  12.         printf("1 PID: %d, PPID: %d\n", getpid(), getppid());
  13.        
  14.         if(fork() == 0) // P1A
  15.                 {
  16.             printf("1A PID: %d, PPID: %d\n", getpid(), getppid());
  17.             if(fork() == 0)
  18.             {
  19.                                 printf("1AA PID: %d, PPID: %d\n", getpid(), getppid());
  20.                 execlp("pstree", "pstree", "-p", pid, (char *)NULL); //pstree
  21.                 sleep(2);
  22.                 return 0;
  23.             }
  24.             sleep(2);
  25.             return 0;
  26.                 }
  27.         sleep(2);
  28.         return 0;    
  29.     }
  30.     if(fork()==0) // P2
  31.     {
  32.         printf("2 PID: %d, PPID: %d\n", getpid(), getppid());
  33.         if(fork() == 0) // P2A
  34.                 {
  35.             printf("2A PID: %d, PPID: %d\n", getpid(), getppid());
  36.             sleep(2);
  37.             return 0;
  38.                 }
  39.         if(fork() == 0) // P2B
  40.                 {
  41.             printf("2B PID: %d, PPID: %d\n", getpid(), getppid());
  42.             sleep(2);
  43.             return 0;
  44.                 }
  45.         sleep(2);
  46.         return 0;    
  47.     }
  48.     if(fork()==0) // P3
  49.     {
  50.         printf("3 PID: %d, PPID: %d\n", getpid(), getppid());
  51.         if(fork() == 0) // P3A
  52.                 {
  53.             printf("3A PID: %d, PPID: %d\n", getpid(), getppid());
  54.             sleep(2);
  55.             return 0;
  56.                 }
  57.         if(fork() == 0) // P3B
  58.                 {
  59.             printf("3B PID: %d, PPID: %d\n", getpid(), getppid());
  60.                         if(fork()==0) // P3BB
  61.                         {
  62.                         printf("3BB PID: %d, PPID: %d\n", getpid(), getppid());
  63.             sleep(2);
  64.             return 0;
  65.                         }
  66.                         sleep(2);
  67.             return 0;
  68.                        
  69.                }
  70.         if(fork() == 0) // P3C
  71.             {
  72.             printf("3C PID: %d, PPID: %d\n", getpid(), getppid());
  73.             sleep(2);
  74.             return 0;
  75.                 }
  76.         sleep(2);
  77.         return 0;    
  78.     }
  79. sleep(2);
  80. return 0;
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement