Is a successful call to write(2) atomic?

Steve O'Hara-Smith steve at sohara.org
Tue Jun 15 22:43:36 UTC 2021


On Tue, 15 Jun 2021 14:49:57 -0400
Kurt Hackenberg <kh at panix.com> wrote:

> No, write(2) is not guaranteed atomic, and that's not obvious. Probably 
> a lot of people have learned that the hard way.

	Strangely (after posting a confident no it isn't guaranteed) I
noticed this in write(2) which implies that it is guaranteed to write a
contiguous block (at least for seekable objects):

--------------
On objects capable of seeking, the write() starts at a position given
by the pointer associated with fd, see lseek(2).  Upon return from write(),
the pointer is incremented by the number of bytes which were written.
--------------

	I've always been sure it wasn't so this is something of a surprise.

	That being said there is no guarantee that another process won't
see EOF at an intermediate point in the write and use that as the starting
point for its write which would then cause corruption.

-- 
Steve O'Hara-Smith <steve at sohara.org>


More information about the freebsd-questions mailing list