Killing all child processes upon parent exit



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
.



Relevant Pages

  • Re: ftpassword
    ... print STDOUT "\n$program: $shell is not among the valid system shells. ... if ($hash eq $curpasswd) { ... --md5 Use the MD5 algorithm for encrypting passwords. ...
    (Fedora)
  • Having trouble porting an application to MS-Windows
    ... doesn't port to Win32. ... a single character response (ACK or NAK) is expected in return. ... print STDOUT " NAK"; ...
    (perl.beginners)
  • length is 0 with unpack_socketaddr_in
    ... I got the following error message when the client drops its connection ... socket; my $host = INADDR_ANY; ... print STDOUT "Server port: $port\n"; ...
    (comp.lang.perl.misc)