fork - How can I kill forked processes that take too long in my perl script without timing out the forked process? -
i've been using following template of forking/processes needs when comes processing "things" in parallel. loops through need process, x number of entries @ time, , time's out entries take long:
my $num_procs = 0; foreach $entry (@entries) { $num_procs++; if($num_procs == $max_procs) { wait(); $num_procs--; } $pid = fork(); if($pid == 0) { process($entry); } } (; $num_procs>0; $num_procs--) { wait(); }
the "process" routine has following template times out process:
my $timeout_in_seconds = 15; eval { local $sig{alrm} = sub { die "alarm" }; alarm($timeout_in_seconds); # alarm(0); }; if ($@) { # timeout }
i've come across issue no longer works because child unable time out. (i think due i/o blocking issue nfs) way around this, i'm thinking, parent kill -9 child.
is there way modify code this?
whenever alarm
can flaky, use case the poor man's alarm:
my $pid = fork(); if ($pid == 0) { ... # child code exit; } if (fork() == 0) { $time = 15; exec($^x, "-e", "sleep 1,kill(0,$pid)||exit 1..$time;kill -9,$pid"); die; # shouldn't here }
the first fork
fires off child process. second fork
running process kill first process after $time
seconds.
Comments
Post a Comment