These two days are in the unix environmental advanced programming, and then the point is found (find yourself getting weaker)
First, talk about the I / O, UNIX file I / O Read, Write is not buffered. Without a cache refers to each read, Write all invokes a system call to the kernel, which is part of POSIX.1. The prototype of both:
SSIZE_T READ (int filedes, void * buf, size_t nbytes); // If you successfully return the number of bytes read
SSIZE_T WRITE (Int filedes, const void * buf, size_t nbytes); // If you successfully return the number of bytes
Note that the above BUF does not refer to the READ, WRITE with cache, but when the read byte is used to store the read bytes, the byte of the Write is stored. For read, Nbytes indicates the number of bytes per reader. However, the size of this block will affect the efficiency of I / O, its value, and specific system.
Supplementally, the I / O to the file is operated without a cached I / O, the following cached I / O is a target.
Standard I / O libraries are I / O with cache, which is described by ANSI C standard. Of course, standard I / O will eventually call the above I / O routine. Standard I / O library replace the user to handle many details, such as cache allocation, to optimize the length to execute I / O, etc.
Standard I / O The purpose of providing cache is to reduce the number of calls read and write, which automatically caches each I / O stream (standard I / O function typically calls Malloc to assign caches). It provides three types of caches:
1) All cache. The I / O operation is executed after filling the standard I / O cache. The files on the disk are usually full cache.
2) row cache. The actual I / O operation is performed by the standard I / O library when the input and output encounters new line characters or cache. Stdin, Stdout is usually a row cache.
3) There is no buffer. Equivalent to Read, Write. Stderr is usually cached because it must be output as soon as possible.
In general, the length of the cache is selected by the system and automatically assign. The standard I / O library automatically releases the cache when the flow is turned off.
In standard I / O libraries, an efficient deficiencies is the amount of data that needs to be replicated. When using each line function fgets and FPUTS, copying two data is usually required: once is between the kernel and the standard I / O cache (when calling Read and Write), the second time is in standard I / O cache ( Usually system allocation and management) and row caches in the user program (the parameters of FGETs require a user row cache pointer).
Whether it is understood in the end, I don't understand, remember a little:
One advantage of using standard I / O routines is that there is no need to consider the choice of cache and optimal I / O length, and it is not more calling read, Write slowly.