问题
In some cases the coder cannot rely on system calls being atomic, e.g. if the file is on a NFS filesystem. (c.f. NFS Overview, FAQ and HOWTO Documents). But atomic system calls are ultimately required for most database work. (c.f. Atomicity of database systems).
Is there a standard (and OS independent) way of confirming writes (and other syscalls) are atomic on a particular FILE in C (or python).
Any suggestions?
Subsequent notes: Atomicity on pipes is discussed in the following:
- unix pipe multiple writers
- What happens if a write system call is called on same file by 2 different processes simultaneously
Note in-particular the "man" page extract dealing specifically with O_APPEND:
If the O_APPEND flag of the file status flags is set, the file offset shall be set to the end of the file prior to each write and no intervening file modification operation shall occur between changing the file offset and the write operation.
回答1:
The write call as defined in POSIX has no atomicity guarantee at all. So you don't need to confirm anything, it's not atomic.
It doesn't even guarantee that the data will have reached the hard drive (if there is a drive at all) if it completes successfully. Successfully reading back the data doesn't give you any guarantees either.
You'll need to use the sync
family of functions to get some durability guarantees.
来源:https://stackoverflow.com/questions/7660293/how-does-one-programmatically-determine-if-write-system-call-is-atomic-on-a-pa