What part of the system sets up the buffering of the three standard streams when a program is started?

Is this part of linux, or glibc, or maybe bash? Does POSIX define the behaviour, or is it part of C?

Posix has some answers:


At program start-up, three 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). When 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.

So if the system can determine that streams are NOT interactive, they CAN be fully buffered (except stderr), but in practice what part of the system does this determining?

  • Some similar questions that don't quite touch the right points:
    – Evan Benn
    Commented Apr 23, 2019 at 0:58
  • 2
    Start here Commented Apr 23, 2019 at 1:01
  • is your question specifically about the stdio buffering? Then please add the stdio tag. Notice that many programs don't do any userland buffering (eg. cat), and other programs do their own, different from and bypassing stdio (eg. PerlIO in perl).
    – user313992
    Commented Apr 23, 2019 at 3:14
  • It would be completely different for different types of file. The kernel do have buffer mechanism, but userspace usually have option to open that file with kernel buffer disabled and do its own userspace buffering Commented Apr 23, 2019 at 3:17

1 Answer 1


Your programming language

This behaviour is an artefact of the C runtime library, and a requirement of the C programming language. Other programming languages have historically been built on top of the C runtime library, and gain this behaviour from it. This is true for C++ programs, for example. Chapter and verse of the C and C++ language standards is quoted fairly often over on Stack Overflow (q.v.).

Most notably, programs written in Python have the same behaviour, and are asked about fairly frequently, sometimes with the blame for the behaviour of the programming language runtime being wildly mis-placed.

Tools to change this behaviour of programs which use the default language semantics (without modifying and recompiling the programs) come in two forms: language-dependent (and sometimes runtime-library-specific) tools that insert themselves into the runtime and change the buffering, and tools that make standard I/O into files that the runtime libraries decide to be interactive devices. Tools in the latter class are language-independent and include Bernstein ptybandage.

Further reading

Just a few instances of the buffering question:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .