For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > January 2005 > the need for double forking?









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 the need for double forking?
neoTheCat

2005-01-22, 3:58 am

hello.

i am very on the double-forking on why it avoids zombie
children. i ran the following code:


int
main()
{ pid_t pid;

pid=fork();

if (pid == 0)
{ printf("child go to sleep now\n");
sleep(20);
printf("child end now\n");
}
else if (pid == -1)
{ printf("Error in forking\n");
}
else
{ printf("parent go to sleep now\n");
sleep(10);
printf("parent end now\n");

}


and when the parent dies, the child is orphaned and is adopted by init.
i can see why the zombie happens when the child dies before the parent
and no one catches the SIGCHLD. but why is the double fork nescessary?
having the child adopted by init seems fine with a single fork...
thanks,
-- neoTheCat

Barry Margolin

2005-01-22, 3:58 am

In article <1106363371.342656.325390@z14g2000cwz.googlegroups.com>,
"neoTheCat" <aglist@metashadow.com> wrote:

> hello.
>
> i am very on the double-forking on why it avoids zombie
> children. i ran the following code:
>
>
> int
> main()
> { pid_t pid;
>
> pid=fork();
>
> if (pid == 0)
> { printf("child go to sleep now\n");
> sleep(20);
> printf("child end now\n");
> }
> else if (pid == -1)
> { printf("Error in forking\n");
> }
> else
> { printf("parent go to sleep now\n");
> sleep(10);
> printf("parent end now\n");
>
> }
>
>
> and when the parent dies, the child is orphaned and is adopted by init.
> i can see why the zombie happens when the child dies before the parent
> and no one catches the SIGCHLD. but why is the double fork nescessary?
> having the child adopted by init seems fine with a single fork...
> thanks,


The double fork is necessary if the original process *doesn't* exit
right away. The original process can just call wait() to wait for the
first child to exit. This way, it doesn't have to deal with catching
SIGCHLD and calling wait() later on when the child that does the real
work finishes.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
neoTheCat

2005-01-22, 3:58 am

thank you so much!!!! you just ended many hours of fruitless searching.
everything i read made it sound like it was *necessary* to double to
create an orphaned child, and i couldn't understand why. i that i was
misunderstanding something.

thank you,
-- neoTheCat

Barry Margolin

2005-01-22, 3:57 pm

In article <1106364779.315052.93500@z14g2000cwz.googlegroups.com>,
"neoTheCat" <aglist@metashadow.com> wrote:

> thank you so much!!!! you just ended many hours of fruitless searching.
> everything i read made it sound like it was *necessary* to double to
> create an orphaned child, and i couldn't understand why. i that i was
> misunderstanding something.


You're welcome. Now just learn how to use Google Groups properly.
Please use the option to quote relevant material in your replies, to
maintain context. Most Usenet readers don't see newsgroups in the
forum-style that Google presents, we just see the new messages.

--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
Sponsored Links







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

Copyright 2008 codecomments.com