2007-4-19 20:14
zhguowen
关于unix环境高级编程一个例题的困惑
#include <stdlib.h>
#include <sys pes.h>
int glob=6;
int main(void)
{
pid_t pid;
int var;
var=88;
printf("before vfork\n");
if((pid=vfork())<0)
printf("vfork error!");
else if (pid==0)
{
glob++;
var++;
/* printf("child process\n");
printf("glob = %d, var =%d \n",glob,var); */
_exit(0);
}
/*parent */
printf("parent process, pid=%d,glob = %d, var =%d \n",getpid(),glob,var);
exit(0);
}
上面是unix环境高级编程第八章的第二个题目.
我正在学习unix环境高级编程,其中第八章中第2个程序,我的测试结果和书上介绍的并不相同 ,在调用了vfork()以后,子进程修改了glob and var的值,在子进程调用_exit()退出后,父进程输 出glob and var值,发现glob and var并没有被子进程修改,按照书上的介绍子进程应该修改glob and var的值。 请问为何?我的编程环境是AIX5.2,编译器 xlc 7.0
并且子进程退出时,_exit(0)和exit(0)两种方式对程序的输出结果没有任何影响, unix环境高级编程是十几年前的书了,在AIX下的学习编程,看这本书合适吗?
望高手回答!
2007-4-22 17:13
oraix
现在版本的vfork是copy父进程的地址空间和堆栈, 所以应该是没有修改父进程的glob和var的值
没试过, 不知LZ的结果是什么. 其实简单的学习方法就是安装全man, 然后man vfork:P
=====
The fork subroutine creates a new process. The new process (child process) is an almost exact copy of the calling process (parent process). The child process inherits the attributes from the parent process.
========
The vfork subroutine is supported as a compatibility interface for older Berkeley Software Distribution (BSD) system programs and can be used by compiling with the Berkeley Compatibility Library (libbsd.a).
In the Version 4 of the operating system, the parent process does not have to wait until the child either exits or executes, as it does in BSD systems. The child process is given a new address space, as in the fork subroutine.[color=#ff0000] The child process does not share any parent address space.[/color]
该书是有点老了,不过思想还是一样的,注意一下新函数库的区别就好了
[[i] 本帖最后由 oraix 于 2007-4-22 17:16 编辑 [/i]]