Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is WriteFile atomic?

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 ?

like image 526
ROAR Avatar asked May 04 '10 12:05

ROAR


People also ask

Is Pwrite Atomic?

Within a multithreaded process, Posix does require read() , write() , pread() and pwrite() to be atomic when they operate on regular files (or symbolic links).

Is read system call Atomic?

System call atomicityUnix file system system calls, such as read and write , should have atomic effect. Atomicity is a correctness property that concerns concurrency—the behavior of a system when multiple computations are happening at the same time. (For example, multiple programs have the file open at the same time.)

Is writing to a file atomic?

Atomic in general means the operation cannot be interrupted will complete or have no effect. When writing files, that is accomplished by writing to a temporary file then replacing the original with the temporary when the write completes.

What are atomic files?

Atomic file guarantees file integrity by ensuring that a file has been completely written and sync'd to disk before renaming it to the original file.


1 Answers

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.

like image 197
Alex Avatar answered Nov 15 '22 09:11

Alex