open(2) 中的 O_SYNC 和 O_DIRECT 标志有何不同/相似之处? [英] How are the O_SYNC and O_DIRECT flags in open(2) different/alike?

查看:34
本文介绍了open(2) 中的 O_SYNC 和 O_DIRECT 标志有何不同/相似之处?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

O_SYNC 和 O_DIRECT 标志的使用和效果非常令人困惑,并且似乎因平台而异.从 Linux 手册页(参见此处),O_DIRECT 提供同步 I/O,最小化缓存影响并要求您自己处理块大小对齐.O_SYNC 只是保证同步 I/O.虽然两者都保证将数据写入硬盘的缓存,但我相信直接 I/O 操作应该比普通同步 I/O 更快,因为它们绕过页面缓存(尽管 FreeBSD 的 open(2) 手册页指出使用 O_SYNC 时会绕过缓存.参见 这里).

The use and effects of the O_SYNC and O_DIRECT flags is very confusing and appears to vary somewhat among platforms. From the Linux man page (see an example here), O_DIRECT provides synchronous I/O, minimizes cache effects and requires you to handle block size alignment yourself. O_SYNC just guarantees synchronous I/O. Although both guarantee that data is written into the hard disk's cache, I believe that direct I/O operations are supposed to be faster than plain synchronous I/O since they bypass the page cache (Though FreeBSD's man page for open(2) states that the cache is bypassed when O_SYNC is used. See here).

O_DIRECT 和 O_SYNC 标志之间究竟有什么区别?一些实现建议使用 O_SYNC |O_DIRECT.为什么?

What exactly are the differences between the O_DIRECT and O_SYNC flags? Some implementations suggest using O_SYNC | O_DIRECT. Why?

推荐答案

O_DIRECT 仅承诺内核将避免将数据从用户空间复制到内核空间,而是直接通过 DMA(直接内存访问;如果可能)).数据不会进入缓存.没有严格保证该函数只有在所有数据传输完毕后才会返回.

O_DIRECT alone only promises that the kernel will avoid copying data from user space to kernel space, and will instead write it directly via DMA (Direct memory access; if possible). Data does not go into caches. There is no strict guarantee that the function will return only after all data has been transferred.

O_SYNC 保证在所有数据传输到磁盘之前调用不会返回(就操作系统而言).这仍然不能保证数据不在硬盘写入缓存中的某处,但操作系统可以保证.

O_SYNC guarantees that the call will not return before all data has been transferred to the disk (as far as the OS can tell). This still does not guarantee that the data isn't somewhere in the harddisk write cache, but it is as much as the OS can guarantee.

O_DIRECT|O_SYNC 是这些的组合,即DMA + 保证".

O_DIRECT|O_SYNC is the combination of these, i.e. "DMA + guarantee".

这篇关于open(2) 中的 O_SYNC 和 O_DIRECT 标志有何不同/相似之处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆