[真2][9][单选]对于如下 C 语言程序
int main() { pid_t pid; int a = 15; 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 = 16 This is the dad process, a = 14
This is the son process, a = 16
This is the dad process, a = 14
This is the dad process, a = 16 This is the son process, a = 14
答案
This is the son process, a = 16 This is the dad process, a = 14
解析
计算机程序设计中的 fork(函数的返回值:若成功调用一次则返回两个值,子进程返回 0,父进程返回子进程 ID;否则,出错返回 1。a 初始值为 15,调用 fork(函数后,第一次返回子进程的值为 0,所以输出 This is the son process, a = 16,第二次返回父进程的值,所以输出 This is the dad process, a = 14。故选择 A 选项。
转载请注明出处。