Beefy Boxes and Bandwidth Generously Provided by pair Networks
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??


in reply to Re^2: How to make parent wait for all the child processes.
in thread How to make parent wait for all the child processes.

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 ...

thing1(); thing2(); sub thing1 { ... } sub thing2 { ... } sub thing3 { ... } thing3();

... 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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1148603]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-04-24 08:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found