Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.81 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.             sleep(2);
  18.             return 0;
  19.                 }
  20.     if(fork() == 0)//P1B
  21.     {
  22.         printf("1B PID: %d, PPID: %d\n", getpid(), getppid());
  23.         sleep(2);
  24.         return 0;
  25.     }
  26.  
  27.     if(fork() == 0)//P1C
  28.     {
  29.         printf("1C PID: %d, PPID: %d\n", getpid(), getppid());
  30.         sleep(2);
  31.         return 0;
  32.     }
  33.  
  34.         sleep(2);
  35.         return 0;    
  36.     }
  37.  
  38.  
  39.  
  40.     if(fork()==0) // P2
  41.     {
  42.         if(fork() == 0) // P2A
  43.                 {
  44.             printf("2A PID: %d, PPID: %d\n", getpid(), getppid());
  45.             sleep(2);
  46.             return 0;
  47.                 }
  48.  
  49.     if(fork() == 0) // P2B
  50.                 {
  51.             printf("2B PID: %d, PPID: %d\n", getpid(), getppid());
  52.             sleep(2);
  53.             return 0;
  54.                 }
  55.  
  56.  
  57.     if(fork() == 0) // P2C
  58.                 {
  59.             printf("2C PID: %d, PPID: %d\n", getpid(), getppid());
  60.             sleep(2);
  61.             return 0;
  62.  
  63.         if(fork() == 0) // P2CA
  64.                 {
  65.             printf("1A PID: %d, PPID: %d\n", getpid(), getppid());
  66.             sleep(2);
  67.             return 0;
  68.         }
  69.  
  70.     if(fork() == 0) // P2CB
  71.                 {
  72.             printf("1A PID: %d, PPID: %d\n", getpid(), getppid());
  73.     execlp("pstree","pstree", "-p", (char *)NULL); //pstree
  74.             sleep(2);
  75.             return 0;
  76.                }
  77.  
  78.  
  79.             sleep(2);
  80.             return 0;
  81.                 }
  82.        
  83. sleep(2);
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement