Go Back   UNIX Socket FAQ > UNIX Platforms > Processes

Reply
 
Thread Tools Display Modes
  #1  
Old 08-19-2007, 12:03 PM
zhn636 zhn636 is offline
Regular
 
Join Date: Apr 2007
Posts: 146
Post 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?
Reply With Quote
  #2  
Old 08-19-2007, 04:26 PM
RobSeace RobSeace is offline
Administrator
 
Join Date: Jun 2002
Location: Boston, MA
Posts: 3,380
Default

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...
Reply With Quote
  #3  
Old 08-20-2007, 04:51 AM
zhn636 zhn636 is offline
Regular
 
Join Date: Apr 2007
Posts: 146
Post

are there any other methods can avoid the occurence of the zombie process except call wait()?
can you give me some some examples?
Reply With Quote
  #4  
Old 08-20-2007, 10:36 AM
RobSeace RobSeace is offline
Administrator
 
Join Date: Jun 2002
Location: Boston, MA
Posts: 3,380
Default

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...
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:45 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.