<?xml version="1.0" encoding="windows-1252"?>
<node id="60383" title="Waiting.." created="2001-02-22 22:01:58" updated="2005-07-25 05:53:33">
<type id="115">
perlquestion</type>
<author id="38527">
spaz</author>
<data>
<field name="doctext">
Fellow Monks,&lt;br&gt;
In one of my scripts I start up several child processes as follows&lt;code&gt;foreach $item ( @array ) {
    if( $pid = fork ) {
        print "Forking off to $pid\n";
    } elsif( defined $pid ) {
        &amp;proprietary_function( $item );
        print "Done with function\n";
        exit;
    } else {
        die "Can't fork: $!";
    }
}
&lt;/code&gt;
My question is this:&lt;br&gt;
&amp;nbsp;&amp;nbsp;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?&lt;br&gt;
I've looked at [perlipc] and [wait] but I'm getting kind of confused about reaping and when to handle it.&lt;br&gt;
Could I use something like&lt;code&gt;sub REAPER {
    my $waitedpid = wait;
    $SIG{CHLD} = \&amp;REAPER;
}
&lt;/code&gt;
And then change&lt;code&gt;    if( $pid = fork ) {
        print "Forking off to $pid\n";
&lt;/code&gt;
To &lt;code&gt;    if( $pid = fork ) {
        $SIG{CHLD} = \&amp;REAPER;
        print "Forking off to $pid\n";
&lt;/code&gt;
?
&lt;br&gt;&lt;br&gt;-- Dave</field>
</data>
</node>
