Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Preforked children not killed when parent is killed

by macli (Beadle)
on Mar 22, 2007 at 23:00 UTC ( #606123=perlquestion: print w/replies, xml ) Need Help??

macli has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I run a prefork server sample script from perl cookbook:
#./prefork.pl & # ps -ef | grep prefork.pl root 22626 947 0 15:34 pts/3 ./prefork.pl root 22627 22626 0 15:34 pts/3 ./prefork.pl root 22628 22626 0 15:34 pts/3 ./prefork.pl #kill 22626 #ps -ef | grep prefork root 22627 1 0 15:34 pts/3 ./prefork.pl root 22628 1 0 15:34 pts/3 ./prefork.pl

From what I understand, when I killed the parent, the children should get killed too, but why the children become orphaned and adopted by init.What changes can be made to the scipt to kill the children when parent is killed?

I comment the sig handling code, it does the same thing,

=for nobody sub REAPER { # takes care of dead children my $pid = wait; $children--; delete $children{$pid}; $SIG{CHLD} = \&REAPER; # reset as necessary, should be at end } sub HUNTSMAN { # signal handler for SIGINT local ( $SIG{CHLD} ) = 'IGNORE'; # we're going to kill our chil +dren kill 'INT' => keys %children; exit 0; # clean up with dignity } =cut # Install signal handlers. #$SIG{CHLD} = \&REAPER; #$SIG{INT} = \&HUNTSMAN;

The sample script is below:

#!/usr/bin/perl # preforker - server who forks first use strict; use warnings; use IO::Socket; use Symbol; use POSIX; # establish SERVER socket, bind and listen. my $server = IO::Socket::INET->new( LocalPort => 6969, Type => SOCK_STREAM, Proto => 'tcp', Reuse => 1, Listen => 10 ) or die "making socket: $@\n"; # global variables my $PREFORK = 2; # number of children to maintain my $MAX_CLIENTS_PER_CHILD = 5; # number of clients each child shou +ld process my %children = (); # keys are current child process ID +s my $children = 0; # current number of children my $client; sub REAPER { # takes care of dead children my $pid = wait; $children--; delete $children{$pid}; $SIG{CHLD} = \&REAPER;# reset as necessary, should be at end } sub HUNTSMAN { # signal handler for SIGINT local ( $SIG{CHLD} ) = 'IGNORE'; # we're going to kill our children kill 'INT' => keys %children; exit 0; # clean up with dignity } # Fork off our children. for ( 1 .. $PREFORK ) { make_new_child(); } # Install signal handlers. $SIG{CHLD} = \&REAPER; $SIG{INT} = \&HUNTSMAN; # And maintain the population. while (1) { sleep; # wait for a signal (i.e., child's death) my $i; for ( $i = $children ; $i < $PREFORK ; $i++ ) { make_new_child(); # top up the child pool } } sub make_new_child { my $pid; my $sigset; my $i; # block signal for fork $sigset = POSIX::SigSet->new(SIGINT); sigprocmask( SIG_BLOCK, $sigset ) or die "Can't block SIGINT for fork: $!\n"; die "fork: $!" unless defined( $pid = fork ); if ($pid) { # Parent records the child's birth and returns. sigprocmask( SIG_UNBLOCK, $sigset ) or die "Can't unblock SIGINT for fork: $!\n"; $children{$pid} = 1; $children++; return; } else { # Child can *not* return from this subroutine. $SIG{INT} = 'DEFAULT'; # make SIGINT kill us as it did befo +re # unblock signals sigprocmask( SIG_UNBLOCK, $sigset ) or die "Can't unblock SIGINT for fork: $!\n"; # handle connections until we've reached $MAX_CLIENTS_PER_CHIL +D for ( $i = 0 ; $i < $MAX_CLIENTS_PER_CHILD ; $i++ ) { $client = $server->accept() or last; # do something with the connection } # tidy up gracefully and finish # this exit is VERY important, otherwise the child will become # a producer of more and more children, forking yourself into # process death. exit; } }

Replies are listed 'Best First'.
Re: Preforked children not killed when parent is killed
by Zaxo (Archbishop) on Mar 23, 2007 at 03:04 UTC

    A server is likely to have a custom SIGHUP handler, being a daemon. SIGHUP is what is really sent to child processes when the parent exits.

    If the child sets $SIG{HUP} to exit, all should be well.

    After Compline,
    Zaxo

Re: Preforked children not killed when parent is killed
by Trizor (Pilgrim) on Mar 22, 2007 at 23:10 UTC
    You need to handle the TERM signal, which is what kill sends by default.

    In your handler you should either relay the TERM signal and then wait for your children to shutdown. Or send them the KILL signal to bring them down NOW.

    IIRC you can relay signals easier by becoming the head of a process group containing you and your children then recieve the signal and relay it to -$$ to send it to your process group.

    Moral of the story is handle TERM and send it to your kids.
Re: Preforked children not killed when parent is killed
by ikegami (Pope) on Mar 22, 2007 at 23:26 UTC

    kill sends TERM by default, not INT (at least on my system). What happens when you issue kill -INTpid?

      Yes, you are right. If I add  $SIG{TERM}  = \&HUNTSMAN; Or kill -INT pid_of_parent the children get killed.
      Thanks guys for the clarification!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://606123]
Approved by Joost
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2021-06-24 21:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What does the "s" stand for in "perls"? (Whence perls)












    Results (133 votes). Check out past polls.

    Notices?