Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.17 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.         sleep(2);
  21.         return 0;
  22.  
  23.     }
  24.  
  25.  
  26.  
  27.     if(fork()==0) // P2
  28.     {
  29.         printf("2 PID: %d, PPID: %d\n", getpid(), getppid());
  30.      
  31.         if(fork() == 0) // P2A
  32.                 {
  33.                    
  34.             printf("2A PID: %d, PPID: %d\n", getpid(), getppid());
  35.          
  36.           if(fork() == 0) // P2AA
  37.                 {
  38.             printf("2AA PID: %d, PPID: %d\n", getpid(), getppid());
  39.  
  40.             sleep(2);
  41.             return 0;
  42.                 }
  43.  
  44.             if(fork() == 0) // P2AB
  45.                 {
  46.             printf("2AB PID: %d, PPID: %d\n", getpid(), getppid());
  47.            
  48.                 }
  49.             sleep(2);
  50.             return 0;
  51.                 }
  52.  
  53.     if(fork() == 0) // P2B
  54.                 {
  55.             printf("2B PID: %d, PPID: %d\n", getpid(), getppid());
  56.            // execlp("pstree","pstree", "-p",pid,(char *)NULL);//pstre
  57.            // tu powinno byc ale nie dziala
  58.  
  59.             if(fork() == 0) // P2BA
  60.                 {
  61.             printf("2BA PID: %d, PPID: %d\n", getpid(), getppid());
  62.             sleep(2);
  63.             return 0;
  64.                 }
  65.  
  66.                 if(fork() == 0) // P2BB
  67.                 {
  68.             execlp("pstree","pstree", "-p",pid,(char *)NULL);//pstre
  69.             printf("2BB PID: %d, PPID: %d\n", getpid(), getppid());
  70.             sleep(2);
  71.             return 0;
  72.                 }
  73.  
  74.                 if(fork() == 0) // P2BC
  75.                 {
  76.             printf("2BC PID: %d, PPID: %d\n", getpid(), getppid());
  77.             sleep(2);
  78.             return 0;
  79.                 }
  80.  
  81.            
  82.             sleep(2);
  83.             return 0;
  84.                 }
  85.     }
  86. sleep(2);
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement