Re: Questions about file-locking

From: Dragan Cvetkovic (
Date: 04/13/04

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

"Luca T." <> 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 !!!