WriteFile 是原子的吗? [英] Is WriteFile atomic?

查看:31
本文介绍了WriteFile 是原子的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设计一个将时间序列数据写入文件的系统.数据是8字节的块,分为时间和载荷两个4字节的部分.

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.

根据 MSDN,WriteFile 函数是原子的(http://msdn.microsoft.com/en-us/library/aa365747(VS.85).aspx ),如果写入的数据大小小于一个扇区.

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.

所以问题是,我是否正确理解了小于一个扇区大小的 writefile 总是完全写入磁盘或根本不写入,无论在实际调用 writefile 期间发生什么?

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 ?

推荐答案

WriteFile 是原子的,只要写入不跨越文件中的扇区边界.因此,如果扇区大小为 512 字节,则从文件偏移量 0 开始写入 20 字节将是原子的,但在文件偏移量 500 处写入的相同数据将不会是原子的.在您的情况下,写入应该是原子的,因为扇区大小应该是 8 的倍数.

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.

这个 MSDN 博客有更多信息关于如何在不使用事务性 NTFS 的情况下进行原子多扇区写入.

This MSDN blog has more information on how to do an atomic multi-sector write without using transacted NTFS.

这篇关于WriteFile 是原子的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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