当前位置: 技术问答>linux和unix
vfork的使用
来源: 互联网 发布时间:2017-04-11
本文导语: #include #include int main(void) { pid_t pid; int count=0; pid=vfork(); count++; printf("%dn",count); return 0; } 运行结果: [root@cfc process]# ./vfork 1 1 总线错误 我的期望结果是: 1 2 why? | 把return 0改成_exit(0) vfork...
#include
#include
int main(void)
{
pid_t pid;
int count=0;
pid=vfork();
count++;
printf("%dn",count);
return 0;
}
运行结果:
[root@cfc process]# ./vfork
1
1
总线错误
我的期望结果是:
1
2
why?
|
把return 0改成_exit(0)
vfork是父进程先阻塞,让子进程是在父进程地址空间中运行, 子进程与父进程的栈空间是共享的
子进程结束后return执行清栈操作会导致父进程执行时栈出现异常从而crash
参见man:
vfork() differs from fork(2) in that the calling thread is suspended until the child terminates (either normally, by calling _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 function or call exit(3), but may call _exit(2).
vfork是父进程先阻塞,让子进程是在父进程地址空间中运行, 子进程与父进程的栈空间是共享的
子进程结束后return执行清栈操作会导致父进程执行时栈出现异常从而crash
参见man:
vfork() differs from fork(2) in that the calling thread is suspended until the child terminates (either normally, by calling _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 function or call exit(3), but may call _exit(2).