如何刷新 Windows 中的所有文件缓冲区?
How to flush all file buffers in Windows?
FlushFileBuffers()
API 在 Windows 中用于将缓冲区刷新到硬盘驱动器以获取 单个 文件。 Linux 中有 sync()
API 用于刷新 所有 个文件的文件缓冲区。
但是,是否有 WinAPI 也可以刷新所有文件,即 sync()
模拟?
根据 File Management Functions 在 WinAPI 中 Linux 没有任何 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.
此外,同一篇文章还说明了如果由于某种原因必须刷新数据时应遵循的正确过程:CreateFile
带有 FILE_FLAG_NO_BUFFERING
和 FILE_FLAG_WRITE_THROUGH
标志的函数。
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关于内存和数据对齐的限制。
FlushFileBuffers()
API 在 Windows 中用于将缓冲区刷新到硬盘驱动器以获取 单个 文件。 Linux 中有 sync()
API 用于刷新 所有 个文件的文件缓冲区。
但是,是否有 WinAPI 也可以刷新所有文件,即 sync()
模拟?
根据 File Management Functions 在 WinAPI 中 Linux 没有任何 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.
此外,同一篇文章还说明了如果由于某种原因必须刷新数据时应遵循的正确过程:CreateFile
带有 FILE_FLAG_NO_BUFFERING
和 FILE_FLAG_WRITE_THROUGH
标志的函数。
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关于内存和数据对齐的限制。