M
Michele Dondi
I hope that the subject is not too imprecise and that this is not too
OT (I am aware that part of the issue is OS-specific rather than
Perlish)...
I was experimenting with fork(), signals, and other process management
functions in perl. I am reproducing a minimal example here:
#!/usr/bin/perl -l
use strict;
use warnings;
sub busy ();
sub rest();
sleep 5;
busy;
sub busy () { # Let the fun begin!! ;-)
my $forked=0;
my @family;
for (1..7) {
defined (my $pid=fork) or
warn "Couldn't fork: $!\n";
$forked *= 2;
if ($pid) {
push @family, $pid;
} else {
@family=();
$forked++;
}
}
local $SIG{TERM} = sub {
kill 15, @family;
waitpid $_, 0 for @family;
die "[$forked:$$] Stopping now: ",
"signaled (@family)\n";
};
while (1) {
next if $forked;
kill 15, @family;
goto &rest;
}
}
sub rest () {
sleep 10 while 1;
}
__END__
It should (i) fork() up to 127 copies of itself, (ii) kill them and
then go to rest.
Now, *generally* it works, *but* (i) it still leaves around exactly
seven <defunct> childs; (ii) what's worst, *in some cases*
(appearently at random), it leaves a small but variable number of
active childs too.
Michele
OT (I am aware that part of the issue is OS-specific rather than
Perlish)...
I was experimenting with fork(), signals, and other process management
functions in perl. I am reproducing a minimal example here:
#!/usr/bin/perl -l
use strict;
use warnings;
sub busy ();
sub rest();
sleep 5;
busy;
sub busy () { # Let the fun begin!! ;-)
my $forked=0;
my @family;
for (1..7) {
defined (my $pid=fork) or
warn "Couldn't fork: $!\n";
$forked *= 2;
if ($pid) {
push @family, $pid;
} else {
@family=();
$forked++;
}
}
local $SIG{TERM} = sub {
kill 15, @family;
waitpid $_, 0 for @family;
die "[$forked:$$] Stopping now: ",
"signaled (@family)\n";
};
while (1) {
next if $forked;
kill 15, @family;
goto &rest;
}
}
sub rest () {
sleep 10 while 1;
}
__END__
It should (i) fork() up to 127 copies of itself, (ii) kill them and
then go to rest.
Now, *generally* it works, *but* (i) it still leaves around exactly
seven <defunct> childs; (ii) what's worst, *in some cases*
(appearently at random), it leaves a small but variable number of
active childs too.
Michele