cache flush function

    Traditional UNIX system implementations have buffer caches or page caches in the kernel, and most disk I/O is done through buffers. When writing data to a file, the kernel typically copies the data into a buffer first, then enqueues it, and writes it to disk later. This approach is called "delayed write".
    Typically, when the kernel needs to reuse the buffer for other disk block data, it writes all delayed write blocks to disk. In order to ensure the consistency of the actual file system on the disk and the content in the buffer, the UNIX system provides three functions sync, fsync and fdatasync.
#include <unistd.h>

int fsync(int fd);
int fdatasync(int fd);
                     /* Return value: if successful, return 0; otherwise, return -1 */
void sync(void);

    sync just queues all modified block buffers into the write queue and returns without waiting for the actual write to disk to finish. Typically, a system daemon called update periodically calls (usually every 30 seconds) the sync function, which ensures that the kernel's block buffer is flushed periodically. The UNIX command sync(1) also calls the sync function.
    The fsync function only works on one file specified by the file descriptor fd, and waits for the completion of the write to disk operation before returning. fsync can be used for applications such as databases that need to ensure that modified blocks are written to disk immediately.
    fdatasync is similar to fsync, but it only affects the data portion of the file. In addition to the data, fsync also updates the properties of the file synchronously.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326313088&siteId=291194637