Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 15 May 2004 18:06:11 +0200
From:      Joerg Sonnenberger <joerg@britannica.bec.de>
To:        hackers@freebsd.org
Subject:   Re: Interoperation of flock(2), fcntl(2), and lockf(3)
Message-ID:  <20040515160611.GA1170@britannica.bec.de>
In-Reply-To: <20040515092114.GB67531@comp.chem.msu.su>
References:  <20040515092114.GB67531@comp.chem.msu.su>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sat, May 15, 2004 at 01:21:14PM +0400, Yar Tikhiy wrote:
> Hi folks,
> 
> I've always been confused by the following sentence from the lockf(3)
> manpage:
> 
> 	The lockf(), fcntl(2) and flock(2) locks may be safely used
> 	concurrently.
> 
> Does that mean that each of those calls uses a locking mechanism
> of its own?

Nope, they are all implemented in the same code.

>  Of course, in practice those calls use a mutual
> mechanism, thus allowing serial access to a file from applications
> using different calls.  However, there's an oddity:  While it's
> possible for a process to obtain the same lock several times w/o
> error (it's a no-op case of upgrading the lock,) intermixing flock(2)
> and fcntl(2), or flock(2) and lockf(3), within the same process
> results in EAGAIN upon the second locking attempt.  That's while

The problem lies in the different owner semantic. flock(2) is attached
to a file descriptor and can be released e.g. by a child process sharing
the descriptor. On the other hand, fcntl(2) locks [and lockf(2) which is
indeed a mapper] are per-process locks. They are not inherited on fork(2)
nor can they be released by any other process. That's why you can acquire
a read-lock using all interfaces at the same time.

> mixing fcntl(2) and lockf(3) is all right as long as the latter
> call is just a wrapper for the former one.  Of course, intermixing
> different lock calls within one process is a poor idea at the first
> place, but I can imagine some mail application that tries to coax
> all the mailbox locking schemes at once.

There are some interesting aspects when mixing different lock types, e.g.
can the deadlock detection for POSIX locks fail against flock(2) locks
from the same process and best of all, flock(2) can dead lock itself if
you open the same file twice.

> 
> Considering all the above, I'd like to add the following paragraph
> to the flock(2), lockf(3), and fcntl(2) man pages (replacing the
> sentence quoted from lockf(3)):
> 
> 	The flock(2), fcntl(2), and lockf(3) locks are compatible.
> 	Processes using different locking interfaces can cooperate
> 	over the same file safely.  However, only one of such
> 	interfaces should be used within a process.  If a file is
> 	locked by a process through flock(2), any record within the
> 	file will be seen as locked from the viewpoint of another
> 	process using fcntl(2) or lockf(3), and vice versa.
> 
> Any objections or comments?

I agree that the different semantics should not be mixed, but the
rest can be refined.

Joerg

P.S.: Did I say that the mix of flock(2) and fcntl(2) locks is evil?

> 
> -- 
> Yar
> _______________________________________________
> freebsd-hackers@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20040515160611.GA1170>