Sorry; forgot the following 2 'Use' Stmts for the listed code...
use Errno qw(EAGAIN);
use POSIX ":sys_wait_h";
Also, perhaps a simpler test script would be....
1) Create a file named fork_sleeper.pl with the following code in
it...
use Time::HiRes qw[time];
sleep 3;
print "Time(".time().") Child: Done Sleeping\n";
exit 0;
2) Use the following code in the same dir as the fork_sleeper.pl file
(NOTE: TimeHiRes is not required, but you won't get much
differentiation in your time stamps if you don't use it... See output
below the code....... Uncomment the exec() or the backticks line of
code to test...
use Errno qw(EAGAIN);
use POSIX ":sys_wait_h";
use Time::HiRes qw[time];
$sleeper = (split /(.*)\\.*?$/,$0)[1] . "\\fork_sleep.pl";
my $i;
for ($i = 0; $i<10; $i++ ) {
my $pid;
print "$i Time(".time().") Parent: Forking\n";
FORK: {
if ($pid = fork()) { next; }
elsif ( defined $pid ) {
print "$i Time(".time().") Child: About to sleep...\n";
# `$sleeper`; ##### USE THIS LINE TO TEST WITH
BACKTICKS....
# exec("$sleeper"); ##### USE THIS LINE TO TEST WITH EXEC().....
exit;
}
elsif ( $! == EAGAIN ) {
print "$i Time(".time().") Parent: Recoverable Error '$?'
'$!'\tHarvest orphaned children and retry fork....\n";
sleep 5;
wait();
print "$i Time(".time().") Parent: Redoing Fork\n";
redo FORK;
}
else { die "Time(".time().") Unrecoverable Fork Error: $!" }
}
}
exit 0;
Output with BackTicks....
0 Time(1185503292.1093

Parent: Forking
1 Time(1185503292.14443) Parent: Forking
2 Time(1185503292.17593) Parent: Forking
3 Time(1185503292.21377) Parent: Forking
0 Time(1185503292.2512) Child: About to sleep...
Terminating on signal SIGINT(2)
That was the last line... it never went further so I hit CTRL-C to
kill it....
Output using exec()....
0 Time(1185503510.3125) Parent: Forking
1 Time(1185503510.36191) Parent: Forking
2 Time(1185503510.39841) Parent: Forking
3 Time(1185503510.43181) Parent: Forking
0 Time(1185503510.45847) Child: About to sleep...
1 Time(1185503510.46419) Child: About to sleep...
2 Time(1185503510.46862) Child: About to sleep...
4 Time(1185503510.4979) Parent: Forking
5 Time(1185503510.531) Parent: Forking
6 Time(1185503510.56713) Parent: Forking
5 Time(1185503510.58752) Child: About to sleep...
4 Time(1185503510.5922

Child: About to sleep...
3 Time(1185503510.59585) Child: About to sleep...
7 Time(1185503510.62381) Parent: Forking
8 Time(1185503510.65922) Parent: Forking
9 Time(1185503510.70196) Parent: Forking
6 Time(1185503510.73913) Child: About to sleep...
7 Time(1185503510.74363) Child: About to sleep...
8 Time(1185503510.74823) Child: About to sleep...
9 Time(1185503510.79715) Child: About to sleep...
Time(1185503518.28125) Child: Done Sleeping
Time(1185503518.32813) Child: Done Sleeping
Time(1185503518.375) Child: Done Sleeping
Time(1185503518.4218

Child: Done Sleeping
Time(1185503518.45313) Child: Done Sleeping
Time(1185503518.51563) Child: Done Sleeping
Time(1185503518.5468

Child: Done Sleeping
Time(1185503518.625) Child: Done Sleeping
Time(1185503518.64063) Child: Done Sleeping
Time(1185503518.6718

Child: Done Sleeping