Need to know how fork works?

前端 未结 7 1881
[愿得一人]
[愿得一人] 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 14:12

    Problem 1 : the output as
          text1
          text2
          text2

    This is because fork() create exact copy (child) of parent process and both processes start their execution right after the system call fork().

    Problem 2 : the output as
          text1text2 
          text1text2 

    This is all about buffering. Refer this link and learn about fork() basics. http://www.csl.mtu.edu/cs4411.ck/www/NOTES/process/fork/create.html

提交回复
热议问题