Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 15 Jun 2021 00:06:25 -0700
From:      "Ronald F. Guilmette" <rfg@tristatelogic.com>
To:        FreeBSD Questions <freebsd-questions@freebsd.org>
Subject:   Re: Is a successful call to write(2) atomic?
Message-ID:  <22440.1623740785@segfault.tristatelogic.com>
In-Reply-To: <CAFbbPuh1xdoXqxjoEQHyPXVqX=KayokPgmpupEYovSHtcD4P5A@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
In message <CAFbbPuh1xdoXqxjoEQHyPXVqX=3DKayokPgmpupEYovSHtcD4P5A@mail.gma=
il.com>, =

Paul Procacci <pprocacci@gmail.com>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
>system.
>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 readi=
ng
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 an=
d
>greatest file system someone decides to make.

Yes.  Thank you.


Regards,
rfg



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?22440.1623740785>