[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Omaha.pm] Perl, fork, and waitpid()
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)
** *** ***** ******* *********** *************