In the following, extremely basic snippet of code, the idea is to fork off 10 processes all doing just the foo() subroutine and then sleep for 2 seconds.

But it only works if $SIG{CHLD} is not explicitly defined. Right now, even with $SIG{CHLD} defined as an empty anonymous subroutine sleep won't behave properly. If you comment out that first line, the script will behave as normally. Example output follows the script.

$SIG{CHLD} = sub { }; while (1) { for(1..10) {spawn(\&foo)} # fork off 10 &foo()'s my $s = sleep (2); # $s will hold the number of # seconds actually slept print "--------------------"; if ($s) { print "Slept for $s seconds\n"; }# If we slept at all else { print "Did not sleep at all\n"; }# If we didn't } sub spawn { return if fork(); # Fork and return the parent exit shift->(); # exec the coderef passed as arg } sub foo { print '*' for(1..5); }

Output with the $SIG{CHLD} line commented out (all output has some asterisks and dashes removed for display purposes):
**************************************---Slept for 2 seconds **************************************---Slept for 2 seconds **************************************---Slept for 2 seconds **************************************---Slept for 2 seconds **************************************---Slept for 2 seconds

And with the $SIG{CHLD} line active.
**************************************---Slept for 2 seconds **************************************---Did not sleep at all **************************************---Did not sleep at all *********************************---Did not sleep at all *******************************************---Slept for 2 seconds **********************************---Did not sleep at all *******************************************---Did not sleep at all

Does anyone have any idea what is going on? This is driving me nuts. Does anyone have an alternate sleep or pause routine that works in this situation?
This is obviously just example code that has been whittled down, but the idea behind it is that sleep just isn't working for me (on several machines) when I am dealing with forked children and want to handle them properly.

In reply to Why is SIGCHLD affecting sleep? by jsoverson

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.