Less Buffered I/O

The Green Hills ANSI C library includes a Standard I/O Package, abbreviated stdio.

stdio includes formatted I/O using printf and scanf, character I/O using getc and putc, plus other features.

In traditional implementations, all I/O performed in stdio is buffered automatically.

In embedded programming there is a constant trade-off between space and performance. Buffering improves performance by increasing the average number of characters written per system call. However, buffering occupies space for the code to manage the buffers, as well as for buffers themselves.

If I/O is totally unbuffered, every character read or written requires a system call. A benefit of this mode is that I/O is never delayed until the buffer is full or lost in a buffer if the application exits abnormally.

If I/O is buffered in the traditional manner, several kilobytes of code are added to the application, because the stdio package invokes malloc and various other routines to manage the buffering.

The Less Buffered I/O mode in which the Green Hills ANSI C library is built provides a reasonable compromise between full buffering and unbuffered I/O.

Rather than allocate a permanent buffer for each file, which is used as long as the file is open, Less Buffered I/O performs buffering within each of the following routines:

fwrite

fputs

puts

printf

fprintf

sprintf

vprints

vfprintf

vsprintf

Thus, during a single call, any characters, which are written to one of these functions, will be buffered. In this way, one function call will often require only one output system call. Once the function completes, all characters are written to the file. No characters are ever left in a buffer after the function call, eliminating the risk that output will be lost, and eliminating the need to flush buffers when a file is closed or the program exits.

No input routines are buffered in any way by the Less Buffered I/O method. Files are not closed upon program termination, except as noted below.

The program may enable full buffering of either input or output by calling either setbuf() or setvbuf(). In this case, characters are only written to a buffered file when the buffer is full, or fflush() or fclose() is called. Upon normal termination of the program, if setbuf() or setvbuf() has been called at any time, then all open files will be flushed and then closed.

All files perform in exactly the same manner, whether they are opened by default (stdin, stdout, and stderr) or opened by using fopen().


Previous



Copyright © 1999, Green Hills Software. All rights reserved.