waitpid returns -1 'if there is no such child process'. If you call it with none of the children still living, this seems like a reasonable expectation.

Unless it has changed a lot in 5.8 the old advice was to 'Do as little as possible in your signal handler, like writing a status into an already defined and allocated variable' or 'Pretend like the signal stuff is reentrant but be aware that it may not always work that way.' I know the p5p people are working on solid fully reentrant signals but I do not know the progress toward that goal in 5.8.

How about preparing a hash for the children when they are forked and then saving the exit status uninterpreted into that data space?

while((my $pid = waitpid(-1, &WNOHANG)) > 0) { $children{$pid}=$?; # $children--; # pushing your luck # print $children children running\n"; # really pushing }

I think having a print in a signal handler may be really too much time spent away waiting to be interupped by another signal. If you decremented the variable but did not print then maybe the parent could run a polling loop say once a second and just print out the $children variable which you would have to arrange so that the signal handler knows about the same $children as the parent, maybe with a file lexical or something. This would get the print out of the handler and the print of the parent will probably skip numbers as more than 1 child exits during a second but it should kind of keep up. Also you could count the number of children with statuses in the hash and subtract that from the total children to get the count which would make the handler only update the children hash.


In reply to Re: $? is -1??? by dga
in thread $? is -1??? by kscaldef

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.