加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 如何刷新Windows中的所有文件缓冲区?

发布时间:2020-12-16 09:22:35 所属栏目:百科 来源:网络整理
导读:Windows中有FlushFileBuffers()API可以将缓冲区刷新到单个文件的硬盘驱动器. Linux中有sync()API来刷新所有文件的文件缓冲区. 但是,是否有WinAPI用于刷新所有文件,即sync()模拟? 解决方法 https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf
Windows中有FlushFileBuffers()API可以将缓冲区刷新到单个文件的硬盘驱动器. Linux中有sync()API来刷新所有文件的文件缓冲区.

但是,是否有WinAPI用于刷新所有文件,即sync()模拟?

解决方法

https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-flushfilebuffers

可以冲洗整个硬盘驱动器.

To flush all open files on a volume,call FlushFileBuffers with a handle to the volume. The caller must have administrative privileges. For more information,see Running with Special Privileges.

此外,如果由于某种原因必须刷新数据,同一篇文章规定了要遵循的正确过程:使用FILE_FLAG_NO_BUFFERING和FILE_FLAG_WRITE_THROUGH标志的CreateFile函数.

Due to disk caching interactions within the system,the FlushFileBuffers function can be inefficient when used after every write to a disk drive device when many writes are being performed separately. If an application is performing multiple writes to disk and also needs to ensure critical data is written to persistent media,the application should use unbuffered I/O instead of frequently calling FlushFileBuffers. To open a file for unbuffered I/O,call the CreateFile function with the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags. This prevents the file contents from being cached and flushes the metadata to disk with each write. For more information,see CreateFile.

但是还要检查file buffering关于内存和数据对齐的限制.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读