What do fully buffered, line buffered and unbuffered mean in C? [closed]
I came across a line that output from cat
command is fully buffered. What does it mean?
Online C11 standard, 7.21.3/3:
When a stream is unbuffered, characters are intended to appear from the source or at the destination as soon as possible. Otherwise characters may be accumulated and transmitted to or from the host environment as a block. When a stream is fully buffered, characters are intended to be transmitted to or from the host environment as a block when a buffer is filled. When a stream is line buffered, characters are intended to be transmitted to or from the host environment as a block when a new-line character is encountered. Furthermore, characters are intended to be transmitted as a block to the host environment when a buffer is filled, when input is requested on an unbuffered stream, or when input is requested on a line buffered stream that requires the transmission of characters from the host environment. Support for these characteristics is implementation-defined, and may be affected via the
setbuf
andsetvbuf
functions.
7.21.3/7:
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.
[I'm using Perl in the examples for conciseness and ease of reproduction, but the concepts I'm illustrating are not specific to Perl. C works in the same manner.]
Buffering dictates how often stuff written to a file handle is flushed (sent) to the OS. Compare the behaviour of the following two commands:
# With buffering (default)
perl -e'$|=0; print "a"; sleep(2); print "b\n";'
# Without buffering
perl -e'$|=1; print "a"; sleep(2); print "b\n";'
Normally, the buffer is only flushed when it becomes full. Line-buffered output is also flushed when a newline is encountered. Compare:
perl -e'print "a"; sleep(2); print "b\n";'
perl -e'print "a\n"; sleep(2); print "b\n";'
Most programs use block-buffering. However, they usually switch to line-buffering for stdout when it's connected to a terminal. Compare:
# Perl's STDOUT is line-buffered when connected to a terminal.
perl -e'print "a\n"; sleep(2); print "b\n";'
# Perl's STDOUT is fully buffered when connected to a pipe.
perl -e'print "a\n"; sleep(2); print "b\n";' | cat
# unbuffer uses pseudo-ttys to fool a program into thinking it's connected to a terminal.
unbuffer perl -e'print "a\n"; sleep(2); print "b\n";' | cat
Unbuffered output writes bytes from the input file to standard output without delay as each is read.
Full buffered output first reads the full file in sequence as it read it and stores it in buffer and display the buffer one's its done..
With -u option you can avoid it.