[Omaha.pm] Perl, fork, and waitpid()

George Neill georgen at neillnet.com
Mon Apr 20 19:16:44 PDT 2009


On Mon, Apr 20, 2009 at 7:42 PM, Jay Hannah <jay at jays.net> wrote:
> On Apr 19, 2009, at 5:42 PM, Dan Linder wrote:
>>
>> I've tried using "waitpid(-1,WNOHANG)" thinking that would return the PID
>> of the last child to die, or "-1" if none had died since last checking, but
>> that seems to just hang _waiting_ for the next death to happen...   I did a
>> "waitpid($pid_of_a_child, 0)" but tha just waits until that specific child
>> dies.
>>
>> Anyone have any example code that can do this?
>
> We use POE for things like this. Specifically, this cookbook recipe:
>
> http://poe.perl.org/?POE_Cookbook/Child_Processes_3
>
> There's a pretty active IRC channel that can help if you get stuck
> (irc.perl.org #poe). I'm sure there's mailing lists too.
>
> I got somebody started from scratch last week and he seemed to take to it
> pretty quickly.

POE looks pretty nifty Jay.

If you want to be lazy (like me), here's a simple/quick xargs example
to demonstrate how one could tackle the problem,

gneill at blackfoot:~$ cat test.input
10
9
8
7
6
5
4
3
2
1

cat test.input | xargs --max-procs=4 --replace=timeout perl -e 'print
"starting timeout\n"; sleep(timeout); print "finished timeout\n";'

running 4 ways parallel,

gneill at blackfoot:~$ time cat test.input | xargs --max-procs=4
--replace=timeout perl -e 'print "starting timeout\n"; sleep(timeout);
print "finished timeout\n";'
starting 10
starting 9
starting 8
starting 7
finished 7
starting 6
finished 8
starting 5
finished 9
starting 4
finished 10
starting 3
finished 3
starting 2
finished 4
starting 1
finished 5
finished 6
finished 1
finished 2

real    0m15.034s
user    0m0.048s
sys     0m0.016s


running serial (aka, no --max-procs)

gneill at blackfoot:~$ time cat test.input | xargs --replace=timeout perl
-e 'print "starting timeout\n"; sleep(timeout); print "finished
timeout\n";'
starting 10
finished 10
starting 9
finished 9
starting 8
finished 8
starting 7
finished 7
starting 6
finished 6
starting 5
finished 5
starting 4
finished 4
starting 3
finished 3
starting 2
finished 2
starting 1
finished 1

real    0m55.087s
user    0m0.036s
sys     0m0.016s

Later,
George


More information about the Omaha-pm mailing list