fork twice to prevent zombie process
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > fork twice to prevent zombie process




  Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    fork twice to prevent zombie process  
baumann.Pan@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-31-07 06:29 AM

Hi,

I read the Unix programming Faq, there are some statements on how to
prevent zombie process,

1.6.2 How do I prevent them from occuring?
------------------------------------------

Another approach is to `fork()' *twice*, and have the immediate child
process exit straight away. This causes the grandchild process to be
orphaned, so the init process is responsible for cleaning it up. For
code
to do this, see the function `fork2()' in the examples section.


anyone can tell me why fork twice is needed?

thanks






[ Post a follow-up to this message ]



    Re: fork twice to prevent zombie process  
Paul Pluzhnikov


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-31-07 06:29 AM

baumann.Pan@gmail.com writes:

> anyone can tell me why fork twice is needed?

Parent fork()s child C1 and immediately wait()s for it.
Child C1 immediately fork()s child C2 and exit()s.
Parent P collects C1 exit status and continues doing whatever it
needs to do.

Now C1 can't become a zombie -- it has exited and has been waited
for by P.

C2 can't become a zombie either -- it's parent (C1) has exited,
so C2 is an orphan.

Orphans on UNIX get inherited by process 1 (init).
Process 1 is constantly waiting for any childred that might exit
(that's one of its jobs). When C2 exits, its exit status gets
collected by init -- C2 doesn't become zombie.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.





[ Post a follow-up to this message ]



    Re: fork twice to prevent zombie process  
baumann@pan


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-31-07 06:29 AM

On Jan 31, 10:34 am, Paul Pluzhnikov <ppluzhnikov-...@charter.net>
wrote:
> baumann....@gmail.com writes: 
>
> Parent fork()s child C1 and immediately wait()s for it.
> Child C1 immediately fork()s child C2 and exit()s.
> Parent P collects C1 exit status and continues doing whatever it
> needs to do.
>
> Now C1 can't become a zombie -- it has exited and has been waited
> for by P.
>
> C2 can't become a zombie either -- it's parent (C1) has exited,
> so C2 is an orphan.
>
> Orphans on UNIX get inherited by process 1 (init).
> Process 1 is constantly waiting for any childred that might exit
> (that's one of its jobs). When C2 exits, its exit status gets
> collected by init -- C2 doesn't become zombie.
>
> Cheers,
> --
> In order to understand recursion you must first understand recursion.
> Remove /-nsp/ for email.


How about if parent immediately exits after child C1 forks(let the C1
be the orphan, parent process are not planing to do anything else)?






[ Post a follow-up to this message ]



    Re: fork twice to prevent zombie process  
Paul Pluzhnikov


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-31-07 06:29 AM

"baumann@pan" <baumann.Pan@gmail.com> writes:

> How about if parent immediately exits after child C1 forks(let the C1
> be the orphan, parent process are not planing to do anything else)?

If the parent doesn't need to do anything else, then it should simply
execute the code that child would have executed after fork.

If for some reason a child is still desired, then fork()ing once
and having the parent exit is sufficient to prevent the child from
becoming a zombie.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 12:15 AM.      Post New Thread    Post A Reply      
  Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register