Is a successful call to write(2) atomic?

Ronald F. Guilmette rfg at
Tue Jun 15 07:06:27 UTC 2021

In message <CAFbbPuh1xdoXqxjoEQHyPXVqX=KayokPgmpupEYovSHtcD4P5A at>, 
Paul Procacci <pprocacci at>wrote:

>I had a long response, but decided to shorten it....I hope you don't mind.

Not at all.

>The only time atomicity is guaranteed is for pipes when the write(2) size
>fits in PIPE_BUF (512 bytes on my version of FreeBSD).
>Otherwise, it depends is really the best answer as it depends on the file
>Sometimes filesystems like ext4 require O_DIRECT to be set, in order to
>avoid the garbled text as you've mentioned, and even then it's limited to
>the number of bytes written.

This is deeply distrubing.  I never knew about this.  I am furiously reading
the FreeBSD & Linux man pages for open(2).  (It appears that both support
that flags, O_DIRECT and O_SYNC, aqnd Linux also adds the O_DSYNC flag.
I feel a bit dumb that I never nknew about these until now.)

>A suggestion; the way I've done something similar in the past.....
>Parent Process - Orchestrator - creates pipes handing one end of the pipe
>to each child.
>Child Process - do'ers - write whatever it intends on writing to the pipe
>the parent is waiting for data on.
>Parent process then grabs the data the child wrote, and in turn writes it
>to stdout (or whatever file descriptor you intend).

Yes, yes.  Believe me when I say that I am considering reworking my code
to do exactly this as we speak.  I don't even know for sure if I have a
"{lack of} atomicity" problem in my existing code, but I am 100% sure
that what you just described would completely solve the problem, if indeed
it does exist.

>This method, a bit more involved, ensures one writer to a file descriptor,
>is portable, and you can rest easy about it working on whatever latest and
>greatest file system someone decides to make.

Yes.  Thank you.


More information about the freebsd-questions mailing list