All the documentation I've encountered claims that $? should be non-negative (more specifically, an unsigned 16-bit integer). However, I have some code where $? is getting set to -1 and I have no idea what this means.

The code in question spawns a bunch of worker children, with a SIG{CHLD} handler to keep track of when they all finish, which looks like this (with some added debugging statements):

sub REAPER { while ((my $pid = waitpid(-1, &WNOHANG)) > 0) { if (WIFEXITED($?)) { } elsif (WIFSIGNALED($?)) { print "$?\n", $? >> 8, "\n", $? & 127, "\n"; print( "child exited with signal: ", WTERMSIG($?), "\n"); } elsif (WIFSTOPPED($?)) { print "child stopped????\n"; next; } else { print "hmmm\n"; } delete $children{$pid}; $children--; print "$children children running\n"; } $SIG{CHLD} = \&REAPER; #probably not needed, but paranoia }

Occasionally, this produces output like:

...
7 children running
6 children running
-1
72057594037927935
127
child exited with signal: 127
5 children running
...

I think that there might be some sort of race condition or reentrancy problem involved, as when I purposely space the children out so that their exits are well seperated, this never happens. However, the normal operation is for them to do very similar amounts of work, and therefore finish very close to one another. However, my understanding is that while the SIGCHLD handler is executing, any other SIGCHLD should be blocked (thus the while loop), so I wouldn't have thought this would be an issue.

So, I ask you, what is going on here? Is the documentation incorrect, am I screwing something up, or is this a bug in perl? I'm using 5.8.0, btw.


In reply to $? 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.