在将数据保存到磁盘方面,FlushFileBuffers 与 CloseHandle 然后 CreateFile 一样好吗?

发布于 2024-09-08 20:07:28 字数 443 浏览 0 评论 0原文

对于磁盘上的文件,是 Win32 函数 FlushFileBuffers 与使用 CloseHandle 然后使用 CreateFile 重新打开文件?

是否存在 CloseHandle 然后 CreateFile 更好的情况,因为它们将数据正确保存到磁盘,而 FlushFileBuffers 却不能?

For a file on disk, is the Win32 function FlushFileBuffers as reliable and certain as closing the file using CloseHandle then re-opening the file using CreateFile?

Are there circumstances where CloseHandle then CreateFile are better because they save the data correctly to disk when FlushFileBuffers does not?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

软糖 2024-09-15 20:07:28

更好的是,CloseHandle() 不会刷新文件系统缓存写入缓冲区。请注意成本,将数据传输到磁盘可能需要很长时间。 CreateFile 的 FILE_FLAG_NO_BUFFERING 选项允许您避免刷新。但由于书面数据的限制,它非常昂贵且难以正确实现。

It is better, CloseHandle() doesn't flush the file system cache write buffers. Beware of the cost, it can take a long time to get the data to the disk. The FILE_FLAG_NO_BUFFERING option for CreateFile allows you to avoid flushing. But it is very expensive and difficult to get right due to the limitations on the written data.

别想她 2024-09-15 20:07:28

根据文档,FlushFileBuffers 确实将所有内容写入磁盘。不过,亲自测试一下可能并没有什么坏处。我过去做过 BRS 测试(大红色开关……PC 曾经有大红色开关),我发现它确实导致所有内容都被写入。调用 FlushFileBuffers 后,无需完全关闭即可关闭 PC。重新打开它,看看数据是否全部存在。该行为可能会因操作系统而改变(理论上它不应该......但你永远不知道)。很久以前我就做过这样的测试(是在 XP 上,甚至可能是 Windows 2000 上)。

我想这是不言而喻的,但您可能不想在您真正关心的工作站上进行此测试。

According to the documentation, FlushFileBuffers does write everything to the disk. However, it probably doesn't hurt to test it yourself. I've done BRS testing (big red switch ... well PCs used to have big red switches) in the past, and I found that it did cause everything to be written. After the call to FlushFileBuffers, turn the PC off without a clean shutdown. Turn it back on and see if the data is all there. The behavior may change by OS (in theory it shouldn't ... but you never know). It was quite some time ago that I did tests like that (it was on XP or possibly even Windows 2000).

And I suppose it goes without saying, but you probably don't want to do this testing on a workstation that you really care about.

北凤男飞 2024-09-15 20:07:28

虽然这些信息与 Delphi 无关,但地球上部署最多的 SQL 数据库 sqlite(例如在 Firefox 中使用)负责处理此类事情,您可以在这里阅读很多原子操作: http://www.sqlite.org/atomiccommit.html
下面引用了有关 FlushFileBuffers 的文章

9.2 不完整的磁盘刷新

SQLite 使用 fsync() 系统调用
Unix 和 FlushFileBuffers() 系统
调用 w32 以同步文件
系统缓冲区到磁盘氧化物上作为
如步骤 3.7 和步骤 3.10 所示。
不幸的是,我们收到了
报告称,这些都不是
接口的工作方式正如许多广告上所宣传的那样
系统。我们听说
FlushFileBuffers() 可以完全
使用注册表设置禁用
一些 Windows 版本。 ...

Although this information is not related to Delphi, but the most deployed SQL-database on earth, sqlite (used for example in Firefox) takes care on such things and you can read a lot ot atomic operations here: http://www.sqlite.org/atomiccommit.html
Below is a quote from the article about FlushFileBuffers

9.2 Incomplete Disk Flushes

SQLite uses the fsync() system call on
Unix and the FlushFileBuffers() system
call on w32 in order to sync the file
system buffers onto disk oxide as
shown in step 3.7 and step 3.10.
Unfortunately, we have received
reports that neither of these
interfaces works as advertised on many
systems. We hear that
FlushFileBuffers() can be completely
disabled using registry settings on
some Windows versions. ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文