【发布时间】:2021-03-15 00:20:43
【问题描述】:
#include <stdio.h>
#include <unistd.h>
int main()
{
int x = 1;
/* fork off a child process */
if (fork()==0)
x++;
/* fork off another child process */
if (fork()==0)
x++;
printf("x = %d : ", x); fflush(stdout);
/* wait for a signal to be received */
pause();
}
每次运行它都会给出不同的结果。我试过阅读原因,但我很难理解它。这可能的输出是什么?为什么?
【问题讨论】:
-
取决于执行什么进程的顺序
-
@Ed Heal,我见过人们说“我已经实现了 X”,意思是“我已经实现了调用 X 的代码”(“我已经使用了 X”)很多次。我怀疑是误译。是的,这是不正确的,但有更好的方法让人们知道这一点。