FlushFileBuffers as good as CloseHandle then CreateFile at saving data to disk?
对于磁盘上的文件,Win32函数FlushFileBuffers是否可靠且确定,例如使用CloseHandle关闭文件,然后使用CreateFile重新打开文件?
在某些情况下,CloseHandle优于CreateFile会更好,因为当FlushFileBuffers不保存时,它们会将数据正确地保存到磁盘上了吗?
最好,CloseHandle()不会刷新文件系统缓存写缓冲区。注意成本,将数据花费到磁盘上可能要花费很长时间。 CreateFile的FILE_FLAG_NO_BUFFERING选项使您避免刷新。但是由于写入数据的限制,这非常昂贵且难以正确处理。
根据文档,FlushFileBuffers确实将所有内容写入磁盘。但是,自己进行测试可能不会受到伤害。我过去曾经进行过BRS测试(红色的大开关,而以前的PC曾经有红色的大开关),我发现它确实导致了所有内容的编写。调用FlushFileBuffers之后,请关闭PC,而不进行干净关机。重新打开它,看看数据是否全部存在。操作系统可能会改变行为(从理论上讲不应该,但是您永远不会知道)。很久以前,我做了类似的测试(在XP甚至Windows 2000上)。
我想不用说了,但是您可能不想在您真正关心的工作站上进行此测试。
尽管此信息与Delphi无关,但在世界上部署最广泛的SQL数据库sqlite(例如在Firefox中使用)会处理此类问题,您可以在此处阅读很多其他原子操作:http:// www.sqlite.org/atomiccommit.html
以下是有关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. ...