package Mgr; use strict; use Global1; sub new { my $this = {}; $this->{children} = {}; bless $this; return $this; } sub killAll { my $this = shift; $SIG{CHLD} = "IGNORE"; foreach (keys %{$this->{children}}) { my $cpid = $this->{children}->{$_}->{pid}; if (kill INT => $cpid) { Global1::error ("Killed child $cpid\n"); } delete $this->{children}->{$_}; } } my $usr1 = 0; my $usr2 = 0; sub monitor { my $this = shift; my $num = 0; my $pid; my $ppid = $$; while ($num < 2) { unless ($pid = fork()) { # Child $SIG{USR1} = \&doPing; $SIG{INT} = \&gotINT; $SIG{HUP} = 'IGNORE'; $SIG{USR2} = 'IGNORE'; CHILD_START: while (! $usr1) { Global1::error ( "Child $$ sleeping for 10 seconds\n" ); sleep(10); } $usr1 = 0; eval { `echo 1 > $$`; my $numkill = kill 'USR2' => $ppid; Global1::error ( "\t$$ Sent SigUSR2 to parent process \n" ); goto CHILD_START; }; sub doPing { $usr1 = 1; } sub gotINT { Global1::error ( "$$ got INT signal - exiting\n" ); exit; } } # Parent Code $this->{children}->{$num}->{pid} = $pid; Global1::error ( "Spawned pinger process $pid child ID # $num\n" ); $num++; } foreach my $ch (keys %{$this->{children}}) { my $numkill = kill 'USR1' => $this->{children}->{$ch}->{pid}; Global1::error ( "Parent $$ sent SigUSR1 to process ".$this->{children}->{$ch}->{pid}." \n" ); } $SIG{USR2} = \&catchUsr2; $SIG{HUP} = \&main::catchHup; $SIG{CHLD} = "IGNORE"; $SIG{INT} = \&main::catchInt; if ($DEBUG) { my $msg = ""; foreach (keys %SIG) { $msg .= "$_ => $SIG{$_}\n"; } Global1::error ( "Parent's SIG hash:\n$msg\n" ); } HANGOUT: while (! $usr2) { sleep 1; } $usr2 = 0; my $done = 0; $num = 0; foreach $num (keys %{$this->{children}}) { my $fc = $this->{children}->{$num}->{pid}; if (-e $fc) { Global1::error ( "Parent process $$ recieved USR2 signal from child $fc\n" ); unlink $fc; kill "USR1" => $fc; last; } } goto HANGOUT; sub catchUsr2 { $usr2 = 1; } } 1;