I'm wondering that, when I'm using fork and creating several children and then the children are creating several children.
What should I do when a child finishes its task?
void foo(){
pid_t childpid = 0;
.
.
.
while (..){
childpid = fork();
if ( childpid == 0)
if(...)
foo();
else
/* Do something */
}
while(wait(NULL) > 0);
}
This is very basic code because I'm trying to learning fork mechanism.
The question is: When the child's task is completed, what should I do? Should I use exit(0)
for kill him or break
?
If I use kill, valgrind gives me still reachable
; if I use break, the process does not die and returns to the main function.
And second question: if child into first if condition, it calls foo again. Then there is a children in it. Should it wait or not?
What should I do?