[31][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); else printf("This is the dad process, a=%d\n", ++a); }
在 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
解析
上述 C 语言程序中 fork 函数调用后返回两个值,父进程返回子进程标记,子进程返回 0。对于子进程,a 的值不变,对于父进程,a 的值也不变。故运行结果为 This is the son process, a = 5 和 This is the dad process, a = 5。选项中没有该结果,可能原题目有误,按照常规思路分析应选 A 选项。涉及考点为第 2 章进程管理。
转载请注明出处。