Note that you should never print inside the subroutines of signals, thus, for production code:

BAD:

local $SIG{'CHLD'} = sub { print "dead child\n"; $timeout=0 };

GOOD:

local $SIG{'CHLD'} = sub { $timeout=0 };

Also, in the above code, $pid == $child_pid (only $child_pid is send from the child to the parent in a IPC)

So if you look at the code from perlplexer in

Perl IPC: Checking Exit Status of Child Process

This is something you actually want:

#!/usr/bin/perl use strict; use warnings; my $timeout = 10; my $parent_pid = $$; my $child_pid = fork(); die "Can't fork() : $!\n" unless defined $child_pid; if ($child_pid){ # parent eval { local $SIG{ALRM} = sub { die "timed-out\n" }; alarm $timeout; if (waitpid($child_pid, 0) > 0){ my ($rc, $sig, $core) = ($? >> 8, $? & 127, $? & 128); if ($core){ print "$child_pid dumped core\n"; }elsif($sig == 9){ print "$child_pid was murdered!\n"; }else{ print "$child_pid returned $rc"; print ($sig?" after receiving signal $sig":"\n"); } }else{ print "$child_pid... um... disappeared...\n"; } alarm 0; }; if($@ eq "timed-out\n"){ print "$child_pid... parent got bored waiting for it...\n"; } }else{ # child print "child $$ is running...\n"; my $ret = system("sleep 20"); exit $ret; }

edit: added alarm

edit2: After reading more about what you actually are trying to do, you also need to loop over the waitpid in such a way that it returns the pid of the child that is ready. Implementing multi queued children is a bit harder. And would require more details about what you require, as, for example, you can prestart 10 children, that are idle until some work for them is done, or you have a single activity queue and a maximum of X children to do the work, and spawn them on the fly when they need to do something.

Checking which child is ready to give you their exitcode can be done with WNOHANG, see waitpid

Depending on what you want to do, independant threads are also an option: threads


In reply to Re^2: get same return that system gives and get pid by FreeBeerReekingMonk
in thread get same return that system gives and get pid by Raiden690

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.