why does this fork() out put produce 8 instead of 5?

后端 未结 3 820
不思量自难忘°
不思量自难忘° 2020-12-22 14:01

So I have to find the output of this code which is using the fork() method. I thought the output was 5 \"hello\" s but instead I got 8. Why is that? This is the

3条回答
  •  梦毁少年i
    2020-12-22 14:16

    Here's what your code is doing:

    main->doit()->Fork()->Fork()->printf()->return->printf()->exit()
                    |       |
                    |       ----->printf()->return->printf()->exit()
                    |
                    ----->Fork()->printf()->return->printf()->exit()
                            |
                            ----->printf()->return->printf()->exit()
    

    As you can see, you have a total of 8 calls to printf().

    It would have been easier for you to see what was going on if you chose to print different strings in your main and doit functions.

    Setting breakpoint on each printf() call is another effective strategy for figuring out these kinds of problems.

提交回复
热议问题