Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Help with Fork bomb

by aitap (Curate)
on Aug 30, 2012 at 15:53 UTC ( #990789=note: print w/replies, xml ) Need Help??

in reply to Help with Fork bomb

$pid = fork; if ($pid == 0){ # Is a child $iterations++; sleep 1; } ... } } if ($pid == 0){ # Is a child exit; }
I think, the problem might be there: every child continues the loop after sleep 1 and creates more and more grandchildren. Placing exit after sleep fixed the issue for me: before after.

By the way, separate shell with ulimit -u 300 was very useful for testing this code.

Sorry if my advice was wrong.

Replies are listed 'Best First'.
Re^2: Help with Fork bomb
by Anonymous Monk on Aug 30, 2012 at 20:11 UTC
    I think I've seen instances when exit in the child kills the parent. Is that possible? How does one defend against it?
          I think I've seen instances when exit in the child kills the parent

      Parricide? I suppose, but if you set up proper handling of SIGCHLD in your code and have a plan of what to do you should be OK.

      # ---- yadayadayada # # Handler for SIGCHLD $SIG{CHLD}=sub { # do something here } ;

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

        I'm using Blue_cowdawg's code. It does this:

        $SIG{CHLD}=\&catchChildDeath; # 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; }

        My sub that the child runs exits at its end:

        sub my_child_work{ my $x = shift; print "child x $x "; sleep 1; exit; }
      In addition to the previous comment, by default, perl ignores SIGCHLD, so dead children remain as zombies and die only when parent dies. You can avoid it by handling SIGCHLD as specified above.
      Sorry if my advice was wrong.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://990789]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2018-03-18 21:55 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (231 votes). Check out past polls.