-1

I need to write a code that will create the tree below by forking but the proccess P0 waits P2 before it terminates and P2 waits at least to of its kids!

                   P0
                  /  \  
               P1    P2
                     /|\
                  P3 P4 P5
JAcj a
  • 1
  • Possible duplicate of [Make parent wait for all child processes](http://stackoverflow.com/questions/19461744/make-parent-wait-for-all-child-processes) – ceving Apr 12 '17 at 10:46
  • `P0->P2->wait for P2->P3, P4, P5. P0 ->P1` Look at the return value of `fork()` and you will know where to place other forks. – Tony Tannous Apr 12 '17 at 21:21

1 Answers1

0

two for loops can help you create that process tree. the first loop helps create

for(int child = 0; child < 2; child++){
pid = fork();
if(pid<0){
    perror("\n there is a fork() error. \n");   
}
else if (pid == 0){
    //P1
    //P2
    if(child == 1){
        for(int grandchild = 0; grandchild < 3; grandchild++){
            fork();
            if(pid==0){
            if(grandchild == 2){
                wait(NULL); 
            }
            else{}

            }
        }

     }
}
else{
      //parent portion  
      if(child == 1){
        wait(NULL);
      }

    }
}

its not syntax accurate, but i am confident it will point you in the right direction.

Kush
  • 438
  • 1
  • 7
  • 20