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

George Neill georgen at neillnet.com
Sun Apr 19 20:07:27 PDT 2009


Dan,

2009/4/19 Dan Linder <dan at linder.org>:
> 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?

Well if you are indeed hooked on perl, you could probably something
via async signals ... signal the parent from the child stating you are
done.  (or just make sure you catch the one it already sends when the
child exits).

With a quick google search I found this,
  http://kobesearch.cpan.org/htdocs/Parallel-Fork-BossWorkerAsync/Parallel/Fork/BossWorkerAsync.html

On a non-perl related note ... perhaps you might find the xargs
command useful here.

HTH,
George


More information about the Omaha-pm mailing list