Going forward with NTS
Eric S. Raymond
esr at thyrsus.com
Tue Feb 5 20:10:22 UTC 2019
Hal Murray <hmurray at megapathdsl.net>:
> I'm not enough of a Unix file system wizard. Are there any guarantees about
> multi-threaded writes to the same file not getting scrambled? Will it work if
> we buffer everything into a big buffer and do a single write?
Under Linux, yes, up to 4096 bytes. Look for "I wrote a script to test" on
this page:
https://stackoverflow.com/questions/1154446/is-file-append-atomic-in-unix
There's a great deal of confusion abroad about this. The first answer reflects
a widespread belief that 512-byte writes are guaranteed atomic by POSIX.
That belief is wrong, as is pointed out in the comments. PIPE_BUF is a real
thing in POSIX but has nothing to do with file writes.
I am here to add that I believe the myth about POSIX persists in
practice because 512 byte file write(2) operations *are* in fact atomic on
every Unix. So the mythology about PIPE_BUF is only half wrong; POSIX
does not require it but it's true anyway.
Yes, I have used this assumption in production code under multiple
Unixes and not been bitten.
The way I would recommend handling this is with a PIPE_BUF sized buffer,
write(2), and a comment that explains that PIPE_BUF is accidentally
correct here. Been there, done that myself. This is very old lore.
--
<a href="http://www.catb.org/~esr/">Eric S. Raymond</a>
My work is funded by the Internet Civil Engineering Institute: https://icei.org
Please visit their site and donate: the civilization you save might be your own.
More information about the devel
mailing list