Question

I wonder if we use vfork , how we know child process or parent process , since the resources are shared . to be more specific , assume the following code :

int main()
{
    int pid = vfork();
    if(pid == 0)
    {
       // code for child
     }
    else 
    {
       // code for parent
    }
    return 0;

}

in the code above , if the resources are shared , then pid variable will have uniqe value , so is this code is valid ? , since i have seen examples do things as the above code .

Was it helpful?

Solution

vfork() suspends the parent until the child either calls exec*() or _exit().

using vfork() in this format as we use fork() results in program run in infinite loop. it doesn't end.
read this discussion to get better idea about using vfork().

OTHER TIPS

In vfork the parent will wait the child to finish, so there is no need to differentiate.

is this code is valid ?

Yes. vfork() still will make a copy of parent process (conceptually), and as normal fork(), in the child process it will return 0, in the parent process the pid of that child process.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top