[net.unix-wizards] sri-unix.2330: STDIO on TTYs: Clarification and Repeat

trt (07/31/82)

THESIS
The UNIX V7 Stdio is often derided as a machine gobbling waste
because it does not buffer output to terminals.
But my experience has been that V7 Stdio is a *person* gobbling waste
because it "cleverly" buffers when it should not.

MOTIVATION
Buffering can lead to all sorts of unpleasant surprises.
Here are a few V7 examples.  (4.xbsd may have similar or other problems.)
All of these (and others) happened at Duke, causing *many* lost hours.
1. There is a nifty game (or DBMS, editor, etc.) on your system.
You want to write a program which interacts with it,
so your program sets up a pipe(II) to and from the game
and then writes commands to the game, picking up the output.
Your program hangs for no apparent reason.
Uh oh, the game uses Stdio so it buffers to pipes?
Too bad, when you finally figure out what is wrong
you can try to get the game source and then hack it for a few hours.
2.  You are working on a combinatorial problem.
It prints solutions for n=1,2,... every 1,4,8,16, ... seconds.
You check it out for a few n, then "a.out > results &" and go home.
Next morning the program is still churning, but the file is empty!
Uh oh, your program uses Stdio so it buffers to files?
Too bad, when you finally figure out what went wrong
you can hack the program and try again tomorrow night.
3.  Your program works fine on a tty,
but the output is lost or even duplicated when stdout is redirected?
Well, maybe you are foolishly using exec(II), or fork(II),
or one of the other system calls that foul up buffering.

THE IDEAL
Suppose single character IO were almost as fast as multi-character IO.
What default buffering strategy should Stdio use?
My take is for all input files to be buffered (as Stdio does now),
and for *all* output files to be *unbuffered*.
This scheme provides uniform behavior independent of IO redirection
and directly follows the IO requests of the programmer.
The behavior is easily predicted, debugging is simplified,
the problem examples above are avoided, things are hunky dory.

APPROXIMATION TO THE IDEAL
For about three years Duke has run a modified V7 Stdio:
(1)  Input files are buffered, as before.
(2)  Stdout and stderr are *unbuffered by default*.
(3)  As a compromise, fopen-ed output files are buffered, as before.
The rare unpleasant surprise has been due to the compromise (3).
For efficiency printf and fprintf are "internally buffered".
Since most programmers use those routines for output
and most output is to terminals we have experienced
a large efficiency *benefit* relative to vanilla V7 Stdio.
More info on Duke's Stdio and UNIX buffering in general is available.