On 2011-07-20 14:34 +0100, Ralph Corderoy wrote:
> No. Here's my understanding of how it currently works. The built-in
> printf works out a character to print at a time; no foresight needed or
> used. It asks the C library to print it with putchar(3). The C library
> can implement buffering to avoid too many expensive write(2) system
> calls. bash has the C library buffer stdout by lines. Each time
> putchar(3) is called the character is appended to the buffer. If the
> character was '\n' or the buffer is now full then write(2) is called to
> pass the buffer to the kernel and the buffer then treated as empty.
>
> When printf is finished the buffer is flushed, e.g. if there's anything
> in it then write(2) is called.
>
> The C library provides two other modes of buffering, see setvbuf(3).
> bash could easily detect that stdout is a regular file, have a new
> stream be duplicated from file descriptor 1, and set it to
> block-buffered.
If standard output is a log file, log entries could remain
latent for a very long time.
The buffering mode we really want is buffered with a forced
flush at reasonable intervals, E.G. one second after the last
write. Unfortunately, there's no such thing in stdio.
--
André Majorel http://www.teaser.fr/~amajorel/