Reck
Reck

Reputation: 8792

Atomicity of file writes using O_APPEND in linux

The man pages state that the write() system call is atomic. Does this mean that if I were to have 2 processes both write 4 GB of text to the same file, I can assume that the first write will write its 4 GB, followed by the second write writing its 4 GB in full (assuming that the file was opened with the O_APPEND flag)?

Or will the OS buffer both writes, and then make repeated calls to write() such that the full 8 GB of changes gets written as a series of small chunks? If this is the case, is there any guarantee about the order of these chunks, or can chunks from the first process be interleaved with chunks from the other one?

Upvotes: 3

Views: 702

Answers (2)

Søren Løvborg
Søren Løvborg

Reputation: 8761

The man page and this 2002 email from Linus Torvalds (and longer thread) strongly suggests that all write(2) calls

  • to regular, local files
  • on "regular UNIX filesystems"
  • implemented entirely in-kernel (so no network file systems or FUSE)
  • of at most 0x7ffff000 bytes (per the man-page)

are atomic, short of I/O errors (e.g. disk full), or the writing process being SIGKILL'ed (not to mention kernel bugs and power failures).

This both for concurrent writes to the same file descriptor, and for concurrent writes to different file descriptors referring to the same inode.

This is a stronger requirement than what POSIX guarantees, since it also promises that write (and read) will never return short in these situations, but always write (read) the entire specified amount.

Upvotes: 1

mc110
mc110

Reputation: 2833

Are POSIX' read() and write() system calls atomic? indicates that writes are only guaranteed to be atomic for writes of less than PIPE_BUF bytes, and then only for pipe operations.

What happens if a write system call is called on same file by 2 different processes simultaneously is another question on this topic with the same answer.

Upvotes: 1

Related Questions