Re: Questions about file-locking

From: David Schwartz (
Date: 04/14/04

Date: Tue, 13 Apr 2004 15:12:57 -0700

"Luca T." <> wrote in message
> Lewin A.R.W. Edwards wrote:
>> man 2 flock
>> "flock(2) places advisory locks only; given suitable permissions on a
>> file, a process is free to ignore the use of flock(2) and perform I/O
>> on the file."
>> fcntl mandatory locking is _possibly_ a better choice, though
>> non-POSIX.
> It doesn't really matter to me if it is mandatory or not... I only need it
> to avoid possible problems between two programs that i wrote. If i have
> understood well fcntl is used to lock only a piece of the file, not the
> whole file, this is why i chose flock and not fcntl and my file must go in
> /etc which is not (normally) an NFS drive.

    It doesn't matter what you lock so long as both programs try to lock the
same thing before accessing the file. Locking the first byte of the file is
just fine. You can lock the first byte even if the file is empty.


Relevant Pages

  • Re: UW imap-2006b: 64 bit problem
    ... UW imapd is obliged> to do considerable more work on SVR4 systems than it does on BSD and> Linux systems which offer flock() locking as an alternative to POSIX> locking. ... IEEE Std 1003.1-1988 requires that all fcntl() locks associated with a file for a given process are removed when *any* file descriptor for that file is closed by that process. ... Put another way, before any library routine opens a file, it must be aware of what files the application and any other libraries have open and locked; otherwise the library routine will remove the lock unexpectedly when it closes the file. ...
  • Re: File locking (Solaris)
    ... >I'd like to use flock() on Solaris, but I'm a bit scared by a paragraph ... The semantics of lock inheritance and release for flock ... differ from those of fcntl(). ... between fcntland lockf() locks is unspecified."). ...
  • Re: Linux, fcntl vs flock and pthreads
    ... First we were using fcntl to do file locking. ... I tried using flock instead and it ... >seconds is needed to get the lock. ...
  • Re: low-end persistence strategies?
    ... > not say if I should use it in preference over fcntl or not. ... It looks to me like flock is 4.2-BSD-style locking and fcntl.lockf is ... Process B opens the file, locks it, updates it, releases the lock. ...
  • Re: flock/fcntl
    ... Note also that some versions of "flock" cannot lock things ... provide its own fcntl-based emulation, ... Is there an easy way to obtain the fcntl ...