问题 问答题

下列程序执行时,系统的输出可能是什么

a=55;
pid=fork();
if (pid==0)
sleep(5);
a=99;
sleep(5);
printf("child leaving\n");
exit(0);

else
sleep(7);
printf("a=%d\n",a);
wait(0);
printf("parent child exited\n");

答案

参考答案:a=55
child leaving
Darent child exited执行fork()以后,父进程(else部分)先打印(睡眠7秒),然后等待子进程结束。
子进程睡眠10秒后打印“child leaving”,然后父进程打印最后一行。注意子进程对a进行赋值不影响进程中的变量a,因为它们分属不同的进程。

单项选择题
选择题