语言:C,OS:Linux
代码:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
int main(void)
{
fork();
printf("hello world\n");
fork();
printf("bye\n");
return 0;
}
输出:
hello world
bye
hello world
bye
hello world
bye
hello world
bye
根据this和this,printf() buffers output until a newline is encountered
。
那么为什么我们在这种情况下有4个“你好世界”呢? (而不是2“你好世界”)
编辑:对不起所有,但是像@GregHewgill说的那样,我从一个输出无法直接到终端的环境运行这个程序,当我在我的计算机上再次检查它时,它只是运行正如所料。