Home  >  Article  >  Backend Development  >  Why does the simple fork() loop produce eight dots instead of two?

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

Susan Sarandon
Susan SarandonOriginal
2024-11-04 02:04:02434browse

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:

<code class="C">#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
    int i;
    for(i = 0; i < 2; i++)
    {
        fork();
        printf(".");
    }
    return 0;
}</code>

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.

The above is the detailed content of Why does the simple fork() loop produce eight dots instead of two?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn