Fork () and exit

I have a simple program:

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

After running the program, my conclusion is: Hello World Hello World . Why is this happening instead of one Hello world ? I assume that the child process is repeating behind the scenes, and the output buffer is shared between processes or something near these lines, but is this a case or something else is happening?

+56
c ++ linux unix fork
Feb 20 '12 at 16:15
source share
8 answers

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.

+91
Feb 20 '12 at 16:18
source share

standard output uses buffered IO. When fork() is called, standard output is not cleared, and buffered content is replicated in the child process. These buffers are cleared when the process exits, resulting in two exits.

If you change the program to:

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

you should see only one.

+43
Feb 20 '12 at 16:18
source share

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

 cout.flush(); fork(); 
+17
Feb 20 '12 at 16:18
source share

The code for outputting "Hello World" is executed only once. The problem is that the output buffer is not flushed. Therefore, 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 will see the result twice.

The easiest way to demonstrate this is to add a new line at the end of the line, which will cause an implicit flash or explicitly hide with std::cout.flush(); . Then you will see only one way out.

+10
Feb 20 '12 at 16:19
source share

If you use:

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

You see only one. I think fork() copies any std::cout output buffer that writes.

+9
Feb 20 '12 at 16:17
source share

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

If you reset cout to fork() , the problem will almost certainly disappear.

+6
Feb 20 '12 at 16:17
source share

The reason is that when calling std::cout<< it really does not execute the output itself, but the data remains in the buffer in the system. When you make a fork, both code and data are copied, as well as all associated buffers. Finally, both father and son wash them off to standard output, and so you see that the result is duplicated.

+3
Feb 20 '12 at 16:19
source share

What you probably see here is the buffering effect. In general, output is buffered until it is explicitly cleared or implicitly executed with an action, such as newline output. Since the output is buffered, both instances of the forked process have buffer output and therefore both display it when the process terminates and the buffer is flushed

+2
Feb 20 '12 at 16:18
source share



All Articles