fork() and output

C++LinuxUnixFork

C++ Problem Overview


I have a simple program:

int main()
{
    std::cout << " Hello World";
    fork();
}

After the program executes my output is: Hello World Hello World. Why does this happen instead of a single Hello world? I'm guessing that the child process is rerun behind the scenes and the output buffer is shared between the processes or something along those lines, but is that the case or is something else happening?

C++ Solutions


Solution 1 - C++

This isn't quite what you thought originally. The output buffer is not shared - when you execute the fork, both processes get a copy of the same buffer. So, after you fork, both processes eventually flush the buffer and print the contents to screen separately.

This only happens because cout is buffered IO. If you used cerr, which is not buffered, you should only see the message one time, pre-fork.

Solution 2 - C++

standard output uses buffered IO. When the fork() is called the standard output is not flushed and the buffered content is replicated in the child process. These buffers are flushed when the process exit, resulting in the two outputs that you see.

If you change the program to:

std::cout << " Hello World;" << std::endl;

you should see only one.

Solution 3 - C++

Because you called fork() without flushing all buffers first.

cout.flush();
fork();

Solution 4 - C++

The code to output "Hello World" is only executed once. The issue is that the output buffer isn't flushed. So when you fork the process, "Hello World" is still sitting in the output buffer. When both programs exit, their output buffers will be flushed and you'll see the output twice.

The easiest way to demonstrate this is by adding a newline at the end of your string, which will cause an implicit flush, or explicitly flush with std::cout.flush();. Then you'll only see the output once.

Solution 5 - C++

If you use:

std::cout << " Hello World" << std::flush;

You only see one. I guess fork() copies whatever output buffer std::cout writes to.

Solution 6 - C++

The string is not immediately written to the screen; instead, it's written to an internal buffer. The child process inherits a copy of the output buffer, so when the child's cout is automatically flushed, Hello World is printed to the screen. The parent also prints Hello World.

If you flush cout before the fork(), the problem will almost certainly go away.

Solution 7 - C++

The reason is that the when you invoke std::cout<< it doesn't really perform the output itself but data is left in a buffer in the system. When you do the fork, both code and data are copied, as well as all the buffers associated. Finally, both father and son flush them to the standard output and thus you see the output duplicated.

Solution 8 - C++

What you're likely seeing here is an effect of buffering. In general output is buffered until it's explicitly flushed or implicitly done with an action like outputting a new line. Because the output is buffered both copies of the forked process have bufferred output and hence both display it upon the process terminating and flushing the buffer

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEumcozView Question on Stackoverflow
Solution 1 - C++John HumphreysView Answer on Stackoverflow
Solution 2 - C++hmjdView Answer on Stackoverflow
Solution 3 - C++JoshuaView Answer on Stackoverflow
Solution 4 - C++Michael MiorView Answer on Stackoverflow
Solution 5 - C++James McLaughlinView Answer on Stackoverflow
Solution 6 - C++NPEView Answer on Stackoverflow
Solution 7 - C++GenísView Answer on Stackoverflow
Solution 8 - C++JaredParView Answer on Stackoverflow