Sync, fsync, fdatasync, fflush

Source: Internet
Author: User
Sync, fsync, fdatasync, and fflush
In most Unix/Linux systems, disk I/O write operations are completed through caching. The basic principle is as follows: when writing data to a file, the kernel usually copies the data to one of the buffer zones first. If the buffer zone is not full, it is not discharged into the output queue, instead, wait until it is fully written or when the kernel needs to reuse the buffer to store data from other disk blocks, then the buffer is queued to the output queue, and then when it reaches the queue, to perform the actual I/O operation. This is called delayed write, which greatly reduces the number of disk writes.
However, in the absence of special applications, we need to write application-layer data to the disk in real time, especially in systems with high reliability requirements, data needs to be written to the disk in a timely manner, even if the instantaneous system failure data can be secure. restore, so there are functions such as sync, fsync, fdatasync, and fflush.

  Main differences:

Sync: All modified buffers are added to the write queue and then returned, not waiting until the actual disk write operation ends; The update system daemon periodically calls the sync function every 30 seconds. This ensures regular flushing of the kernel's block buffer. This function has no parameters and takes effect on all buffers in the system.   Fsync: only works for a single file with a file descriptor specified, and waits until the disk write operation ends, and then returns (so it is the safest ); this function takes the specified file descriptor (corresponding to files opened by functions such as open) as the parameter and takes effect only for the files specified by this descriptor. Fdatasync: similar to fsync, but only affects the data part of the file. Fsync also updates the file attributes synchronously. In fact, currently The implementation of the fdatasync function in glibc is the same as that of fsync. It can be understood that it is the same as fsync.   Fflush: a standard Io function (such as fread and fwrite) creates a buffer in the memory. This function refreshes the memory buffer and writes the content to the kernel buffer. To write it to the disk, you also need to call fsync. (That is, call fflush first and then call fsync, otherwise it will not work ). Fflush takes the specified file stream descriptor as the parameter (corresponding to the file stream opened by fopen and other functions). It only returns the data in the upper-layer buffer zone when it is refreshed to the kernel buffer zone, therefore, fsync is not very secure. You need to call fsync to write data to the hard disk. To implement the above functions, You need to convert the file stream Descriptor (FP) to the file descriptor (FD) to facilitate fsync calls. Use the following function: int fileno (File * FP ); <-In stdio. Summary: If a hard disk write command is issued for all the buffers, the sync function should be used. However, note that this function only returns the result after the command is put into the queue. Pay attention to this when programming; if you want to submit the changes made to an opened file to the hard disk, you should call the fsync function, which will be returned only after the data is actually written to the hard disk, therefore, it is the safest and most reliable method. If it is for an opened file stream operation, you should first call fflush to synchronize the modification to the kernel buffer, then, call fsync to synchronize the modification to the hard disk. This article is from http://windlazio.blogspot.com/2011/07/syncfsyncfdatasyncfflush.html.

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.