Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

The man page for the stdbuf command states that line-buffered mode is invalid as a standard input buffering option. What is the reason for this?

tail -f access.log | stdbuf -iL cut -d' ' -f1 | uniq

stdbuf: line buffering stdin is meaningless
Try `stdbuf --help' for more information.
share|improve this question
add comment

2 Answers 2

up vote 5 down vote accepted

Simplified, stdbuf is a wrapper around stdio functionality. Line buffering of input streams is undefined in stdio; I can find no standards document that says what it means, so it is literally meaningless as far as the standards go.

Assuming behavior analogous to stdout line buffering, the line buffering of stdin would require calling read() once for each character read, because there is no other way to guarantee that you don't read past a newline on a descriptor. Since the point of buffering is to reduce the number of system calls, it is unsurprising that the stdio library doesn't implement this.

share|improve this answer
add comment

As the author of stdbuf let me direct you to the "stdio input buffering problems" section at http://www.pixelbeat.org/programming/stdio_buffering/

share|improve this answer
    
Thank you Padraig, this clarifies to an extent. Having said that, I understand that the input stream is line-buffered by default when connected to a terminal under Linux. Trying to get my head around why stdbuf would not offer this feature. –  1_CR Jul 25 '12 at 17:51
4  
The input line buffering you're thinking of happens in the kernel, specifically in the tty driver, not in the program running on the terminal, therefore to control it you don't need stdbuf you only need stty. There is no line buffering of input in the C library. Also I think what you really need in your tail|cut|uniq pipeline is line-buffered output on both tail and cut. –  Alan Curry Jul 26 '12 at 4:56
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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