0

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:

https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_05

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?

2

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:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.