Killing all child processes upon parent exit
- From: bsder <snort_sam@xxxxxxxxx>
- Date: Wed, 30 Nov 2005 20:58:07 +1100
Hi,
Could anyone please tell me how to kill all child processes when parent exit?
Here is the server code:
#!/usr/bin/perl
use strict; use Socket; use Data::Dumper;
# forward declaration
sub logmsg { print "$0 $$: @_ at ", scalar localtime, "\n" }$SIG{INT} = $SIG{TERM} =$SIG{HUP} = \&signal_handler;
$SIG{CHLD} = 'IGNORE';
$SIG{CLD} = 'IGNORE';my %kids = {};
my $proto = getprotobyname('tcp');
socket(SOCK,PF_INET,SOCK_STREAM,$proto) || die "socket : $!";
my $port = 7888;
my $address = pack_sockaddr_in($port, INADDR_ANY);bind(SOCK, $address) || die "bind : $!";
while (1) {
listen(SOCK, SOMAXCONN) || die "listen: $!";
my $hostName = inet_ntoa(INADDR_ANY);
logmsg "server started on port $port, $hostName"; sub REAPER {
my $waitedpid = wait;
$SIG{CHLD} = \&REAPER;
logmsg "reaped $waitedpid" . ($? ? " with exit $?" : '');
}
$SIG{CHLD} = \&REAPER;print STDOUT "Server host: $hostName\n";
print STDOUT "Server port: $port\n";
(my $addr = accept(NEWSOCK,SOCK)) or ($! eq 'Interrupted system all' and redo) or die $!;
select(NEWSOCK); $| = 1; select(STDOUT);
my $kidpid;
if (!defined($kidpid = fork())) {
die "cannot fork: $!";
}
elsif ($kidpid == 0) { # child
my ($cPort, $cHost) = unpack_sockaddr_in($addr);
my $cHostName = inet_ntoa($cHost);
print STDOUT "Client host: $cHostName\n";
print STDOUT "Client port: $cPort\n";
print NEWSOCK "Welcome to Code Generator.\r\n"; while (my $m=<NEWSOCK>) {
$m =~ s/\n|\r//g;
last if ($m eq ".");
print NEWSOCK "Server received $m\r\n";
}
close(NEWSOCK) || die "close in child: $!";
delete %kids->{$kidpid};
exit;
}
}
sub signal_handler { local $SIG{HUP} = 'IGNORE'; kill HUP => -$$; print STDOUT "going to die\n"; exit; }
Client code: #!/usr/bin/perl
$domain = 2; # Internet domain $type = 1; # Sequenced, reliable, two-way connection, byte streams $proto = 6; # Transmission Control Protocol (TCP) socket(SOCK,$domain,$type,$proto); $host = pack('C4', 127,0,0,1); # localhost = 127.0.0.1 $port = 1024; $address = pack('S n a4 x8', $domain, $port, $host); bind(SOCK, $address); print STDOUT "Client host: ",join('.',unpack('C4', $host)),"\n"; print STDOUT "Client port: $port\n"; $sHost = pack('C4', 127,0,0,1); # localhost = 127.0.0.1 $sPort = 7888; $sAddress = pack('S n a4 x8', $domain, $sPort, $sHost); connect(SOCK, $sAddress); print STDOUT "Server host: ",join('.',unpack('C4', $sHost)),"\n"; print STDOUT "Server port: $sPort\n"; select(SOCK); $| = 1; select(STDOUT); while ($m=<SOCK>) { print STDOUT $m; $m = <STDIN>; print SOCK $m; } close(SOCK); exit;
Thanks Sam .
- Follow-Ups:
- Re: Killing all child processes upon parent exit
- From: Anno Siegel
- Re: Killing all child processes upon parent exit
- Prev by Date: Re: Perl subtraction wierdness
- Next by Date: Re: Killing all child processes upon parent exit
- Previous by thread: Time::HiRes usleep on windows strange behaviour...please please help!!
- Next by thread: Re: Killing all child processes upon parent exit
- Index(es):
Relevant Pages
|