Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 15 Jun 2021 23:03:35 -0700
From:      "Ronald F. Guilmette" <rfg@tristatelogic.com>
To:        freebsd-questions@freebsd.org
Subject:   Re: Is a successful call to write(2) atomic?
Message-ID:  <27545.1623823415@segfault.tristatelogic.com>
In-Reply-To: <20210616012606.3C77A1183464@ary.qy>

next in thread | previous in thread | raw e-mail | index | archive | help
In message <20210616012606.3C77A1183464@ary.qy>, =

"John Levine" <johnl@iecc.com> wrote:

>It appears that Ronald F. Guilmette <rfg@tristatelogic.com> said:
>>More to the point, if indeed each call to write() *is* "atomic"... at le=
ast
>>in the sense that the given buffer will be treated like an indivisable w=
hole,
>>all of the way along its journey to some physical device... then why are
>>users nontheless being encouraged, still, to "use some form of concurren=
cy
>>control"?  I mean what would be the point of that if in fact write() nev=
er
>>busts up the hunks of data given to it into separate sub-hunks?
>
>I think it depends on the device.  If I just want to write stuff to a log
>file and not get it scrambled, this should do the trick:
>
>  fd =3D open("somefile", O_CREAT|O_WRONLY|O_APPEND);
>
>  /* put some stuff in buf[] */
>  flock(fd, LOCK_EX);
>  write(fd, buf, strlen(buf)): /* O_APPEND ensures it's added at the end =
*/
>  flock(fd, LOCK_UN);

Thanks John, but as I noted earlier in this thread, if the data passed to
write() in a single call isn't treated as an indivisible whole OR if each
of the (mutltiple) processes that are making the calls (using code as
you have written above) is maintaining its own separate file pointer,
then it isn't 100% clear that what you suggested will actually solve
the "garbling" problem.

I'm going to try passing all lines up to a parent process which will then
be the only one writing to the file, and see if that resolves the issue.


Regards,
rfg



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