You might want different behavior if the parent is just done, or if you've gone and SIGINTed (control-c) the parent. For This is wrote a little set that had some useful code in it: The code follows:
use POSIX qw(:sys_wait_h); use IO::Handle; my %scythe; # a scythe for the REAPER (see FAQ); $SIG{CHLD} = \&REAPER; # set handling of zombie child process +es. STDOUT->autoflush; # make sure output is flushed before f +orking # setup an interupt handler # kills every pid used as a key in %scythe #################### $SIG{INT} = sub { print "\nCleaning: "; print join(", ", keys(%scythe)); print "\n"; kill('INT', keys %scythe); print "done with: "; print join(", ", keys(%scythe)); die "\n"; }; #### MAIN #### #### END MAIN, begin defs #### sub phork { # we will call fork and return a pid. The child will exec with all +args # and suppress the child's output; my $pid; if ($pid = fork) { # fork the process; #parent return $pid; }else { #child die "CANNOT FORK!!\n" unless defined $pid; close(STDOUT); # suppressing output close(STDERR); # suppressing output {exec(@_);}; # calls exec with current @_ exit(1); # exec may maybe fail... maybe +. } } sub baitKiller { # This functions is a dummy to be used to be added to %scythe for a $ +pid # This way when the interupt is called, the process with the $pid wil +l # be cleaned up (killed) so it doesn't keep running after its parent' +s # death. If you'd rather it did, just don't assign anything in %scyt +he # for the key of it's $pid. return "Come get some!"; } sub REAPER { # we reap child processes, and post process it. # note: this sub relies on a global %scythe # sort of... if a $scythe{$pid} is defined, it will assume that its + a # reference to a function that takes $pid and $exit_value as args # and presumably does something useful with that. Then it deletes + the # $scythe{$pid} hash entry. is it not nifty? # local vars my $pid; my $exit_value; $pid = waitpid(-1, &WNOHANG); if($pid == -1) { # no child waiting } elsif (WIFEXITED($?)) { # the process exited, get exit value. my $slot; my $state; $exit_value = $? >> 8; if (exists($scythe{$pid})) { $scythe{$pid}->( $pid, $exit_value ); delete($scythe{$pid}); } else { # we're reaping something we didn't sow or didn't care about; return; } } else { # false alarm on $pid } $SIG{CHLD} = \&REAPER; #reset signal handling. } #end sub REAPER

In reply to Re: forking through a subroutine by daniell
in thread forking through a subroutine by jptxs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.