|
|
#include "global.h"
#include "err.h"
int glob=6;
int main()
{
int var;
pid_t pid;
var = 88;
printf("before vfork\n");
if((pid=vfork())<0)
err_sys("vfork error");
else if(pid==0){
glob++;
var++;
exit(0);//_exit(0);
}
/*parent*/
printf("pid =%d,glob=%d, var=%d\n",getpid(),glob,var);
exit(0);
}
其中global.h 主要是一些头文件,err.h是书后给出的错误处理函数。
引用原书中的话,如果调用_exit(0)应该输出如下
$before vfork
pid =5422,glob=7, var=89
如果调用exit(0)输出如下
$before vfork
可是在我的机子上运行的结果,linux/2.6.8/Debian
但是书中说应该的运行结果是(调用exit())
$before vfork
pid =5422,glob=7, var=89
书中解释说:
the child calls exit, which flushes and close all the standard I/O stream. This includes the standard output. Even though this is done by the child, it's done in the parent's address space, so all the standard I/O FILE objects that are modified are modified in the parent. When the parent calls printf later, standard output has been closed, and printf return -1.
可是为什么在我的机子上运行结果都一样呢?哪位兄弟能给个解释!多谢! |
|