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

查看:420
本文介绍了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天全站免登陆