单选题
1分
对于如下C语言程序int main(){pid_t pid;int a=1;pid = fork();if(pid==0)printf("This&n...
对于如下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=0
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=0
参考答案: A
参考解析: 【解析】 函数fork()的作用是通过系统调用创建一个与原来进程几乎完全相同的进程。对于子进程返回标识符0;对于父进程返回子进程的PID。父进程和子进程拥有各自的局部变量a,初始值都为1;所以子进程中PID为0,执行的是第1个printf,由a的值为1,先自增再输出值,可知输出的是This is the son process, a=2;而父进程中PID为子进程的PID,不为0,执行的是第2个printf,由a的值为1,先自减再输出值,可知输出的是This is the dad process, a=0。