RE: [ILUG] Children will not die, zombies are roaming my system

From: Kenn Humborg (kenn at domain bluetree.ie)
Date: Thu 27 Jul 2000 - 14:15:44 IST


> Working on a bit of code here to spawn a separate server for each net
> connection. My problem is that when the child processes finish
> (and exit())
> they are still left lying about the system as zombie processes.
> The only way
> to get rid of them is to kill the parent. I can't rightly see what the
> problem is but I'm sure it's very simple. Anybody have any suggestions ?

A zombie process is a process which has exited, but is
waiting for the parent to 'reap' its exit status. If you
are interested in the exit status, you must use waitpid()
or friends to get it.

If you are not, you can get init to do it for you. When the
parent exits before the child, the child obviously has no-one
to report its exit status to. This is called an orphan.
What the kernel does in this case is make PID 1 (init) the
parent. init is always doing a 'wait' when it's not doing
anything else, so it ends up reaping the orphan when it
exits, and no zombie.

Do achieve this, you 'double-fork'. This is the pseudo-code:

   pid = fork()
   if (child) {
      pid = fork()
      if (child) {
         /* exec() or do_work() or whatever */
      } else {
         /* In first child, exit to orphan second child */
         exit (0);
      }
   } else {
      /* in parent, reap first child */
      waitpid(pid);
   }

init will reap child 2 and child 1 gets reaped very quickly
by the parent.

Later,
Kenn



This archive was generated by hypermail 2.1.6 : Thu 06 Feb 2003 - 13:07:00 GMT