9

So I want to do the following:
Set up a daemon that forks a bunch of processes.

So the Daemon forks a bunch of processes then forks another bunch of processes

the problem is the child processes might take a long time to exit. How do I prevent zombie children if the parent process has other work to do despite forking children?

The parent process (the daemon) does something like this:

while(true)
{
SQL QUERY EXECUTED

   while(mysql_fetch_array)
   {
       Fork children
   }
}

The problem is how can I wait for the children processes to exit if the parent process has to do other work besides forking children and if the children take a long time to exit.

I am using the System daemon PEAR function to create the daemon and the pcntl_fork function to create the processes.

hakre
  • 193,403
  • 52
  • 435
  • 836

3 Answers3

4

I don't remember where I saw this:

Parent forks child
  Waits until child is dead  (this won't take long, see ahead)
  Goes on

Child does only 2 things:
  Forks a grandchild
  Exits

Grandchild does whatever work is needed
  Exits

The trick is that when the Granchild dies, its parent (one of your Children) is already dead. But someone has to be notified for the death. It appears that in Linux systems, it's not the grandparent that is notified but the grand-grand-...-grandparent of all. And because that process knows its job, it periodically checks for dead children and does not allow them to become zombies.

Here's a link with explanation: http://fixunix.com/unix/533215-how-avoid-zombie-processes.html

When a process's parent terminates, the "init" process takes over as its parent. So when the child process exits, the grandchild loses its parent, and is adopted by init. Init always reaps its dead children, so they don't become zombies.

ypercubeᵀᴹ
  • 113,259
  • 19
  • 174
  • 235
2

You should consider having the parent do nothing other than wait for the children. If the parent dies for any reason, then the children will become zombies. If the parent however doesn't do anything, then there are very few chances for it to die unexpectedly.

rid
  • 61,078
  • 31
  • 152
  • 193
  • the problem is that I am dealing with time sensitive information. The daemon parent process checks the database for an event that is to occur at a specific time, forks child processes to handle the event, and then goes back to check another time to see if there are any other events that are supposed to occur at that time. So you see, the parent process needs to keep working. Is there a more efficient way of accomplishing this? –  Nov 10 '11 at 21:12
  • @Pota Onasys, why don't you use processes instead of forks? – rid Nov 10 '11 at 21:13
  • @Pota Onasys, that should work. You wouldn't need to concern yourself with zombies anymore. – rid Nov 10 '11 at 21:48
  • You always need to concern yourself with zombies. – Problematic Nov 10 '11 at 22:48
0

If you explicitly set your SIGCHLD handler to SIG_IGN (instead of SIG_DFL) this will stop your children becoming zombie processes, assuming you're not interested in their exit codes. Alternatively on newer Linuxes you should use sigaction's SA_NOCLDWAIT flag instead.

Neil
  • 54,642
  • 8
  • 60
  • 72