> A unix question:
> Two (or more) processes 'open' (not fopen) the same file with
> O_APPEND. Then both processes 'write', say, 80 byte blocks to the
> file.
> Do the two (or more) processes stomp on each other's blocks of data?
> And by stomp, I mean will characters or blocks be interleaved or over-
> written. And what happens at block boundaries?
Not unless some turkey has broken something internally. The point of
O_APPEND is to solve exactly this problem. Each write() is atomic, and
with O_APPEND, they will all append to the file. Caveat: The atomicity
of write() calls is guaranteed only up to BUFSIZ bytes. But with
O_APPEND, longer writes won't stomp on each other; they might be
interlaced.
Quote:> And what happens with nfs and afs in the equation?
No idea. I wouldn't trust them unless I could get a definitive answer
from someone who I personally believed was knowledgeable.
Quote:> The big question is how do we get a whole bunch of processes to open a
> single file (a log file, in fact) and write to the end of it at the same
> time.
Open() with O_APPEND, and use write(). Don't use fopen() and
fprintf(); it is free to chop messages up in interesting and amusing
ways, and they'll come out interlaced. If you want printf's
formatting, use sprintf() to write to a buffer (which is no bigger
than BUFSIZ), and write it with a single write() call. This is what I
do in my personal C debugging module, and the result is highly
portable and quite reliable. Don't use any of the stdio routines to
write it out; even if you call fflush(), you aren't guaranteed that
the data will make it into the kernel much less to the disk within any
finite time limit. (Sad but true of too many commercial systems.) You
might also consider calling fsync() if you're worried about loss of
messages during a crash, and of course that's when you're most
interested in not losing the messages.
Quote:> We are thinking HP/UX here, but a general answer would be useful too.
It isn't very well documented, but anything that calls itself "Unix"
will guarantee atomicity of read() and write() up to BUFSIZ bytes, and
O_APPEND will work to prevent lost output to audit trails. (You then
have only the minor problem of the files filling up your disk. ;-)
--
All opinions Copyright (c) 1991 by John Chambers. Inquire for licensing at:
Home: 1-617-484-6393 ...!{bu.edu,harvard.edu,ima.com,eddie.mit.edu,ora.com}!minya!jc
Work: 1-508-486-5475 {sppip7.lkg.dec.com!jc,ub40::jc}