Does NEWLINE always flush stdio buffer?

Doug Gwyn gwyn at smoke.BRL.MIL
Sun Jul 2 18:16:03 AEST 1989


In article <1989Jul2.040259.3872 at utzoo.uucp> henry at utzoo.uucp (Henry Spencer) writes:
>It might be reasonable to default to line buffering for pipes, ...

Line buffering has an additional flaw -- it requires that almost every
character being output be tested against '\n'.

As I recall, the original STDIO implementations did not treat "terminal
devices" (i.e. those responding to tty-oriented ioctls) specially.  Line
buffering appears to have been added later for two main reasons:
	(1) to guarantee that prompts etc. were seen before requesting
	    input from a terminal
	(2) unbuffered output was painfully slow

As has already been noted in this discussion, applications can solve both
problems by using full buffering and issuing an fflush() before an input
request.  That's actually what I prefer, and is essentially how my portable
"Dx" IPC package (part of a large project currently under development)
expects to be used.  My belief is that line buffering is an attempt to
automate a solution to a certain class of application design problems,
without taking into account the possibility you brought up or other,
similar difficulties with automating the determination of when to flush
and when not to flush output buffers.  As seems to always happen, the
attempt to make system software "smart" fails because it takes genuine
intelligence to really solve the problem right, and automated algorithms
following fixed rules cannot properly deal with the entire spectrum of
possibilities.

The proposed ANSI Standard for C suggests (sort of a soft requirement)
what are essentially the current UNIX System V STDIO rules for line-buffered
streams: not only should the output buffer be flushed when a new-line is
encountered or the buffer fills up, but also ALL line-buffered output streams
should be flushed whenever input is requested from an unbuffered stream or
when line-buffered input has to refill its buffer.  The cost of implementing
this can be substantial!  Since it is only a "soft" requirement, I wouldn't
mind at all if implementations claimed that "interactive devices" never
exist, thereby obtaining full buffering by default except for stderr.
Portable applications need to issue explicit fflush()es at the appropriate
times anyway, so we might as well be using full buffering all the time.



More information about the Comp.lang.c mailing list