3

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?

Reck
  • 7,966
  • 2
  • 20
  • 24

1 Answers1

1

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.

Community
  • 1
  • 1
mc110
  • 2,825
  • 5
  • 20
  • 21
  • doesn't that only hold true for writes to a pipe? – Reck May 30 '14 at 15:43
  • I believe you are right - in fact there is no atomicity guarantee for file writes as suggested in the answer to the question I linked to - edited answer above to clarify. – mc110 May 30 '14 at 15:46