if(pid==0){ //child1
pid = fork();
if(pid==0){//child1 of child1
print("I am child 1 of child 1");
}
else{//child1
pid = fork();
if(pid==0){//child 2 of child 1
print("I am child 2 of child 1");
}
else{//child 1 waits on children
print("I am child 1");
}
}
}
else{ //parent
pid = fork();
if (pid==0){//child2
pid = fork();
if(pid==0){//child1 of child 2
print("I am child 1 of child 2");
}
else{//child2
pid = fork();
if(pid==0){//child2 of child 2
print("I am child 2 of child 2");
}
else{//child 2 waits on children
print("I am child 2");
int sum2 = sum(secondHalf,count);
}
}
}
else{//parent
//i want to wait here for children and grandchildren process
}
}
Hey guys I'd like to wait on all my child processes and grand child processes before proceeding on with the parent. I tried giving each process a unique id and waitpiding but the following did not work either
pid_t child1,child2,child1ofchild1,child2ofchild1,child1ofchild2,child2ofchild2;
int status;
child1 = fork();
if(child1==0){ //child1
child1ofchild1 = fork();
if(child1ofchild1==0){//child1 of child1
print("I am child 1 of child 1");
exit(1);
}
else{//child1
child2ofchild1 = fork();
if(child2ofchild1==0){//child 2 of child 1
print("I am child 2 of child 1");
exit(1);
}
else{//child 1 waits on children
print("I am child 1");
exit(1);
}
}
}
else{ //parent
child2 = fork();
if (child2==0){//child2
child1ofchild2 = fork();
if(child1ofchild2==0){//child1 of child 2
print("I am child 1 of child 2");
exit(1);
}
else{//child2
child2ofchild2 = fork();
if(child2ofchild2==0){//child2 of child 2
print("I am child 2 of child 2");
exit(1);
}
else{//child 2 waits on children
print("I am child 2");
}
}
}
else{//parent
waitpid(child1, &status, WNOHANG|WUNTRACED);
waitpid(child1ofchild1, &status, WNOHANG|WUNTRACED);
waitpid(child2ofchild1, &status, WNOHANG|WUNTRACED);
waitpid(child2, &status, WNOHANG|WUNTRACED);
waitpid(child1ofchild2, &status, WNOHANG|WUNTRACED);
waitpid(child2ofchild2, &status, WNOHANG|WUNTRACED);
print("got here");
}
}
essentially I'd like to create a process tree which the main parent creates two children and their children create two children. I set it up this way because id like to have control over what happens in each specific process. For some reason I cannot wait for all the children and grandchildren processes to end before continuing on with the main process. Can anyone help me?