This is not exactly what you thought originally. The output buffer is not shared - when fork is executed, both processes receive a copy of the same buffer . Thus, after you have developed the fork, both processes ultimately clear the buffer and print the contents to the screen separately.
This only happens because cout is buffered by IO . If you used cerr, which is not buffered, you should see the message only once, pre-fork.
John Humphreys - w00te Feb 20 '12 at 16:18 2012-02-20 16:18
source share