Need to know how fork works?

前端 未结 7 1784
[愿得一人]
[愿得一人] 2020-12-15 13:32

I am trying the following C code:

int main()
{
    printf(\"text1\\n\");
    fork();
    printf(\"text2\\n\");
    return 0;
}

I was expect

7条回答
  •  有刺的猬
    2020-12-15 13:58

    The forked process gets a copy of the variable memory, and at the time of the fork the output buffer has yet to be flushed. No output has been written to the console when you fork, only buffered up. Both processes thus continues with text1 already in the buffer, and thus both print it.

提交回复
热议问题