under the hood, system should be something similar to:
sub system { my $pid = fork; if (!$pid) { exec @_; exit(-1); } while (1) { if (waitpid($pid, \$?, 0) > 0) { # supposing this calls the C wait +pid! return $? } if ($! == EINTR) { run_signal_handlers(); next; } return -1; } }
Setting a signal handler for SIG{CHLD} probably interrupts the waitpid call, your handler is run and as it calls wait(), the process just run gets reaped.

When waitpid is called again, it finds that the child it should be waiting for has been already reaped, and so, returns an error.

If you check $! it should contain the value ECHILD.

update: I think this could be considered a perl bug, so report it to p5p!


In reply to Re: system() returns -1 with $SIG{CHLD}? by salva
in thread system() returns -1 with $SIG{CHLD}? by cmv

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.