Is WriteFile atomic?
Asked Answered
C

1

8

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 ?

Crew answered 4/5, 2010 at 12:58 Comment(0)
E
9

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.

Ezarra answered 13/1, 2011 at 16:57 Comment(1)
Is it atomic as long as the machine is in a sane state? (i.e. if the process might crash, but the kernel is kept in a good state, then is it atomic?)Vilberg

© 2022 - 2024 — McMap. All rights reserved.