Without buffered IO and with buffered IO

Source: Internet
Author: User

Let's take a look at the I/O and standard (cached) I/O with no cache.
I/O with no cache: read,write,open ...
Standard (with cached) I/O: fgets,fread,fwrite ...

This is compared using two corresponding functions:
ssize_t write (int filedes, const void *buff, size_t nbytes)
size_t fwrite (const void *ptr, size_t size, size_t nobj, FILE *FP)

The above buff and PTR refer to the buffer used by the application itself, which is actually written to the buffer memory set by the kernel when it is required to write to the file. if the cache is not full, it is not queued to the output queue until the cache is full or the kernel again needs to reuse the cache before it is discharged into the disk I/O input queue for actual I/O operations, which means that the data is actually written to disk at this time, and this technique is called delayed writing.

If we read and write to the kernel's buffer directly with non-cached I/O, there are many problems with mismanagement (such as excessive write-time, or inefficiencies caused by multiple system calls).

Standard (with cache) I/O solves these problems for us, it handles many details, such as buffer allocation, to optimize the length of the I/O, etc., more convenient for us to use.

Because standard (with cache) I/O adds a buffer to the upper layer of the system call, it introduces the concept of the stream, expressed as file* under Unix/linux (not limited to the notion that Unix/linux,ansi C has a file), File actually contains all the information needed to manage the flow: The actual I/O file descriptor, pointer to stream cache (standard I/O cache, allocated by malloc, also called User State process space cache, different from the kernel set of cache), cache length, current bytes in the cache, error flags, and so on.

As a result, the standard I/O with caching is directed to the stream without cached I/O to the file descriptor Operation .

Standard I/O streams are automatically cached for each I/O stream (standard I/O functions typically call malloc to allocate caching). It provides three types of caching:
1) Full cache. I/O operations are not performed until the standard I/O cache is filled. The files on the disk are usually fully cached.
2) Row cache. Actual I/O operations are performed by the standard I/O library when the input output encounters a new line character or is full. stdin, stdout are usually row caches.
3) No caching. The equivalent of read and write. STDERR is usually not cached because it must be exported as soon as possible.

Generally, the length of the cache is selected by the system and allocated automatically. The standard I/O library automatically releases the cache when the stream is closed. Alternatively, you can use function fflush () to feed (refresh) All of the data that is not written to the kernel (kernel buffers), and Fsync () writes the data for all kernel buffers to a file (disk).

There is also the disadvantage of introducing cache management into the standard I/O library-efficiency issues. For example, when you use one line of functions fgets and fputs, you typically need to replicate two data: once between the kernel and the standard I/O cache (when you call read and write), and the second time in the standard i/ o Caching (usually system allocation and management) and row caching in user programs (fgets parameters require a user row cache pointer).

No matter what the above is understood, remember one point:

One advantage of using standard I/O routines is that there is no need to consider caching and optimal I/O length choices, and it is no more slow than directly calling read and write.

File operation with cache is the implementation of standard C library, first call with cached file operationfunctionThe standard library automatically allocates memory and reads a fixed amount of content stored in the cache. So each subsequent read and write operation is not directed at the files on the hard disk, but on the memory cache. When to read a file from a hard disk or write a file to a hard disk there is a mechanism control for the standard library. File operations without caching are usually system calls, lower-level, read and write files directly from the hard drive, the speed is not satisfactory due to IO bottlenecks, and the atomic operation requires the programmer to guarantee itself, but the use of appropriate efficiency is not bad. In addition, the cached file IO in the standard library is invoked by the system with no cache IO implementation. "The term without buffering means that each read and write invokes a system call in the kernel." All disk I/O is passed through the kernel block buffer (also known as the kernel buffer cache), with the exception of I/O to the original disk device. Since read or write data is buffered by the kernel, the term "no buffered I/O" means that the two functions are not automatically buffered in the user's process, and a system call is made every time read or write. "--------excerpted from <unix environmental programming >----------------------------------------------------------------------------------- ------Linux Standard IO Library caching policies The standard IO Library operations revolve around a stream, and when we open a file through the fopen Standard IO library function, we associate a file with an IO stream. Here we equate the IO stream with the file pointer file* because all operations against IO streams are implemented through file* pointers.     we know that the purpose of introducing the standard IO Library is to improve IO efficiency, avoid frequent read/write system calls, and system calls consume more resources. Thus, the standard IO library introduces IO caching, which improves IO efficiency by accumulating a certain amount of IO data and then centralizing the writing to the actual file to reduce system calls. The standard IO library automatically manages the internal cache and does not require programmer intervention. However, it is also because we do not see the standard IO Library cache, sometimes it gives us a certain degree of confusion. The caching strategy for the standard IO library is described here.     A. Standard I/O Cache-Standard Output for example: (this is all referred to by default)     1 when stdout is connected to the terminal device, it is a row cache, which means that the standard IO library does not see a new line character \ The cache is refreshed once (that is, the actual output operation is performed once).This feature can be verified by the following test code    int main ()
{
printf ("This line Should is Cached ...");
Sleep (3); At this point there is no output on the terminal
printf ("\nthis line Should is Cached Again"); At this point you can see the output of the first printf, because the line-breaking character refreshes the
Sleep (3); At this point, you can see only one row of output, but not the second printf output
printf ("This line Should isn't be Cached again\n"); This time you can see the output of the second and third printf, because the end \ n Refresh
Sleep (3);
GetChar ();
 }   2 when STDOUT is redirected to a specific file, the standard output is fully cached, meaning that the actual write operation is performed only when the output cache is stuffed or when fflush or fclose are invoked, and no specific example is given here. The stdout can be freopen by redirecting it to a specific file for testing.    two. Standard error STDERR: In order to see the error message as soon as possible, the standard error is not with any caching

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.