#!/usr/bin/perl -w
use strict;
my $nd = NDTest->new;
$nd->Bind;
warn "My PID is $$\n";
foreach my $i (1..10)
{
if (!fork())
{
# Child
sleep(1);
exit($i);
}
sleep(2);
}
package NDTest;
sub new {
my $class = shift;
my $self = {};
$self->{mode} = 'fork';
bless $self,$class;
}
sub SigChildHandler {
my $self = shift; my $ref = shift;
return undef if $self->{'mode'} ne 'fork'; # Don't care for childs.
# return 'IGNORE' if $^O eq 'linux'; # We get zombies on Linux otherwise
my $reaper;
sub {
warn "Running reaper.\n";
$$ref = wait;
$SIG{'CHLD'} = $reaper;
};
}
sub Bind ($) {
my $self = shift;
my $fh;
my $child_pid;
my $reaper = $self->SigChildHandler(\$child_pid);
$SIG{'CHLD'} = $reaper if $reaper;
}
####
[sgifford@sghome pa1]$ perl /tmp/t107
My PID is 18286
Running reaper.
Use of uninitialized value in scalar assignment at /tmp/t107 line 37.
####
$ ps -ef |grep 18286
sgifford 18286 16547 0 14:02 pts/0 00:00:00 perl /tmp/t107
sgifford 18288 18286 0 14:02 pts/0 00:00:00 [perl ]
sgifford 18289 18286 0 14:02 pts/0 00:00:00 [perl ]
sgifford 18292 18286 0 14:02 pts/0 00:00:00 [perl ]
sgifford 18295 18286 0 14:02 pts/0 00:00:00 [perl ]
sgifford 18298 18286 0 14:02 pts/0 00:00:00 [perl ]
sgifford 18301 18286 0 14:02 pts/0 00:00:00 [perl ]
sgifford 18303 18212 0 14:03 pts/20 00:00:00 grep 18286
####
#!/usr/bin/perl -w
use strict;
my $nd = NDTest->new;
$nd->Bind;
warn "My PID is $$\n";
foreach my $i (1..10)
{
if (!fork())
{
# Child
sleep(1);
exit($i);
}
sleep(2);
}
package NDTest;
sub new {
my $class = shift;
my $self = {};
$self->{mode} = 'fork';
bless $self,$class;
}
sub SigChildHandler {
my $self = shift; my $ref = shift;
return undef if $self->{'mode'} ne 'fork'; # Don't care for childs.
# return 'IGNORE' if $^O eq 'linux'; # We get zombies on Linux otherwise
my $reaper;
$reaper =
sub {
warn "Running reaper.\n";
$$ref = wait;
$SIG{'CHLD'} = $reaper;
};
}
sub Bind ($) {
my $self = shift;
my $fh;
my $child_pid;
my $reaper = $self->SigChildHandler(\$child_pid);
$SIG{'CHLD'} = $reaper if $reaper;
}
####
$ perl /tmp/t107
My PID is 18322
Running reaper.
Running reaper.
Running reaper.
Running reaper.
Running reaper.
Running reaper.
Running reaper.
Running reaper.
Running reaper.
Running reaper.