Have a monitoring app written in perl that forks off a bunch of sub processes. Sometimes, these sub processes hang on bad nodes and won't die off. Been trying to use sig alrm to kill them but I just can't seem to get this right. Forking still confuses me a bit, so guessing I'm just not quite getting it here. Would appreciate any input.
Here is some sample code. Every fork should get killed off due to the sleep but isn't.
A bit of the code here was taken from examples I found, some of it probably isn't necessary.
#!/bin/perl
for ($i=0; $i<=10; $i++) {
wait_for_a_kid() if keys %pid_to_node > 3;
$pid = fork;
if ($pid) {
## parent does...
$pid_to_node{$pid} = $i;
}
else {
print "$i $$\n";
local $SIG {ALRM} = sub {
kill -15, $$ or die "kill: $!";
print "\tKilled PID $$\n"}; # Just SIGTERM
eval {
## child does...
setpgrp(0,0);
exit !&Test;
alarm 1;
waitpid $pid => 0;
};
}
}
## final reap:
1 while wait_for_a_kid();
sub wait_for_a_kid {
my $pid = wait;
return 0 if $pid < 0;
my $node = delete $pid_to_node{$pid} or warn("Why did I see $p
+id ($?)\n"), next;
}
sub Test {
sleep 10;
}
OUTPUT:
0 1668
1 1669
2 1670
3 1671
Why did I see 1668 (0)
5 1811
6 1812
7 1813
8 1814
9 1920
10 1921
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.