小弟弄不明白的_exit(0),请高人指点!!!
C++代码
#include<stdlib.h>
#include<unistd.h>
#include<stdio.h>
#include<sys/types.h>
int glob = 5;
int main()
{
int var=10;
pid_t pid;
printf("befork vforkn");
if((pid = vfork()) < 0)
{
printf("errorn");
exit(1);
}
else if(pid == 0)
{
glob++;
var++;
exit(0);
}
printf("pid = %d, glob = %d, var = %d",getpid(), glob,var);
_exit(0);
}
描述:为什么这段代码运行的结果如下:
befork vfork
pid = 9897, glob = 6, var = 11
照理来讲上面的第二行是不输出的,不知到为什么老是输出第二行。
因为用vfork,所以父子进程运行在同一个存储空间,且父进程等子进程运行完子进程执行exit(0),
则glob=6与var=11写入磁盘,当子进程exit(0), 后父进程执行printf,
且printf后没有"\n",之后又执行_exit(0),不进行I/O的刷新为什么还有printf的输出?
有没有高手解答一下,小弟感激不尽!!!!!!!!!
[解决办法]
好像_exit(0)只借宿进程,不做一些收尾工作,而exit(0)还要做一些收尾工作
[解决办法]
楼主看APUE不大细心啊,>>>
Most modern implementations of exit will not bother to close the streams. Because the process is about to exit, the kernel will close all the file descriptors open in the process. Closing them in the library simply adds overhead without any benefit.
所以说,parent还能输出也没什么大不了的。
另外,_exit只是保证不调用使用atexit注册的函数,而刷新或关闭流都是由实现决定的。
[解决办法]
[解决办法]
APUE既然教的是POSIX,在看那本书时,也不妨参考一下Open Group,另外就是看自己系统上的man手册。
下面是Open Group的描述,但是很明显,楼主的系统并不严格遵循。
The exit() function shall then flush all open streams with unwritten buffered data, close all open streams, and remove all files created by tmpfile().
The _Exit() and _exit() functions shall not call functions registered with atexit() nor any registered signal handlers. Whether open streams are flushed or closed, or temporary files are removed is implementation-defined.
[解决办法]