Why does stdout need explicit flushing when redirected to file?

Flushing for stdout is determined by its buffering behaviour. The buffering can be set to three modes: _IOFBF (full buffering: waits until fflush() if possible), _IOLBF (line buffering: newline triggers automatic flush), and _IONBF (direct write always used). “Support for these characteristics is implementation-defined, and may be affected via the setbuf() and setvbuf() functions.” [C99:7.19.3.3]

“At program startup, three text streams are predefined and need not be opened explicitly
— standard input (for reading conventional input), standard output (for writing
conventional output), and standard error (for writing diagnostic output). As initially
opened, the standard error stream is not fully buffered; the standard input and standard
output streams are fully buffered if and only if the stream can be determined not to refer
to an interactive device.” [C99:7.19.3.7]

Explanation of observed behaviour

So, what happens is that the implementation does something platform-specific to decide whether stdout is going to be line-buffered. In most libc implementations, this test is done when the stream is first used.

  1. Behaviour #1 is easily explained: when the stream is for an interactive device, it is line-buffered, and the printf() is flushed automatically.
  2. Case #2 is also now expected: when we redirect to a file, the stream is fully buffered and will not be flushed except with fflush(), unless you write gobloads of data to it.
  3. Finally, we understand case #3 too for implementations that only perform the check on the underlying fd once. Because we forced stdout’s buffer to be initialised in the first printf(), stdout acquired the line-buffered mode. When we swap out the fd to go to file, it’s still line-buffered, so the data is flushed automatically.

Some actual implementations

Each libc has latitude in how it interprets these requirements, since C99 doesn’t specify what an “interactive device” is, nor does POSIX’s stdio entry extend this (beyond requiring stderr to be open for reading).

  1. Glibc. See filedoalloc.c:L111. Here we use stat() to test if the fd is a tty, and set the buffering mode accordingly. (This is called from fileops.c.) stdout initially has a null buffer, and it’s allocated on the first use of the stream based on the characteristics of fd 1.

  2. BSD libc. Very similar, but much cleaner code to follow! See this line in makebuf.c

Leave a Comment