Advertisement
Gazishahi

Lab Quiz

Mar 17th, 2023
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <unistd.h>
  4. #include <sys/wait.h>
  5. #include <stdlib.h>
  6.  
  7. int main() {
  8.     pid_t pid1, pid2, pid3;
  9.    
  10.     printf("Parent PID: %d\n", getpid());
  11.    
  12.     pid1 = fork();
  13.    
  14.     if (pid1 == 0) {
  15.         printf("Child 1 PID: %d, Parent PID: %d\n", getpid(), getppid());
  16.        
  17.         pid2 = fork();
  18.        
  19.         if (pid2 == 0) {
  20.             printf("Child 2 PID: %d, Parent PID: %d\n", getpid(), getppid());
  21.         }
  22.         else {
  23.             pid3 = fork();
  24.            
  25.             if (pid3 == 0) {
  26.                 printf("Child 3 PID: %d, Parent PID: %d\n", getpid(), getppid());
  27.             }
  28.             else {
  29.                 wait(0);
  30.             }
  31.         }
  32.     }
  33.     return 0;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement