Welcome to the Monastery | |
PerlMonks |
Re^3: How to make parent wait for all the child processes.by Apero (Scribe) |
on Nov 25, 2015 at 13:23 UTC ( [id://1148603]=note: print w/replies, xml ) | Need Help?? |
Your StartServer() subroutine suffers from the same problem your original code did in that it never exits when it finishes in your child process. This means you get a second process that continues executing after you invoke that routine. The parent process runs the for loop with my code from above, waits for the 3 children it starts to finish, and stops. However, you still have the child you fork()-ed before you called that StartServer() routine. The child just executes code from that point, including the later forks that you seem to only want in your parent. I suspect you want the child codepath in your modified code above mine to call exit() after its work is done. This is why the example I gave you does this inside the conditional testing the PID (and actually calls it from the child processing routine, so the main-level code is really just a backup in case someone mistakenly calls return() from it.) You might also consider putting your main code above your subroutines. It's a bit hard to read when your code looks like this ...
... since one has to read the entire program to figure out if there's more code or just more subroutines. It would be better in my short example to put thing3() above all the subroutines.
In Section
Seekers of Perl Wisdom
|
|