Earindil has asked for the wisdom of the Perl Monks concerning the following question:
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. -----------------------------------------------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; }
#!/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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: forking and timeouts
by moritz (Cardinal) on Aug 09, 2007 at 12:31 UTC | |
by Earindil (Beadle) on Aug 09, 2007 at 13:49 UTC | |
by Crackers2 (Parson) on Aug 09, 2007 at 15:02 UTC | |
by Earindil (Beadle) on Aug 09, 2007 at 16:10 UTC | |
|
Re: forking and timeouts
by ww (Archbishop) on Aug 09, 2007 at 12:47 UTC | |
by almut (Canon) on Aug 09, 2007 at 13:14 UTC | |
by ww (Archbishop) on Aug 09, 2007 at 13:30 UTC |