stdio buffering considered harmful

gwyn%brl-vld at sri-unix.UUCP gwyn%brl-vld at sri-unix.UUCP
Mon Aug 1 06:12:26 AEST 1983


From:      Doug Gwyn (VLD/VMB) <gwyn at brl-vld>

There are two obvious choices to solve the problem of buffered output
not appearing (i.e. being flushed out) before an input operation:

(1)  Write the program so it fflush()es relevant output before trying
the read (this works with all versions of stdio and puts the decision
where it really belongs, in the programmer's lap);

(2)  Write stdio itself so ALL output streams to terminals (and pipes)
are flushed whenever input is attempted from ANY terminal (or pipe).
There is no reason to handle stdin and stdout only, since that only
helps in some cases but does not solve the general problem, if it is
a problem.

Personally I favor alternative (1).  According to UNIX System V manuals
Bell has adopted alternative (2) for terminals only, not pipes.
Berkeley appears to have adopted (2) for stdout/stdio terminals only.

I think programmers ought to learn to program carefully rather than
rely on library code to make up for their sloppiness.



More information about the Comp.unix.wizards mailing list