Advertisement
minh_tran_782

2.1.2 fork()

Feb 13th, 2022
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/unistd.h>
  4.  
  5. int main ()
  6. {
  7.     int p1 = fork();
  8.     int p2 = fork();
  9.     int p3 = fork();  
  10.     if (p1 == 0 && p2 > 0 && p3 > 0 )
  11.     {
  12.        
  13.           printf("Parent A (pid = %d)" , getppid());
  14.         printf(" creates process B (pid = %d)",getpid());
  15.         printf("\n");  
  16.         wait(NULL);
  17.     }
  18.     else if (p1 > 0 && p2 == 0 && p3 > 0)
  19.     {
  20.    
  21.         printf("Parent A (pid = %d)" , getppid());
  22.         printf(" creates process C (pid = %d)",getpid());
  23.         printf("\n");  
  24.         wait(NULL);
  25.     }
  26.     else if (p1 > 0 && p2 > 0 && p3 == 0)
  27.     {
  28.      
  29.         printf("Parent A (pid = %d)" , getppid());
  30.         printf(" creates process D (pid = %d)",getpid());
  31.         printf("\n");  
  32.         wait(NULL);
  33.     }
  34.      else if (p1 == 0 && p2 == 0 && p3 > 0)
  35.     {
  36.      
  37.         printf("Parent B (pid = %d)" , getppid());
  38.         printf(" creates process E (pid = %d)",getpid());
  39.         printf("\n");  
  40.         wait(NULL);
  41.     }
  42.       else if (p1 > 0 && p2 == 0 && p3 == 0)
  43.     {
  44.      
  45.         printf("Parent C (pid = %d)" , getppid());
  46.         printf(" creates process G (pid = %d)",getpid());
  47.         printf("\n");  
  48.         wait(NULL);
  49.     }
  50.     else if (p1 == 0 && p2 > 0 && p3 == 0)
  51.     {
  52.  
  53.         printf("Parent B (pid = %d)" , getppid());
  54.         printf(" creates process F (pid = %d)",getpid());
  55.         printf("\n");  
  56.         wait(NULL);
  57.     }
  58.     else if (p1 == 0 && p2 == 0 && p3 == 0)
  59.     {
  60.         printf("Parent E (pid = %d)" , getppid());
  61.         printf(" creates process I (pid = %d)",getpid());
  62.         printf("\n");  
  63.         wait(NULL);
  64.     }
  65.      
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement