Translate

Image of Linux Kernel Development (3rd Edition)
Image of Operating System Concepts
Image of Beginning Google Maps API 3
Image of Android Wireless Application Development

Standard Streams Buffering

STDIO (Standard In/Out) streams are either:

  • Fully buffered – the buffer is not flushed until the buffer is full.
  • Line buffered – the buffer is flushed when a complete line is in the buffer or the buffer is full.
  • Unbuffered – the buffer is flushed whenever there is data in the buffer. This is usually at the end of a call to a stdio function such as putc or printf.

By default, stderr is not fully buffered and stdin and stdout are not fully buffered unless the system can determine that the stream is not connected to a tty device file. A tty terminal device is a character device that performs input and output on a character-by-character basis. The communication between terminal devices and the programs that read and write to them is controlled by the tty interface. Examples of tty devices are ASCII terminal and modems.

Adding a n to a printf format string will usually initiate a flush of stdout‘s buffer before the printf returns to the calling program unless the output of the program is redirected to a regular file. You can also manually flush the buffer anytime you want to with a call to fflush. You can use setvbuf to change the buffering mode for a stream to any of the three forms of buffering described above for any open stream.

Comments are closed.