http://www.perlmonks.org?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