73 points · by keyist · 16y ago · 29 comments
Standard Error is unbuffered: http://www.opengroup.org/onlinepubs/009695399/functions/stdi...
"When 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."
$ ./unbuffer ./tst >/tmp/logit 2>&1; cat /tmp/logit
line 1
stderr line 1
line 2
stderr line 2
http://expect.nist.gov/example/unbufferSo, at least in C, I think flushing is intelligent enough not to block if no one's receiving the output.
grep foo *.txt|head -n10
Here head will close its stdin after having read 10 lines. If grep writes more it will get the EPIPE error on write (which may indirectly be called by fflush).This will also send the SIGPIPE signal which by default will kill the process, so grep is automatically terminated when no more output will be read from it.
You write a program called "thefirstprogram". Who cares what it does. However, a user is pushing its (standard) output into a second program called "asecondprogram". Now imagine "asecondprogram" has melted, but not terminated. It's basically in a screwed up state.
So the run command was:
bash-2.04> thefirstprogram | asecondprogram
I believe the flushing scheme advocated in the article is to be considered harmful, as you will in fact never see an error message if asecondprogram gets in a bad state and stops reading from stdin (and the buffers fill up).
I do agree the flushing makes for nicer ordering of error messages, but is more fragile.
Only used flushed output when you really need flushed output otherwise you'll get less resilience in the case of meltdown of cooperating programs.
If the second program is messed up, it makes no difference if you flush or not. You'll still never see an error message.
Stdin will fill up, flush or not, the program will block, and then sit there - no error message.
You could maybe detect if stdin is blocked and say something on stderr - but if you want to do that it makes no difference whatsoever if you flush stdout or not.
I believe pipes are block-buffered and basically look just like redirecting to a file.
Anyway, if you want to see freakin' echo block because of this, try:
$ mkfifo foo
$ echo "OH HAI" > foo
<blocks>
In another xterm: $ cat foo
<echo unblocks and exits>.So yes, you are wrong here.
Could someone explain exactly why this is?
(Note that only stdout is the issue, see ready's comment.)
If you did a setbuf(stderr,NULL) as well as setbuf(stdout,NULL), you'd achieve the same effect that the author's going for, with no need to remember to flush.