http://qs321.pair.com?node_id=60383

spaz has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monks,
In one of my scripts I start up several child processes as follows
foreach $item ( @array ) { if( $pid = fork ) { print "Forking off to $pid\n"; } elsif( defined $pid ) { &proprietary_function( $item ); print "Done with function\n"; exit; } else { die "Can't fork: $!"; } }
My question is this:
  If I were to build up a list of PID's, how could I wait for them all to exit, and if they don't exit after a certain amount of time, kill them?
I've looked at perlipc and wait but I'm getting kind of confused about reaping and when to handle it.
Could I use something like
sub REAPER { my $waitedpid = wait; $SIG{CHLD} = \&REAPER; }
And then change
if( $pid = fork ) { print "Forking off to $pid\n";
To
if( $pid = fork ) { $SIG{CHLD} = \&REAPER; print "Forking off to $pid\n";
?

-- Dave

Replies are listed 'Best First'.
Re: Waiting..
by TheoPetersen (Priest) on Feb 23, 2001 at 03:10 UTC
    You might find Parallel::ForkManager useful. It was made to make this sort of thing easy, and your sample code is almost an example from the pod.
Re: Waiting..
by mikfire (Deacon) on Feb 23, 2001 at 03:35 UTC
    Sigh. Look. You want to reap the children yourself, right? Stop invoking the magic of the signal handler and do it your own bad self. Oh, and stop re-installing the signal handler until you know you need to do it.

    The idea would be like this

    # Fork a child sub SPAWN { if ( $cpid = fork ) { $tracker{$cpid} = 1; } elsif ( defined $cpid ) { # do interesting things } else { die "What a world, what a world!"; } } # Spawn all your children SPAWN() for ( @jobs ); # Wait for them to die do { select( undef, undef, undef, 0.5 ); # 1/2 second sleep $counter++; $dpid = waitpid(-1,1); # non-blocking waitpid if ( defined( $dpid ) && $dpid != 1 ) { if ( defined( $tracker{$dpid} ) ) { print "Child $dpid is dead\n"; delete $tracker{$dpid}; } else { print "I am now terribly confused.\n"; } } until ( $dpid == -1 || $counter > 2000 ); if ( @temp = keys %tracker ) { print "Timed out while waiting for these pids:@temp\n"; }
    Obviously, this is pretty quickly written code - several declarations and initializations are left as an excercise to the reader :) Notice I wait 1,000 seconds before giving up. You really ought to make the a variable set on the command line. The sleep loop is actually pretty tight. You could, depending on how long lived the processes are, make it longer. Use sleep() if the interval is greater than 1 second.

    The spawning loop can be much fancier as well - I have several examples that wait for a child to finish and then spawn another off until there are no more jobs to be done. You can pass information into the SPAWN routine ( like the name of the file the child process is handling ) and print nicer messages.

    I have several refinements on this idea, if you are interested but I haven't the free time to go into all of them. Email me ( mik at speed dot stdio dot com ) if you would like to see them.

    mikfire

Re: Waiting..
by merlyn (Sage) on Apr 16, 2001 at 21:33 UTC