linux file IO process

Let's now talk about the Linux file IO process. The file IO was introduced earlier, so what does its specific process look like?

1. First, let's introduce a few functions

Cache synchronization-In order to ensure that the disk system is consistent with the content in the buffer, the Linux system provides three functions: sync, fsync and fdatasync. Function description: write data to the opened file; successfully return the number of bytes written, if an error occurs, return -1.

Header file: #include <unistd.h>        

int fsync(int fd);        

int fdatasync(int fd);        

void sync(void);

Description: sync-queue all modified block buffers into the write queue, and then return. It does not wait for the end of the actual disk write operation. (Commonly used)     

fsync-Write the block buffer of the file corresponding to fd to disk immediately, and wait for the actual disk write operation to complete and return. (Commonly used)        

fdatasync-similar to fsync, but only affects the data part of the file. In addition to data, fsync will also update file attributes synchronously (less used)

Here we focus on sync and fsync because these two are very commonly used

The above meaning has been explained to you, but in order to help you understand more vividly, here is a picture

This picture is a very clear description of the IO process. If you do not synchronize the data when writing the data, the data will stay in the page cache (high-speed page cache) for 1 to 2 minutes, and then be managed by one of the kernels The thread of the data synchronizes it to the disk. In the case of sync, the data is stuffed into the IO queue, and then it is ignored (although 100% synchronization cannot be guaranteed in this way, the efficiency of synchronization will be higher) and leave. . In the case of fsync, it will synchronize the data to the disk, know that the synchronization is completed, and then return, so that the synchronization is successful, but every fsync has to wait for the synchronization to complete and return, so the efficiency is relatively low. So generally we use fsync and sync together.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/qq_44065088/article/details/108430479