Re: Confused with fork & vfork



"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?
.



Relevant Pages

  • Re: Way cool
    ... It segfaults at the end This is because the child returns first rather than _exits: ... All the program does ist vfork() once, after which a parent process and a child process exist, both sharing the same address space until the child disconnects by *directly* _exiting or execing. ...
    (comp.os.linux.development.system)
  • Re: atexit+vfork+exit combination behaves differently on Aix & x-linux
    ... > using atexit) is executed by both parent and child on aix. ... The reason is that it is a really *bad* idea to use vfork(). ... Indeed when you vforka process, both parent and child can *both* ... process fork() and then execimmediately afterwards. ...
    (comp.unix.programmer)
  • Re: Do fork copies the "Code" Memory space?
    ... >> vfork doesn't even copy the page tables. ... do the OS 'rebase' the child? ... > which point the parent process continues. ... > using the same virtual memory address but different physical ...
    (comp.unix.programmer)
  • Re: Re: [9fans] APE fork()
    ... vfork was implemted due to insufficient core. ... fork used to swap the parent to disk and continue with the child process. ...
    (comp.os.plan9)
  • Re: Do fork copies the "Code" Memory space?
    ... >> continues until the child process either exits, or calls `execve', ... >> which point the parent process continues. ... >> and vfork suspend the parent process...and if the child call execve, ... After child does exec or exit ('exit' would be really ...
    (comp.unix.programmer)