Thanks for the pointers to POE and xargs. Unfortunately I'm restricted to using Perl core modules (and probably back to Perl 5.001 from 1995).<br><br>DaveB wrote:<br>> I would think "wait" (rather than "waitpid") should fit the bill, as
"wait" <br>> should return the PID of whatever child exited most recently,
right?<br><br>*slaps forehead* <br><br>So, after a few minutes of re-reading the wait() manpage and some hacking, here's a script that demonstrates what I was looking for:<br>##### begin #####<br>#!/usr/bin/perl<br>use strict;<br>
my @array = qw(AA BB CC DD EE FF GG);<br>my $sleep = 10;<br>my %children;<br><br>for my $A (0..scalar(@array)-1) {<br> my $pid = fork();<br> if ($pid) {<br> # parent<br> $children{$pid}=@array[$A];<br>
} elsif ($pid == 0) {<br> # child<br> my $X = $sleep*rand();<br> my $now = localtime();<br> printf "$now Executing %s for %5.3f seconds.\n",@array[$A],$X;<br>
sleep $X;<br> exit(0);<br> } else {<br> die "couldn't fork: $!\n";<br> }<br>}<br><br>my $exited;<br>while (($exited = wait()) && ($exited > 0 )) {<br>
my $now = localtime();<br> printf "$now EXITED: $exited(%s)", $children{$exited};<br> delete $children{$exited};<br> if (scalar %children > 0 ) {<br> printf ", waiting for";<br>
foreach my $B (sort keys(%children)) {<br> printf ": %5i(%s) ",$B, $children{$B};<br> }<br> }<br> printf "\n";<br>}<br>##### end #####<br>
<br>And here's some output (YMMV due to the rand() call):<br>$ perl A0006.pl<br>Mon Apr 20 22:36:25 2009 Executing AA for 0.356 seconds.<br>Mon Apr 20 22:36:25 2009 Executing BB for 9.797 seconds.<br>Mon Apr 20 22:36:25 2009 Executing CC for 4.411 seconds.<br>
Mon Apr 20 22:36:25 2009 Executing DD for 7.816 seconds.<br>Mon Apr 20 22:36:25 2009 Executing EE for 5.170 seconds.<br>Mon Apr 20 22:36:25 2009 Executing FF for 8.632 seconds.<br>Mon Apr 20 22:36:25 2009 Executing GG for 6.502 seconds.<br>
Mon Apr 20 22:36:25 2009 EXITED: 12343(AA), waiting for: 12344(BB) : 12345(CC) : 12346(DD) : 12347(EE) : 12348(FF) : 12349(GG)<br>Mon Apr 20 22:36:29 2009 EXITED: 12345(CC), waiting for: 12344(BB) : 12346(DD) : 12347(EE) : 12348(FF) : 12349(GG)<br>
Mon Apr 20 22:36:30 2009 EXITED: 12347(EE), waiting for: 12344(BB) : 12346(DD) : 12348(FF) : 12349(GG)<br>Mon Apr 20 22:36:31 2009 EXITED: 12349(GG), waiting for: 12344(BB) : 12346(DD) : 12348(FF)<br>Mon Apr 20 22:36:32 2009 EXITED: 12346(DD), waiting for: 12344(BB) : 12348(FF)<br>
Mon Apr 20 22:36:33 2009 EXITED: 12348(FF), waiting for: 12344(BB)<br>Mon Apr 20 22:36:34 2009 EXITED: 12344(BB)<br><br>Thanks for the kick in the pants to re-read the wait() function. :-)<br><br>Dan<br><br clear="all">"Quis custodiet ipsos custodes?" (Who can watch the watchmen?) -- from the Satires of Juvenal<br>
"I do not fear computers, I fear the lack of them." -- Isaac Asimov (Author)<br>** *** ***** ******* *********** *************<br>
<br><br><div class="gmail_quote">On Mon, Apr 20, 2009 at 21:16, George Neill <span dir="ltr"><<a href="mailto:georgen@neillnet.com">georgen@neillnet.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="im">On Mon, Apr 20, 2009 at 7:42 PM, Jay Hannah <<a href="mailto:jay@jays.net">jay@jays.net</a>> wrote:<br>
> On Apr 19, 2009, at 5:42 PM, Dan Linder wrote:<br>
>><br>
>> I've tried using "waitpid(-1,WNOHANG)" thinking that would return the PID<br>
>> of the last child to die, or "-1" if none had died since last checking, but<br>
>> that seems to just hang _waiting_ for the next death to happen... I did a<br>
>> "waitpid($pid_of_a_child, 0)" but tha just waits until that specific child<br>
>> dies.<br>
>><br>
>> Anyone have any example code that can do this?<br>
><br>
> We use POE for things like this. Specifically, this cookbook recipe:<br>
><br>
> <a href="http://poe.perl.org/?POE_Cookbook/Child_Processes_3" target="_blank">http://poe.perl.org/?POE_Cookbook/Child_Processes_3</a><br>
><br>
> There's a pretty active IRC channel that can help if you get stuck<br>
> (<a href="http://irc.perl.org" target="_blank">irc.perl.org</a> #poe). I'm sure there's mailing lists too.<br>
><br>
> I got somebody started from scratch last week and he seemed to take to it<br>
> pretty quickly.<br>
<br>
</div>POE looks pretty nifty Jay.<br>
<br>
If you want to be lazy (like me), here's a simple/quick xargs example<br>
to demonstrate how one could tackle the problem,<br>
<br>
gneill@blackfoot:~$ cat test.input<br>
10<br>
9<br>
8<br>
7<br>
6<br>
5<br>
4<br>
3<br>
2<br>
1<br>
<br>
cat test.input | xargs --max-procs=4 --replace=timeout perl -e 'print<br>
"starting timeout\n"; sleep(timeout); print "finished timeout\n";'<br>
<br>
running 4 ways parallel,<br>
<br>
gneill@blackfoot:~$ time cat test.input | xargs --max-procs=4<br>
--replace=timeout perl -e 'print "starting timeout\n"; sleep(timeout);<br>
print "finished timeout\n";'<br>
starting 10<br>
starting 9<br>
starting 8<br>
starting 7<br>
finished 7<br>
starting 6<br>
finished 8<br>
starting 5<br>
finished 9<br>
starting 4<br>
finished 10<br>
starting 3<br>
finished 3<br>
starting 2<br>
finished 4<br>
starting 1<br>
finished 5<br>
finished 6<br>
finished 1<br>
finished 2<br>
<br>
real 0m15.034s<br>
user 0m0.048s<br>
sys 0m0.016s<br>
<br>
<br>
running serial (aka, no --max-procs)<br>
<br>
gneill@blackfoot:~$ time cat test.input | xargs --replace=timeout perl<br>
-e 'print "starting timeout\n"; sleep(timeout); print "finished<br>
timeout\n";'<br>
starting 10<br>
finished 10<br>
starting 9<br>
finished 9<br>
starting 8<br>
finished 8<br>
starting 7<br>
finished 7<br>
starting 6<br>
finished 6<br>
starting 5<br>
finished 5<br>
starting 4<br>
finished 4<br>
starting 3<br>
finished 3<br>
starting 2<br>
finished 2<br>
starting 1<br>
finished 1<br>
<br>
real 0m55.087s<br>
user 0m0.036s<br>
sys 0m0.016s<br>
<br>
Later,<br>
<font color="#888888">George<br>
</font><div><div></div><div class="h5">_______________________________________________<br>
Omaha-pm mailing list<br>
<a href="mailto:Omaha-pm@pm.org">Omaha-pm@pm.org</a><br>
<a href="http://mail.pm.org/mailman/listinfo/omaha-pm" target="_blank">http://mail.pm.org/mailman/listinfo/omaha-pm</a></div></div></blockquote></div><br>