vfork()问题

qq_28839381 2015-08-16 03:20:46
#include "stdio.h"
#include <unistd.h>

int main(int argc, char const *argv[])
{
pid_t result = vfork();

if(result == 0)
{

}
else if(result > 0)
{
printf("123\n");
}
else printf("vfork failed\n");

return 0;
}

运行结果

请问结果为什么会这样?
...全文
95 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
nswcfd 2015-08-17
  • 打赏
  • 举报
回复
vfork() differs from fork(2) in that the parent is suspended until the child terminates (either normally, by call- ing _exit(2), or abnormally, after delivery of a fatal signal), or it makes a call to execve(2). Until that point, the child shares all memory with its parent, including the stack. The child must not return from the current func- tion or call exit(3), but may call _exit(2). 由于子进程没有调用execve或者_exit,最终会进入return,这跟最后一句child不能return相违背(因为fork下父子共享stack),其结果是破坏了parent的stack(?有待确认?),导致parent的行为异常。
nswcfd 2015-08-17
  • 打赏
  • 举报
回复
不知道怎么去解释,vfork期待着子进程立刻调用exec的。

18,772

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 专题技术讨论区
社区管理员
  • 专题技术讨论区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧