Re: Confused with fork & vfork
- From: Joe Pfeiffer <pfeiffer@xxxxxxxxxxx>
- Date: 30 Nov 2007 08:41:43 -0700
"Samrat Kannikar" <samrat.kannikar@xxxxxx> writes:
I have 2 C programs where 1 program uses fork and second uses vfork. The
behaviuor of the program has confused me...
int a; //uninitialized data segment
int main()
{
pid_t pid;
pid = vfork();
if(pid == 0) //child
{
a = 10;
printf("\n Child Process a=%d \n",a);
exit(0);
}
else if(pid > 0) //parent
{
printf("\n Parent Process a=%d \n",a);
exit(0);
}
else
{
printf("\n Could not create child process \n");
}
}
Since vfork is used child executes first and prints 10. But parent process
also prints 10. Since vfork uses COW and child process is changing value of
a should a new page be not allocated to the child process and value of
parent must still be 0?
Some highlights from man vfork
Standard Description
(From SUSv2 / POSIX draft.) The vfork() function has the same effect
as fork(2), except that the behavior is undefined if the process cre-
ated by vfork() either modifies any data other than a variable of type
pid_t used to store the return value from vfork(), or returns from the
function in which vfork() was called, or calls any other function
before successfully calling _exit(2) or one of the exec(3) family of
functions.
Notice -- behavior is undefined in the circumstance you describe.
Another snippet from the man page:
vfork() differs from fork(2) in that the parent is suspended until the
child makes a call to execve(2) or _exit(2). The child shares all mem-
ory with its parent, including the stack, until execve(2) is issued by
the child. The child must not return from the current function or call
exit(3), but may call _exit(2).
So the parent and child share their memory. Where did you find
documentation saying vfork puts pages in COW?
.
- References:
- Confused with fork & vfork
- From: Samrat Kannikar
- Confused with fork & vfork
- Prev by Date: rpcgen cannot find any C Preprocessor
- Next by Date: Re: How Can I call close() for all
- Previous by thread: Confused with fork & vfork
- Next by thread: rpcgen cannot find any C Preprocessor
- Index(es):
Relevant Pages
|
|