Considering the below code :
int main()
{
int pid;
pid=vfork();
if(pid==0)
printf(\"child\\n\");
else
printf(\"parent\\n\");
return 0;
There aren't 2 copies. When you cal vfork
the parent freezes while the child does its thing (until it calls _exit(2)
or execve(2)
). So at any single moment, there's only a single pid
variable.
As a side note, what you are doing is unsafe. The standard spells it clearly:
The vfork() function shall be equivalent to fork(), except that the behavior is undefined if the process created 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() or one of the exec family of functions.
As a second side note, vfork
has been removed from SUSv4
- there's really no point in using it.