a question on open() and lseek()

Rob Bernardo rob at ptsfb.UUCP
Wed Apr 16 00:42:39 AEST 1986


In article <2594 at brl-smoke.ARPA> moi%brandeis.csnet at csnet-relay.arpa writes:
>
>	The manual page for "open()" mentions that one of the flags one
>	can specify with the call is O_APPEND.  It's supposed to request
>	that all "write()"s be appended to the end of the file.  Is
>	there any reason to use the sequence:
>		f = open( file, O_WRONLY );
>		lseek( f, 0, L_XTND );
>	instead of the single call:
>		f = open( file, O_WRONLY | O_APPEND );

None that I can think of. You would have to so the lseek() call before every
write() if there might be another process appending to the same file.
And that could fail because between your lseek() and your write()
your process might get swapped out and the other appending process might write
at the end of the file; your write() would then overwrite that of the other
process, rather than be at the end of the file. I've seen this happen
where there was a common error log file for a set of related
programs on a system where the O_APPEND flag was broken causing the implicit
lseek to take place only after the open, but no before each write.

Rob Bernardo, San Ramon, CA    (415) 823-2417    {ihnp4|dual|qantel}!ptsfa!rob
			       Pacific * Bell



More information about the Comp.unix.wizards mailing list