Advertisement
Guest User

Untitled

a guest
Apr 5th, 2020
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.60 KB | None | 0 0
  1. for(i = 0; i < 2; ++i){
  2.   pid_t ret = fork();
  3.  
  4.   if(ret < 0){
  5.     printf("Fork Failed!\n");
  6.     return 1;
  7.   } else if(ret == 0) { //Children process
  8.     pid_t children_ret = fork();
  9.  
  10.     if(children_ret < 0){
  11.       printf("Fork Failed!\n");
  12.       return 1;
  13.     } else if(children_ret == 0) { //Grandchildren process
  14.      printf("\n [ID =%d] My parent is [%d]\n", getpid(), getppid());
  15.       exit(0);
  16.     }
  17.  
  18.      printf("\n [ID =%d] My parent is [%d]\n", getpid(), getppid());
  19.     wait(NULL);
  20.     exit(0);
  21.   }
  22. }
  23.  printf("\n [ID = %d] I am the root parent \n", getpid());
  24. wait(NULL);
  25. exit(0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement