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