Linux delay write learning

Source: Internet
Author: User

The following system calls are used to write data in the buffer back to the disk.
Where:
The system calls sync to write data to the buffer first, and then writes the data back to the disk from the buffer;
It is an asynchronous call, but the I/O operation may not be completed.
The fsync System Call will write the metadata and data area data back to the disk;
It is a synchronous call, and the I/O operation is completed when the function returns.
Fdatasync only writes data in the data area back to the disk, and metadata is not written back to the disk.

# Include <unistd. h>

Int fsync (int fd );
Int fdatasync (int fd );
Void sync (void );

Therefore, you can decide whether to call fsync, sync, or fdatasync based on your actual needs.

Metadata: data about data, that is, the descriptive information of a file, such as the file size,
Last modification time, last access time, File Permission, and so on.

What is the difference between fsync and fflush?
# Include <stdio. h>

Int fflush (FILE * stream );
Fflush is a library function provided by libc, while fsync is a system call provided by the operating system;
Fflush writes data from the c-database buffer to the kernel buffer, while fsync writes data from the kernel buffer to the disk;
Fsync writes data to the disk.

Why do I need to write data back to the disk?
In linux, most disk I/O is cached.
When writing data back to the disk, data is not immediately written back to the disk,
Instead, it copies data from the kernel to the buffer and writes the data back to the disk if necessary.
This is delay write.
When necessary, it refers:
1. The buffer is full;
2. The time when the disk is periodically written.
Buffer data structure:
Struct buffer_head {
...
Bool B _dirty;
Time;
};
The member B _dirty indicates whether the buffer is modified;
The member time indicates that the dirty buffer needs to be automatically written back after the time,
Is this done by the Linux kernel thread kflush?

Delayed writing reduces the number of reads and writes to the disk and improves the efficiency of writing files.
However, one problem is:
Files cannot be updated to the disk immediately when being written;
If the system loses power or encounters other unpredictable faults at this time, the file update content will be lost.
Therefore, in linux, fsync, sync, and other system calls are provided for users to immediately write data back to the disk as needed.

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.