UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

#1 2007-08-19 01:03 PM

zhn636
Member
Registered: 2007-04-24
Posts: 146

Re: zombie state

can you explain to me why I call fork twice can avoid zombie state?
  and I can call fork twice which child process I can avoid to become zombie ?the first or the second child process?

Offline

#2 2007-08-19 05:26 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: zombie state

The reason to fork() twice is so that the grandchild becomes an orphan (since its
parent, the child of the original process, will exit)...  Therefore, the original parent
process no longer has any relation to the running grandchild, so it won't get stuck
needing to reap it when it dies...  Instead, the grandchild becomes an adopted child
of "init" (process 1) when its parent (the first child of the original parent) exits, and
so init becomes responsible for reaping it (which it will) and preventing it from
becoming a zombie...

Offline

#3 2007-08-20 05:51 AM

zhn636
Member
Registered: 2007-04-24
Posts: 146

Re: zombie state

are there any other methods can avoid the occurence of the zombie process except call  wait()?
  can you give me some some examples?

Offline

#4 2007-08-20 11:36 AM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,847
Website

Re: zombie state

On some systems, simply ignoring SIGCHLD will prevent zombies, as well...  That's
not portable behavior, though...  The most common method of preventing zombies
is probably a simple waitpid() reaper installed as a SIGCHLD handler...  Or, the
double-fork() to abandon the grandchild to init, as you mentioned...

Offline

Board footer

Powered by FluxBB