一般快速fork()说明(General Quick fork() explanation)
假设我们有这样的事情:
printf("A"); fork(); printf("B");是输出吗?
1)ABAB 2)ABB
你能解释一下吗?
suppose if we have something like this:
printf("A"); fork(); printf("B");Is the output going to be
1) ABAB 2) ABB
Can you please explain?
最满意答案
正确的答案是它取决于stdout的缓冲模式,其他答案似乎忽略了。
当您使用未刷新的缓冲区进行分叉然后在两个进程中继续使用stdio(而不是在子进程中通常的快速执行或_exit )时,fork时缓冲区中的内容可以打印两次,每次一次处理。
The right answer is that it depends on the buffering mode of stdout, which the other answers seem to be ignoring.
When you fork with unflushed buffers and then continue using stdio in both processes (instead of the usual quick execve or _exit in the child process), the stuff that was in the buffer at the time of the fork can be printed twice, once by each process.
更多推荐
发布评论