了vfork()的atexit断言失败 [英] vfork() atexit assertion failed
问题描述
我想明白下面这段code的
#包括LT&;&stdio.h中GT;
#包括LT&;&unistd.h中GT;
#包括LT&; SYS / types.h中>诠释的main()
{
将为pid_t PID;
unsigned int类型I = 0;
PID =的vfork();
开关(PID)
{
案例-1://某种错误
看跌期权(叉错误);
打破;
案件0://子进程
而(ⅰ&小于100)
{
的printf(%d个\\ N,I);
我++;
}
打破;
默认://父
而(ⅰ&下; 1000)
{
的printf(%d个\\ N,I);
我++;
}
打破;
}
// _exit(0);
}
和请不要告诉我说了vfork()是坏的,这类事情。我知道它是,但什么是在这个code是导致这种错误发生的准确。
在此先感谢
目前还不清楚是什么你正在尝试做或理解,但这里是从的手动:
的的vfork()函数具有作为叉(2)相同的效果,所不同的是
行为是未定义如果vfork的创建过程中()或者
- 修改不是用来存储vfork的返回值的类型将为pid_t的变量以外的任何数据()
- 从函数返回其中的vfork()被调用
- 调用任何其他函数之前调用成功_exit(2)或某个exec(3)系列函数
块引用>您正在做两个1:
我++
3的printf(%d个\\ NI)
。不管你期望的,它不会工作。作为一个方面说明,
的vfork
也不错。只是棘手的,危险的,几乎是无用和SUSv4删除。I am trying to understand the following piece of code
#include<stdio.h> #include<unistd.h> #include<sys/types.h> int main() { pid_t pid ; unsigned int i=0; pid=vfork(); switch(pid) { case -1: // some sort of error puts("fork error"); break; case 0: // the child process while(i<100) { printf("%d\n", i); i++; } break; default: //parent while(i<1000) { printf("%d\n", i); i++; } break; } // _exit(0); }
And please don't tell me that vfork() is bad and these kind of things .I know it is , but what is happening exactly in this code that is causing this kind of error . Thanks in advance
解决方案It's unclear what you are trying to do or understand, but here is a slightly edited quote from the manual:
The vfork() function has the same effect as fork(2), except that the behavior is undefined if the process created by vfork() either
- modifies any data other than a variable of type pid_t used to store the return value from vfork()
- returns from the function in which vfork() was called
- calls any other function before successfully calling _exit(2) or one of the exec(3) family of functions
You are doing both 1:
i++
and 3printf("%d\n", i)
. Whatever you expect, it won't work.As a side note,
vfork
isn't bad. Just tricky, dangerous, almost useless and removed from SUSv4.这篇关于了vfork()的atexit断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!