http://qs1969.pair.com?node_id=79665


in reply to Have children maintain themselves or main script maintain children.

It depends on exactly the inisialisation the children have to do. If the children have to make a database connection then you'll want to prefork them as in the second option. If the children should just be clones of the parent (ie can start work immediately after forking) then the first would be most appropriate.

That said just in just about any serious application you'll want to do some initialisation in the children so you'll want to prefork them, and to keep them alive for maximum speed.

You should make sure your children don't live forever though so that any memory can be reclaimed and they can go back to sharing all their pages with the parent. Maybe they should die after 100 uses or 60 minutes.

This is exactly how apache works - preforking its children and then they kill themselves after a set time. However apache keeps a dynamic number of children depending on the work load - a minimum number up to a maximum number.

Top tip: Don't use signals in the parent to catch SIGCHLD (as demonstrated in The Perl Cookbook) otherwise you'll miss some of your children dieing, just use wait. Set in non-blocking if you must but you want to try to do the absolute minimum in the parent so it is always alive to spawn more children.