Date: 15 Jun 2021 21:26:04 -0400 From: "John Levine" <johnl@iecc.com> To: freebsd-questions@freebsd.org Cc: rfg@tristatelogic.com Subject: Re: Is a successful call to write(2) atomic? Message-ID: <20210616012606.3C77A1183464@ary.qy> In-Reply-To: <26258.1623802141@segfault.tristatelogic.com>
next in thread | previous in thread | raw e-mail | index | archive | help
It appears that Ronald F. Guilmette <rfg@tristatelogic.com> said:
>More to the point, if indeed each call to write() *is* "atomic"... at least
>in the sense that the given buffer will be treated like an indivisable whole,
>all of the way along its journey to some physical device... then why are
>users nontheless being encouraged, still, to "use some form of concurrency
>control"? I mean what would be the point of that if in fact write() never
>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 = 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);
R's.
John
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20210616012606.3C77A1183464>
