Here is a scaled down version of what you are trying to do in a fashion that will work...

#!/usr/bin/perl -w use strict; $SIG{CHLD}=\&catchChildDeath; # Track how many children we have running our $children=0; # Limits on min and max for number of children to run my $min_child=5; my $max_child=10; # Enable or disable our ability to spawn my $spawnEnabled=1; # Run until killed while(1){ spawn(); sleep 5; } exit(0); # Deal with the death of a child. A sad event to be sure. sub catchChildDeath{ printf "Death of child noted.\n"; $children--; $children = ( $children < 0 ? 0 : $children); unless ($spawnEnabled){ $spawnEnabled = ( $children < $min_child ? 1 : 0 ); } printf "There are %d children running\n",$children; } # Spawn if we must, or not. sub spawn{ unless ( $spawnEnabled ) { if ($children <= $min_child ){ $spawnEnabled=1; } else { return unless $spawnEnabled; } } if ( fork()){ $children++; $spawnEnabled = 0 if $children > $max_child; printf "%d children running\n",$children; } else { kidPlay(); } } # This encapsulates the behavior of the child. sub kidPlay{ my $iter=0; printf "Child PID: %d\n",$$; while($iter++ < 10){ sleep 1; } printf "Child PID: %d exiting..",$$; exit(0); }
Of particular note is the use of $SIG{CHLD} to catch the event of a child process exiting. Also the counters and limit values that I use to track what is going on.

When I ran this it worked marvelously. Add to it what you want. Disclaimer: this code only minimally tested and is not to be used in conjunction with ICBM launchers, nuclear reactors or transporter rooms.


Peter L. Berghold -- Unix Professional
Peter -at- Berghold -dot- Net; AOL IM redcowdawg Yahoo IM: blue_cowdawg

In reply to Re: Help with Fork bomb by blue_cowdawg
in thread Help with Fork bomb by Anonymous Monk

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.