Stdio buffer flushing
Doug Gwyn
gwyn at smoke.BRL.MIL
Sun Jul 8 15:14:17 AEST 1990
In article <1990Jul5.220009.3724 at chinet.chi.il.us> les at chinet.chi.il.us (Leslie Mikesell) writes:
>(void)vfprintf(logfile, fmt, ap);
>putc('\n', logfile); /* log messages don't come with \n */
>(void)fflush(logfile);
>Apparently the putc is performing a flush *before* adding the \n, so
>that a concurrent process can write it's entry first. Is this a
>bug or are you not supposed to mix different forms of buffered output?
All stdio I/O operations are defined in terms of getc() and putc(),
so clearly "mixing forms" of output is not an issue. However, there
is no particular guarantee that an output operation will not cause
an actual write() at some point, even if you have setbuf()ed and have
not yet invoked fflush(). Certainly when the buffer is full it will
be automatically flushed. While I'm not sure why your implementation
of vfprintf() (or less likely, putc()) would flush the buffer where
you say it did, there is no rule that prohibits it from doing so.
The C standard does not require that no extra buffer flushing occur,
although it does suggest that it is not intended.
More information about the Comp.unix.questions
mailing list