WriteFile 是原子的吗?
我正在设计一个将时间序列数据写入文件的系统。数据是 8 字节的块,分为两个 4 字节部分:时间和有效负载。
根据 MSDN,WriteFile 函数是原子的( http:// msdn.microsoft.com/en-us/library/aa365747(VS.85).aspx),如果写入的数据大小小于一个扇区。
由于文件仅包含这些块(文件没有“结构”,因此不可能重建损坏的文件),逐个添加,因此至关重要的是整个块或没有任何内容写入文件所有的时间。
所以问题是,我是否正确理解了大小小于扇区的 writefile 总是完全写入磁盘或根本不写入,无论实际调用 writefile 期间发生什么情况?
I'm designing a system that will write time series data to a file. The data is blocks of 8 bytes divided into two 4 bytes parts, time and payload.
According to MSDN the WriteFile function is atomic ( http://msdn.microsoft.com/en-us/library/aa365747(VS.85).aspx ), if the data written is less than a sector in size.
Since the file will only contain these blocks (there is no "structure" of the file so it's not possible to reconstruct a damaged file), added one after each other, it's vital that the whole block, or nothing is written to the file at all times.
So the question is, have I understood it correctly that a writefile less than a sector in size is alway written completely to disk or not written at all, no matter what happens during the actual call to writefile ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要写入不跨越文件中的扇区边界,WriteFile 就是原子的。因此,如果扇区大小为 512 字节,则从文件偏移量 0 开始写入 20 个字节将是原子的,但在文件偏移量 500 写入相同的数据将不是原子的。在您的情况下,写入应该是原子的,因为扇区大小应该是 8 的倍数。
此 MSDN 博客 有关如何在不使用事务处理 NTFS 的情况下执行原子多扇区写入的更多信息。
WriteFile is atomic as long as the write does not cross a sector boundary in the file. So if the sector size is 512 bytes, writing 20 bytes starting at file offset 0 will be atomic, but the same data written at file offset 500 will not be atomic. In your case the writes should be atomic, since the sector size should be a multiple of 8.
This MSDN blog has more information on how to do an atomic multi-sector write without using transacted NTFS.