Home > Archive > Unix Programming > September 2005 > waiting for all children
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
waiting for all children
|
|
|
| I am trying to develop an application in which the parent is given some tasks.
The parent creates some children and assigns the responsibility of doing those jobs to
the children. Now the parent wants to be reliable to the external process which
gave tasks to parent. So it has to watch the children till they terminate and
after termination of children normally(child processes exiting when the tasks
given to them are over and not due to signals or other reasons) report to the
external process that task given was done successfully. So the parent keeps an watch
over all the children. Now I want to know how can it keep watch over all children and
not miss any of them while it is in the signal handler of one terminated child
(I hope the below code clarifies my question).
int main() {
int child_pids[MAX]; // am currently using MAX as right now this is a rough sketch only
catch_signal(); // signal handlers (here i am catching SIGCHLD only )
while(i am getting tasks from external processes) {
pid = fork();
// Assuming i check for fork errors
if(pid == 0) { // if i am in child
execl(path of task given to parent ,task given to parent,(char*)0);
}
else { // if i am in parent
child_pid[i] = pid; // assuming i as a counter which is incremented to store child's pid
}
} // end while which is waiting for tasks from external process
} // end main
// here i handle just SIGCHLD
void catch_signal() {
signal(SIGCHLD,catch_child);
}
// Now in catch_child signal handler i am doing like this
void catch_child() {
int status,pid;
while ((pid = (waitpid(-1,status,WNOHANG))) != 0) {
lookup_child(pid,child) // look it up and find which program was it running
child->status = status; // assuming child is a structure and one of its // members is status
// Now if child terminated normally do nothing and return and continue to monitor
//other remaining children but if it has terminated abnormally restart it
if(check_child_status(child) == NORMAL) { }
else
restart_child(child);
} // end while of waitpid
} // end catch_child
Now I am puzzled if parent starts seven children and three of them terminate in
a short interval how do I know that three have terminated(not less). If i am in
the signal handler catch_child and restarting process 1,how can i know
processes 2,3 have terminated. As the SIGCHLD of process 2 and
3 will be blocked while i am in the handler function for signal of process
one how do I ensure i dont miss any SIGCHLD's for processes and handle
them in order they are recieved. I want to know if there
is a way to do this.
Also waitpid using the WNOHANG option is the correct way to wait for
all children to complete without blocking or is there some other way
which will allow me to wait for all children without blocking
Thanks a lot.
| |
| Gustavo Rios 2005-09-17, 7:01 pm |
| AS you said, while handling SIGCHLD, the delivering of new signal will
be blocked, NOT LOST.
As soon as you leave it, it will be invoked again.
|
|
|
|
|