tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Buffering in standard streams



On Thu, 21 May 2015 12:30:21 +0200
Pierre Pronchery <khorben%defora.org@localhost> wrote:

> I just read this nice article from a coreutils maintainer:
> http://www.pixelbeat.org/programming/stdio_buffering/

In case it affects the discussion, note that

	$ printf "one\ntwo\nthree\n" | ( sed 1q ; sed 1q ; sed 1q )
	one
	As you can see the first sed process reads all the data,
	starving the rest.

is incorrect.  Nothing is "starved"; the second and third sed are never
invoked because the first one terminates the pipeline when it
encounters the first newline.  Furthermore they are not *in* the
pipeline and never see any input.  Cf: 

	$ printf "one\ntwo\nthree\n" | ( sed 1d ; nl )
	two
	three

	$ printf "one\ntwo\nthree\n" | ( sed 1d | sed 1d | nl )
	     1  three

The example using ssh is similarly flawed.  

That said, ISTM Buehler's first suggestion, an environment variable, is
the best option.  

I have occasionally wanted to control stdio buffering, and I'm not
persuaded by Ulrich's argument against having libc honor an environment
variable.  A utility like stdbuf can no more "set up the environment
properly" than can an environment variable, because both rely on the
user's understanding about which file descriptors will be used.  

I would use a syntax like:

	STDBUF=4096:line:1024

to mean

	stdin, 4K
	stdout, line buffered
	stderr, 1K

--jkl


Home | Main Index | Thread Index | Old Index