Confused with fork & vfork



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?

When i replace vfork by fork the value of a in parent is 0 & in child is 10.
Im confused with this. Plz tell me what is happening?

Regards,
Samrat



.



Relevant Pages

  • Re: Embedded linux: With or without MMU
    ... The basic difference between the two is that when a new process is ... created with vfork(), the parent process is temporarily ... This strange state of affairs continues until the child process ...
    (comp.os.linux.embedded)
  • question about zombie process, deamon, and double fork
    ... always call wait on a child process if it knows that the child process ... Can it also be said if parent process exits before ... /* daemon() - detach process from user and disappear into the ... int daemon ...
    (comp.unix.programmer)
  • Re: unix fork question
    ... Write a program that forks a child process into the background. ... The parent ... int main ... Jimmie Barnett ...
    (comp.unix.programmer)
  • Re: Capture von Java ausgabe innerhalb eines c Progs
    ... int pipes; ... {printf; return 0; ... exit; // child process ... nChildReturnCode); ...
    (de.comp.os.unix.programming)
  • Trouble with ptrace self-attach rule since kernel > 2.6.14
    ... My multithreaded application tries to write callstacks of all threads (some ... above mentioned problem - and terminating the child process just after this ... int main ...
    (Linux-Kernel)