[23][9][单选]对于如下 C 语言程序:
int main( pid_t pid; int a = 1; pid = fork(; if (pid == 0 printf("This is the son process, a=%d\n", a; else printf("This is the dad process, a=%d\n", a + 2; 在 UNIX 操作系统中正确编译链接执行后,其运行结果为:
This is the son process, a=0
This is the dad process, a=2
答案
This is the son process, a=0
This is the dad process, a=2
解析
在 UNIX 类操作系统中,父进程通过调用 fork(函数创建子进程,子进程获得与父进程地址空间相同的一份拷贝,包括文本、数据和堆栈段,以及用户栈。fork(函数被调用一次,返回两次:一次是在父进程中,一次是在新创建的子进程中。所以程序中的变量 a,在父进程和子进程中存在两份,初始值都是 1。程序运行后子进程输出 a=0,父进程输出 a=2。
转载请注明出处。