"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Why does the simple fork() loop produce eight dots instead of two?

Why does the simple fork() loop produce eight dots instead of two?

Published on 2024-11-12
Browse:354

Why does the simple fork() loop produce eight dots instead of two?

Curious Case of Unexpected Branching in fork()

Consider the following code snippet that employs the fork() primitive to create child processes:

#include 
#include 
#include 

int main(void)
{
    int i;
    for(i = 0; i 

Surprisingly, executing this program yields eight output dots, six more than you might initially anticipate. Why does this occur?

Unveiling the Hidden Processes

To unravel this mystery, let's delve into the operation of fork(). fork() creates a replica of the current process, resulting in a parent and a child process.

Initially, there's a single process, which forks into two. Both of these processes incrementally execute the for loop, printing a dot each time. On the second iteration, each process forks again, creating a total of four processes. These four processes print a dot before terminating.

Buffered Output and Delayed Appearance

However, printf() buffers its output, meaning it accumulates multiple prints before sending them out at once. When all four processes print their second dots, they get buffered. This is where the sneaky effect arises.

Upon fork(), the buffered dots are inherited by the child processes. So, when each child process exits, its buffered dot appears on the output stream. Adding these four delayed dots to the four dots printed incrementally accounts for the unexpected total of eight.

Avoiding Buffered Dots

To bypass this buffered behavior, it's advisable to call fflush(stdout); after each printf() statement. This forces the output to be sent immediately, ensuring that the expected number of dots is displayed.

Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3