Sync, Fsync and Fdatasync

Source: Internet
Author: User

Transfer from http://blog.csdn.net/todd911/article/details/11701847

Traditional UNIX implementations have a buffer cache or page cache in the kernel, and most disk I/O is buffered. When writing data to a file, the kernel typically copies the data into one of the buffers, and if the buffer is not yet full, it is not queued to the output queue, but waits for it to be full or when the kernel needs to reuse the buffer to hold other disk block data. The buffer is then queued to the output queue, and then the actual I/O operation is performed when it arrives at the head of the team. This type of output is called deferred write (delayed write)

deferred write reduces disk read and write times, but reduces file content updates so that data written to the file is not written to disk for a period of time. In the event of a system failure, this delay may result in the loss of file update content. To ensure consistency between the actual file system on the disk and the content in the buffer cache, the UNIX system provides sync, Fsync, and Fdatasync three functions.

The declaration of the function is as follows:

[CPP]View Plaincopy
    1. #inlcude <unistd.h>
    2. void sync (void);
    3. int Fsync (int filedes);
    4. int Fdatasync (int filedes);
    5. Fsync and Fdatasync return 0 if successful, and 1 if the error returns.

The 1.sync function simply queues all the modified block buffers into the write queue and then returns, not waiting for the actual write disk operation to end . The system daemon, commonly referred to as update, periodically calls the sync function (typically every 30 seconds). This ensures that the block buffers of the kernel are flushed periodically. Command sync also calls the Sync function .

The 2.fsync function is a system-provided system call . works only on a single file specified by the file descriptor Filedes, and waits for the write disk operation to end and then returns . Fsync can be used in applications such as databases, which need to ensure that modified blocks are immediately written to disk. Accepts a file descriptor of type int.

The 3.fdatasync function is similar to Fsync, but it affects only the data portion of the file . In addition to the data, Fsync also synchronizes the properties of the updated file.

Sync, Fsync and Fdatasync

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.