Okay so the following code is supposed to:

loop infinitely
 have a nested loop that iterates n times (in this case 10) 
  within the nested loop there is a conditional fork (fork on 6th loop)
  sleep after completing the nested loop

Instead it proceeds as expected initially but after a few (2) iterations there are mutliple children which execute simultaneously... The problem goes away if I remove the CHLD handler, but then I have zombie children... If I set CHLD to IGNORE the problem goes away (though I always have *one* zombie)

It seems to be related to the fact that it is somehow running through the nested loop multiple times?

Looping 6 (28680) ...
I am child 28682 [0]
Looping 6 (28680) ...

Any ideas?

sub REAPER { $waitedpid = wait; print "GOT PID: $waitedpid\n"; $SIG{CHLD} = \&REAPER; } $SIG{CHLD} = \&REAPER; while(1){ my $i=0; print scalar localtime(time()), "\n"; for(;$i<10;$i++){ print "Looping $i ($$) ...\n"; if( $i == 5 ){ if( ! defined($pid = fork) ){ print "Danger danger Will Robinson!\n"; } unless( $pid ){ print "I am child $$ [$pid]\n"; exit 0; } } } sleep 5; }

In reply to Problem with children (out of conrol forking vs. zombies) by belg4mit

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.