Search code examples
cforkipcvfork

how to know parent or child process in vfork function


I wonder if we use vfork, how do we know the 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 the pid variable will have a unique value, so is this code valid? Since I have seen examples do things as the above code.


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().