Considering the below code :
int main()
{
int pid;
pid=vfork();
if(pid==0)
printf("child\n");
else
printf("parent\n");
return 0;
}
In case of vfork() the adress space used by parent process and child process is same, so single copy of variable pid should be there. Now i cant understand how this pid variable can have two values returned by vfork() i.e. zero for child and non zero for parent ?
In case of fork() the adress space also gets copied and there are two copy of pid variable in each child and parent, so I can understand in this case two different copies can have different values returned by fork() but can't understand in case of vfork() how pid have two values returned by vfork()?
exec()
or_exit()
in thevfork()
'd child, but that's just about it. Pretty much anything else leads to undefined behaviour. Recommendation: don't usevfork()
; indeed, I wouldn't even bother to learn how to use it safely. It's likegets()
, a function that it is best to pretend doesn't exist. – Jonathan Lefflervfork()
is quite useful for MMU-less systems. – ninjaljvfork()
is useful for spawning processes from Java (where else can you find such large address space wastage?), and indeed Java 1.7 seems to have added support forvfork()
andposix_spawn()
. – ninjalj