Taylor Smyth
Taylor Smyth

Reputation: 1

How to kill forked processes?

Well I'm new to programming Perl (or any language in general) I have basic understandings of the language and have written a small script that runs multiple forked threads of my processes here's a snippet of the script.

use Perl6::Slurp;
use Parallel::ForkManager;

my $pm = Parallel::ForkManager->new(10);
my $time = 100;

alarm("$time");

for my $i (0 .. 100) {
  my $pid = $pm->start and next;
  job();
  $pm->finish;
}
$pm->wait_all_children;

sub job {
  print "Function Started On Thread";
}

Now, that's not my actual code. but its pretty much a summary of what it is without the function I would like it to end when the alarm ends.

Now i don't know if this is a simple action, but as i said im really new to programming in general. Thanks for anyone that helps!

Upvotes: 0

Views: 446

Answers (1)

ikegami
ikegami

Reputation: 386646

Send a signal to the process group. Just add the following to the parent:

local $SIG{ALRM} = {
   local $SIG{TERM} = 'IGNORE';
   kill TERM => -$$;
   die "Timed out\n";
};

Upvotes: 1

Related Questions