Having a real problem understanding forking and timeouts. I have a section of code that keeps hanging on bad nodes. I'm trying to kill off the fork if it doesn't complete in a certain amount of time. I think I'm on the right track, but so but not quite there yet. Can some kind brother lay some wisdom on me?
foreach my $node (@nodes) { chomp $node; wait_for_a_kid() if keys %pid_to_node > 4; if (my $pid = fork) { ## parent does... $pid_to_node{$pid} = $node; } else { local $SIG {ALRM} = sub { kill -15, $pid or die "kill: $!"; print "Killed PID $pid\n"}; eval { unless ($pid) { ## child does... setpgrp(0,0); exit !&NODE($node); } alarm 15; 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; 1; }
I ended up working around this problem by implementing a timeout on my LWP::Simple gets in the NODE subroutine which is where I was having the problem. But, I'd still love to understand this whole forking/timing out issue since I seem to encounter it frequently. -----------------------------------------------
Here's is some sample code I came up with using what has been mentioned so far. Everything seems to work here except it's not killing the process if it is > 1 sec old. Seems to be ignoring the alarm completely:
#!/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


In reply to forking and timeouts by Earindil

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.