Linux fflush 与 fsync的区别

在学习innodb_flush_method的方法时,看到下面的文章:

http://www.orczhou.com/index.php/2009/08/innodb_flush_method-file-io/

 

文章对此参数介绍的比较详细。(摘录其中一段。。。。)

 

参数Innodb_flush_method(Linux)可以设定为:Fdatasync、O_DSYNC、O_DIRECT。我们看看这个三个参数是如何影响程序MySQL对日志和数据文件的操作:

#mytable td{text-align:center}
 Open logFlush logOpen datafileFlush data
Fdatasync fsync()  fsync()
O_DSYNCO_SYNC   fsync()
O_DIRECT fsync() O_DIRECT Fsync()

Linux man page 上对open的O_SYNC的描述:
O_SYNC
The file is opened for synchronous I/O. Any write ()s on the resulting file descriptor will block the calling process until the data has been physically written to the underlying hardware. But see RESTRICTIONS below .

Restrictions

There are many infelicities in the protocol underlying NFS, affecting amongst others O_SYNC and O_NDELAY .

POSIX provides for three different variants of synchronised I/O, corresponding to the flags O_SYNC , O_DSYNC and O_RSYNC . Currently (2.1.130) these are all synonymous under Linux.


Linux man page上对fflush,fsync,sync和fdatasync的解释:

 

#include <stdio.h>

int fflush(FILE *stream);

Description

The function fflush() forces a write of all user-space buffered data for the given output or update stream via the stream's underlying write function. The open status of the stream is unaffected.

 

Notes
Note that fflush() only flushes the user space buffers provided by the C library. To ensure that the data is physically stored on disk the kernel buffers must be flushed too.

此时就需要用到fsync 或者sync函数。

 

 

#include <unistd.h>

void sync(void);


Description

sync() first commits inodes to buffers, and then buffers to disk.

#include <unistd.h>

int fsync(int fd);

int fdatasync(int fd);

Description

fsync() transfers ("flushes") all modified in-core data of (i.e., modified buffer cache pages for) the file referred to by the file descriptor fd to the disk device (or other permanent storage device) where that file resides. The call blocks until the device reports that the transfer has completed. It also flushes metadata information associated with the file (see stat(2)).

fsync 将文件相关的所有更改都发送到disk device。 这个调用是阻塞的,直到disk通知此函数传输完成。此函数也会将该文件的文件信息flush到disk。

 

Calling fsync() does not necessarily ensure that the entry in the directory containing the file has also reached disk. For that an explicit fsync() on a file descriptor for the directory is also needed.

fdatasync() is similar to fsync(), but does not flush modified metadata unless that metadata is needed in order to allow a subsequent data retrieval to be correctly handled. For example, changes to st_atime or st_mtime (respectively, time of last access and time of last modification; see stat(2)) do not not require flushing because they are not necessary for a subsequent data read to be handled correctly. On the other hand, a change to the file size (st_size, as made by say ftruncate(2)), would require a metadata flush.

The aim of fdatasync(2) is to reduce disk activity for applications that do not require all metadata to be synchronised with the disk.

 

fdatasync与fsync的区别在于fdatasync不会flush文件的metadata信息。这个是为了减少对磁盘的操作。。。

 

Notes
If the underlying hard disk has write caching enabled , then the data may not really be on permanent storage when fsync() / fdatasync() return.

When an ext2 file system is mounted with the sync option, directory entries are also implicitly synced by fsync().

On kernels before 2.4, fsync() on big files can be inefficient. An alternative might be to use the O_SYNC flag to open.

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值