in reply to Re: Help with Fork bomb
in thread Help with Fork bomb

I think I've seen instances when exit in the child kills the parent. Is that possible? How does one defend against it?

Replies are listed 'Best First'.
Re^3: Help with Fork bomb
by blue_cowdawg (Monsignor) on Aug 30, 2012 at 20:32 UTC
        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; }
        Slightly off topic (but rather important in some circumstances)...

        Perl is not C ...
            and   printf   is not Perl's   print   (nor is it say).

        So perhaps you should reconsider using it before it gets to be a habit that gets you in trouble.

Re^3: Help with Fork bomb
by aitap (Curate) on Aug 30, 2012 at 20:42 UTC
    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.