следите за руками:
-------------------controller.pl---------------
#!/usr/bin/perl -wuse strict;
use POSIX 'WNOHANG';
my $child;
my $need_start = 1;
# если умирает потомок рестартуем его
sub kill_child {
my $stiff;
while (($stiff = waitpid(-1,WNOHANG))>0) {
if($stiff == $child) {
print "Killed child $child\n";
$need_start = 1;
} else {
print "Killed unknown child $stiff\n";
}
}
}
sub start_child {
my $name = shift;
$child=fork();
if (!$child) {
print "Я потомок\n";
exec($name);
}
}
$|=1;
$SIG{CHLD} = 'kill_child';
while (1) {
sleep 1;
print "Я родитель\n";
if($need_start) {
$need_start = 0;
start_child("./worker.pl");
}
}
---------------------end----------------------------------------
--------------------worker.pl-----------------------------------
#!/usr/bin/perl -w
use strict;
my $quit = 0;
sub sig_worker {
my $sig = shift;
$quit++;
print "I get signal = $sig\n";
return;
}
$SIG{TERM} = $SIG{INT} = \&sig_worker;
print "---------------- Start Worker Programm------------------\n";
while(!$quit){
print "I work, don't wory!!!\n";
sleep 1;
}
#Определим причину по которой завершилась программа
if($quit) {
print "Exit by quit = $quit\n";
}
print "---------------- End Worker Programm---------------------\n";
exit(0);
---------------------end----------------------------------------