[22][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; } } 在 UNIX 操作系统中正确编译链接后执行,其运行结果为
This is the son process, a = 2
This is the dad process, a = 0
答案
This is the son process, a = 2
This is the dad process, a = 0
解析
【解析】在 UNIX 操作系统中,fork(函数用于创建一个新进程。当 pid == 0 时,表示当前进程是子进程,子进程会执行++a 操作;当 pid!= 0 时,表示当前进程是父进程,父进程会执行--a 操作,故本题答案选择 A 选项。
【涉及考点】
UNIX 操作系统进程相关概念
转载请注明出处。