The recursive algorithm could be improved so that it will skip over a node (pid) that is explored earlier.
Right now it rescans the entire proc table on each recursive iteration.
Below you will find my revisions to help improve the code run time.
I ran many tests on this new algorithm and the run time has gone down substantially.
use vars qw(%gprocs %pprocs);
sub killfam {
my ($signal, @pids) = @_;
if ($ppt_OK) {
my $pt = Proc::ProcessTable->new;
foreach my $row (@{$pt->table}) {
push @{$pprocs{$row->ppid}}, $row->pid;
}
my @kids = get_pids(@pids);
@pids = (@pids, @kids);
}
kill $signal, @pids;
}
sub get_pids {
my @kids = @_;
my @pids;
foreach my $kid (@kids) {
if (defined $pprocs{$kid}) {
my @child = @{$pprocs{$kid}};
my @rchild = ();
foreach my $p (@child) {
unless (exists $gprocs{$p}) {
push @rchild, $p;
$gprocs{$p} = undef;
}
}
if (scalar @rchild) {
push @pids, @rchild, get_pids(@rchild);
}
}
}
@pids;
}
Thanks,
-=Steve