What is a buffered, buffered, and unbuffered average in C?

I came across a line whose output from the command is catfully buffered. What does it mean?

0
source share
3 answers

Online Standard C11 , 7.21.3 / 3:

When a stream is unbuffered, characters should appear from both the source and destination as soon as possible. Otherwise, characters may be accumulated and transmitted to or from the host environment as a block. When the stream is fully buffered, the characters are intended to be sent to or from the host environment as a block when the buffer is full. When a stream is buffered by line, the characters are intended to be sent to or from the host environment as a block when a newline character is encountered. In addition, characters are intended to be sent as a block to the host environment when the buffer is full, when input is requested in an unbuffered stream, or when a request is requested in a buffered stream that requires transmission of characters from the host environment. Support for these characteristics is determined by the implementation and may be affected by using functionssetbufand setvbuf.

7.21.3 / 7:

When the program starts, three text streams are predefined and they obviously do not need to be opened - standard input (for reading ordinary input), standard output (for writing normal output) and standard error (for recording diagnostic output). As originally discovered, the standard error stream is not fully buffered; standard input and standard output streams are fully buffered if and only if a stream can be defined so as not to refer to an interactive device.

+2
source

[I use Perl in the examples for brevity and ease of reproduction, but the concepts I illustrate are not related to Perl. C works the same way.]

, , , () . :

# 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";'

. . :

perl -e'print "a"; sleep(2); print "b\n";'

perl -e'print "a\n"; sleep(2); print "b\n";'

. , stdout . :

# Perl STDOUT is line-buffered when connected to a terminal.
perl -e'print "a\n"; sleep(2); print "b\n";'

# Perl 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 connected to a terminal.
unbuffer perl -e'print "a\n"; sleep(2); print "b\n";' | cat
+2

, .

, , , .

-u .

+1

Source: https://habr.com/ru/post/1689841/


All Articles