4

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:

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.

Community
  • 1
  • 1
NevilleDNZ
  • 1,269
  • 12
  • 31
  • 2
    I think you'll find that databases don't require atomic system calls: they require the *appearance* of atomic updates to the database, but that is done using a transaction log. At any time the actual database files may be inconsistent but the transaction log contains the required information to make them consistent again. – Duncan Oct 05 '11 at 11:33
  • NFS is simply full of highly broken, non-conformant behavior that makes it unsuitable for most uses. Use smb/cifs/9p/anything-but-nfs for serious networked filesystem usages. – R.. GitHub STOP HELPING ICE Oct 05 '11 at 18:41
  • The approach taken by most databases seems to be to put a prominent note into the documentation along the lines of *"Don't run this on NFS."*. – caf Oct 06 '11 at 06:11

1 Answers1

4

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.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • 4
    `write` has some atomicity guarantees for pipes, and it has ordering guarantees for regular files. – R.. GitHub STOP HELPING ICE Oct 05 '11 at 18:39
  • The question says "files" in the title and talks about database work. While you're right, I really don't thing the asker can rely on those guarantees for ACID-type work. – Mat Oct 06 '11 at 06:19