in reply to POSIX and sigaction
Second, why are you using a handler? It seems to me you are not ( at least in this example ) gaining anything by installing a handler. You want the parent to handle reaping the children. Signal handlers are really only useful when you want something to happen outside the scope of the parent process and that "something" needs to happen immediately. I do not gather you are working like that. If a child waits a few seconds for you to reap it, will there be any harm?
This snippet is from a code that spawns N children and gives them something to do from a job queue. When a child finishes, ie exits, I simply spawn another child off. Note the loop at the end to wait for the last N children to finish.
while( @jobq ) { if ( $counter < $num_children ) { &SPAWN( $ref ); } else { do { sleep 5; $dpid = waitpid( -1, 1 ); } until $dpid; last if ( $dpid == -1 ); # No children left printf "%s has finished, spawning next child (%d left)\n", $tr +acker{$dpi d}, scalar @jobq; &SPAWN( $ref ); } } do { sleep 5; $dpid = waitpid( -1, 1 ); printf("%s has finished\n", $tracker{$dpid}) if(defined( $tracker{ +$dpid} ) ); } until $dpid == -1;
|
|---|