Reputation: 1323
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.
Upvotes: 1
Views: 738
Reputation: 3162
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()
.
Upvotes: 2
Reputation: 15121
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.
Upvotes: 0
Reputation: 1824
In vfork the parent will wait the child to finish, so there is no need to differentiate.
Upvotes: 0