Standard I/O buffering (5.4)

Source: Internet
Author: User
Document directory
  • Buffering
Buffering

The goal of the buffering provided by the standard I/O library is to use the minimum numberReadAndWriteCILS. (recall Figure 3.5, where we showed the amount of CPU time required to perform I/O using various buffer sizes .) also, it tries to do its buffering automatically for each I/O Stream, obviating the need for the application to worry about it. unfortunately, the single aspect of the standard I/O library that generates the most confusion is its buffering.

Three types of buffering are provided:

  1. Fully buffered. in this case, actual I/O takes place when the standard I/O buffer is filled. files residing on disk are normally fully buffered by the standard I/O library. the buffer used is usually obtained by one of the standard I/O functions callingMalloc(Section 7.8) The first time I/O is already med on a stream.

    The term flush describes the writing of a standard I/O buffer. A buffer can be flushed automatically by the standard I/O routines, such as when a buffer fills, or we can call the FunctionFflushTo flush a stream. unfortunately, in the Unix environment, flush means two different things. in terms of the standard I/O library, it means writing out the contents of a buffer, which may be partially filled. in terms of the Terminal Driver, such asTcflushFunction in chapter 18, it means to discard the data that's already stored in a buffer.

  2. Line buffered. in this case, the standard I/O Library performs I/O when a newline character is encountered on input or output. this allows us to output a single character at a time (with the standard I/OFputcFunction), knowing that actual I/O will take place only when we finish writing each line. line buffering is typically used on a stream when it refers to a terminal: standard input and standard output, for example.

    Line buffering comes with two caveats. first, the size of the buffer that the standard I/O Library is using to collect each line is fixed, so I/O might take place if we fill this buffer before writing a newline. second, whenever input is requested through the standard I/O library from either (a) An unbuffered stream or (B) A line-buffered stream (that requires data to be requested from the kernel), all line-buffered output streams are flushed. the reason for the qualifier on (B) is that the requested data may already be in the buffer, which doesn't require data to be read from the kernel. obviusly, any input from an unbuffered stream, item (a), requires data to be obtained from the kernel.

  3. Unbuffered. The standard I/O Library does not buffer the characters. If we write 15 characters with the standard I/OFputsFunction, for example, we recommend CT these 15 characters to be output as soon as possible, probably withWriteFunction from section 3.8.

    The standard error stream, for example, is normally unbuffered. This is so that any error messages are displayed as quickly as possible, regardless of whether they contain a newline.

Iso c requires the following buffering characteristics.

  • Standard input and standard output are fully buffered, if and only if they do not refer to an interactive device.

  • Standard error is never fully buffered.

This, however, doesn' t tell us whether standard input and standard output can be unbuffered or line buffered if they refer to an interactive device and whether standard error shoshould be unbuffered or line buffered. most implementations default to the following types of buffering.

  • Standard error is always unbuffered.

  • All other streams are line buffered if they refer to a terminal device; otherwise, they are fully buffered.

    The four platforms discussed in this book follow these conventions for standard I/O buffering: standard error is unbuffered, streams open to terminal devices are line buffered, and all other streams are fully buffered.

We recommend e standard I/O Buffering in more detail in section 5.12 and Figure 5.11.

If we don't like these defaults for any given stream, we can change the buffering by calling either of the following two functions.

#include                                                
 void setbuf(FILE *restrict fp, char *restrict buf); 
 int setvbuf(FILE *restrict fp, char *restrict buf, 
int mode, size_t size);

Returns: 0 if OK, nonzero on Error

 

These funther must be called after the stream has been opened (obviusly, since each requires a valid file pointer as its first argument) but before any other operation is already med on the stream.

WithSetbuf, We can turn buffering ON or OFF. To enable buffering, Buf must point to a buffer of LengthBufsiz, A constant defined in. Normally, the stream is then fully buffered, but some systems may set line buffering if the stream is associated with a terminal device. To Disable buffering, we set BufNull.

WithSetvbuf, We specify exactly which type of buffering we want. This is done with the modeargument:

_ Iofbf

Fully buffered

_ Iolbf

Line buffered

_ Ionbf

Unbuffered

If we specify an unbuffered stream, the Buf and size arguments are ignored. if we specify fully buffered or line buffered, Buf and size can optionally specify a buffer and its size. if the stream is buffered and Buf isNull, The standard I/O library will automatically allocate its own buffer of the appropriate size for the stream. By appropriate size, we mean the value specified by the constantBufsiz.

Some C library implementations use the value fromSt_blksizeMember ofStatStructure (see section 4.2) to determine the optimal standard I/O buffer size. As we will see later in this chapter, the GNU C library uses this method.

Figure 5.1 summarizes the actions of these two functions and their varous options.

Figure 5.1. Summary of SetbufAnd SetvbufFunctions

Function

Mode

Buf

Buffer and length

Type of buffering

Setbuf

 

Non-null

User Buf of LengthBufsiz

Fully buffered or line buffered

Null

(No buffer)

Unbuffered

Setvbuf

_ Iolbf

Non-null

User Buf of Length Size

Fully buffered

Null

System Buffer of appropriate length

_ Iofbf

Non-null

User Buf of Length Size

Line buffered

Null

System Buffer of appropriate length

_ Ionbf

(Ignored)

(No buffer)

Unbuffered

 

Be aware that if we allocate a standard I/O buffer as an automatic IC variable within a function, we have to close the stream before returning from the function. (We'll discuss this more in section 7.8 .) also, some implementations use part of the buffer for internal bookkeeping, so the actual number of bytes of data that can be stored in the buffer is less than size. in general, we shocould let the system choose the buffer size and automatically allocate the buffer. when we do this, the standard I/O Library Automatically releases the buffer when we close the stream.

At any time, we can force a stream to be flushed.

# Include

 int fflush(FILE *fp);

Returns: 0 if OK,EOFOn Error

 

This function causes any unwritten data for the stream to be passed to the kernel. as a special case, if FP isNull, This function causes all output streams to be flushed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.