For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > April 2007 > Re: when parent process keeps forking child but doesnot wait...









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: when parent process keeps forking child but doesnot wait...
Hubble

2007-04-24, 4:07 am

On 24 Apr., 06:16, djh...@gmail.com wrote:
> In my case, parent receives a request and forks itself and lets the
> child perform a task.
> But parent doesnot wait for child to complete and exit.
> This kind of behavior is normal ,right?
>
> But, when I ps , looks like it displays all those completed and
> exited child processes-->which is bothering me. Do I need to worry
> about it?


Yes. The Zombie Processes (state Z) occupy space in the process table.
The easiest way to solve this is to use double forking:
pid1=fork();
if (pid1==0) { /* son */
pid2=fork();
if (pid2==0) /* son of son */
{ do_work();
_exit(0);
}
/* son */
_exit(0);
}
/* parent, error checking pid<0 */
wait(&status);

Here, parent can wait since its son exists immediately after creating
his own son. Init process takes care of sons son after son has exited.

Hubble.


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com