vfork() 系统调用的返回值
考虑下面的代码:
int main()
{
int pid;
pid=vfork();
if(pid==0)
printf("child\n");
else
printf("parent\n");
return 0;
}
在 vfork() 的情况下,父进程和子进程使用的地址空间是相同的,因此变量 pid 的单个副本应该存在。现在我无法理解这个 pid 变量如何具有 vfork() 返回的两个值,即子级为零,父级非零?
在 fork() 的情况下,地址空间也会被复制,并且每个子级和父级中有两个 pid 变量的副本,因此我可以理解在这种情况下,两个不同的副本可以具有 < 返回的不同值strong>fork(),但无法理解在vfork()的情况下,pid如何具有vfork()返回的两个值?
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()?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有2本。当您调用
vfork
时,父级会冻结,而子级则执行其操作(直到它调用_exit(2)
或execve(2)
)。因此,在任何时刻,都只有一个pid
变量。顺便说一句,你所做的事情是不安全的。 标准清楚地说明了这一点:
作为第二个旁注,
vfork
已从SUSv4
中删除 - 使用它确实没有意义。There aren't 2 copies. When you cal
vfork
the parent freezes while the child does its thing (until it calls_exit(2)
orexecve(2)
). So at any single moment, there's only a singlepid
variable.As a side note, what you are doing is unsafe. The standard spells it clearly:
As a second side note,
vfork
has been removed fromSUSv4
- there's really no point in using it.