Re: Questions about file-locking

From: Dragan Cvetkovic (me_at_privacy.net)
Date: 04/13/04


Date: Tue, 13 Apr 2004 17:25:27 -0400


"Luca T." <lucat@despammed.com> writes:

> 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.

Why not using lockf() instead? It is POSIX and if you want to, you can lock
the whole file. Don't know about Linux, but POSIX specifies that:

    If size is 0, the section from the current offset through the largest
    possible file offset shall be locked (that is, from the current
    offset through the present or any future end-of-file).

That way, if you ever decide to port your program to other Unices, you'll
have much less problem (for example on Solaris flock() is part of UCB
subsystem that need not be installed).

Bye, Dragan

-- 
Dragan Cvetkovic, 
To be or not to be is true. G. Boole      No it isn't.  L. E. J. Brouwer
!!! Sender/From address is bogus. Use reply-to one !!!


Relevant Pages

  • 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."). ...
    (comp.unix.programmer)
  • 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. ...
    (comp.unix.programmer)
  • 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. ...
    (comp.lang.python)
  • 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 ...
    (comp.lang.perl.misc)
  • Re: I cant flock, it returns 0, any ideas.
    ... > I can't flock, it returns 0, any ideas. ... You can only hold a lock on an open file. ... will fail but attempts to get another LOCK_SH lock will ... this is not how you pause the script! ...
    (comp.lang.perl.misc)