Differences between cached I/O and non-cached I/O in UNIX systems

Source: Internet
Author: User
Take ssize_t write (INT filedes, const void * buff, size_t nbytes) and size_t fwrite (const void * PTR, size_t size, size_t nobj, file * FP) let's talk about the differences between I/O with cache and I/O without cache in UNIX systems. First, we need to understand the concept that the so-called cache proxy does not refer to the buff parameters of the above two functions,Buffer Memory set in the kernel of UNIX systems.
When writing data to a file, the kernel first writes the data to the cache. If the cache is not fullOutput queueUntil the cache is full or the kernel needs to re-use the cache, It is not discharged into the input queue until it reaches the first line, in the actual I/O operation, that is, the data is actually written to the disk at this time. This technology is called delayed writing. Assume that the cache set by the kernel is 100 bytes. If you use write and the buff size is 10, when you want to write nine identical buckets to a file, you need to call 9 Write times, that is, 9 system calls. At this time, the data is not written to the hard disk. If you want to write data to the hard disk immediately,Call fsync to perform actual I/O operations. The standard I/O, that is, the cache-containing I/O adopts file *. The file actually contains all the information required to manage the stream: the actual I/O file descriptor, pointer to the stream cache (standard I/O cache, allocated by malloc, also known as the cache of user-state process space, different from the cache set by the kernel), cache length, the number of bytes in the cache and error mark. If the length of the stream cache is 50 bytes, write the above data to the file, you only need to call the system twice (fwrite calls the Write System Call ),Because the data is first written to the stream cache, it is filled in with the kernel cache when it is full or when fflush is called, so the system calls write twice.. Fflush sends (refresh) All unwritten data in the stream to the kernel (kernel buffer), and fsync writes data in all kernel buffers to a file (Disk ). Read and Write without caching are described in comparison with stream functions such as fread/fwrite. Because fread and fwrite are user functions (3), they performData slowdownRead/write is a system call (2) so they do not cache at the user layer, so read and write are non-Cache Io,In fact, the kernel is still cached, but the user layer cannot see it..

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.