如何刷新 Windows 中的所有文件缓冲区? [英] How to flush all file buffers in Windows?

查看:58
本文介绍了如何刷新 Windows 中的所有文件缓冲区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows 中有 FlushFileBuffers() API 可以将缓冲区刷新到硬盘驱动器以获取单个文件.Linux 中有 sync() API 可以为 所有 文件刷新文件缓冲区.

There is FlushFileBuffers() API in Windows to flush buffers till hard drive for a single file. There is sync() API in Linux to flush file buffers for all files.

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

However, is there WinAPI for flushing all files too, i.e. a sync() analog?

推荐答案

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

可以刷新整个硬盘.

要刷新卷上所有打开的文件,请使用卷的句柄调用 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.

此外,同一篇文章还说明了由于某种原因必须刷新数据时应遵循的正确程序:CreateFile 函数与 FILE_FLAG_NO_BUFFERINGFILE_FLAG_WRITE_THROUGH 标志.

Also, the same article states the correct procedure to follow if, for some reason, data must be flushed: CreateFile function with the FILE_FLAG_NO_BUFFERING and FILE_FLAG_WRITE_THROUGH flags.

由于系统内的磁盘缓存交互,当多次写入单独执行时,在每次写入磁盘驱动器设备后使用 FlushFileBuffers 函数可能效率低下.如果应用程序对磁盘执行多次写入并且还需要确保将关键数据写入持久性介质,则应用程序应使用无缓冲 I/O 而不是频繁调用 FlushFileBuffers.要为无缓冲 I/O 打开文件,请使用 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.

还要检查文件缓冲的限制 关于内存和数据对齐.

But also check the restrictions of file buffering about memory and data alignment.

这篇关于如何刷新 Windows 中的所有文件缓冲区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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