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

Dan Linder dan at linder.org
Sun Apr 19 15:42:31 PDT 2009


I'm trying to speed up some processing in a script.  Currently, the script
has a set of subroutines it runs through in sequence, some take only a
second to run, others can run for minutes.  Meanwhile the multi-core system
is only using one CPU and the others are idle.

What I'd like to do is have the parent process fork a number of children
processes and wait for one to complete, then start another child to work on
the next remaining subroutine.  I've got the forking routine down and I can
get multiple children running at one time, but waiting for any one of them
to complete has me stumped.

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?

Thanks,

Dan

"Quis custodiet ipsos custodes?" (Who can watch the watchmen?) -- from the
Satires of Juvenal
"I do not fear computers, I fear the lack of them." -- Isaac Asimov (Author)
** *** ***** ******* *********** *************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/omaha-pm/attachments/20090419/ab909228/attachment.html>


More information about the Omaha-pm mailing list