in reply to Re: Re: (OT?) File Locking
in thread (OT?) File Locking

Under UNIX, a file opened for appending automatically seeks to the end on every write(). No locking should be required, even with multiple processes appending, as long as every write is accomplished with a single system call (you can check that with strace/truss). If you take a look at the apache source, it doesn't lock the log file when it writes a message, and that's certainly a case where many processes are appending to the same file at once.

In perl, you'll want to set $| on the log file handle, or the output buffers from the different processes will interleave. Or use syswrite.

I don't know if this is true in Windows.

Replies are listed 'Best First'.
Re: Re: Re: Re: (OT?) File Locking
by dws (Chancellor) on Apr 14, 2002 at 19:26 UTC
    I don't know if this [automatic seek to eof when writing a file opened for append] is true in Windows.

    It isn't. This is a frequent source of hard-to-track file corruption.

      The automatic seek is there, it is just that the seek and subsequent write are not done as an atomic operation so there is a race condition. Looking at the source code, I see that the translation of "\n" to "\r\n" is done between the seek and the write, so the race condition will probably be noticeably worse if you didn't use binmode.

              - tye (but my friends call me "Tye")