[12][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;
}
答案
This is the son process, a = 2
This is the dad process, a = 0
解析
函数fork(的作用是通过系统调用创建一个与原进程几乎完全相同的进程,对于子进程返回标识符0;对于父进程返回子进程的PID。父进程和子进程拥有各自的局部变量a。初值为1;所以子进程中PID为0,执行的是第1个printf,由a的值为1,先自增再输出值,可知输出的是This is the son process, a = 2;而父进程PID不为0,执行的是第2个printf,由a的值为1,先自减再输出值,可知输出的是This is the dad process, a = 0。故本题答案应选择A选项。
【涉及考点】
第3章 进程线程模型
转载请注明出处。