From owner-freebsd-hackers@FreeBSD.ORG Sat May 15 09:09:33 2004 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 5A69416A4CE for ; Sat, 15 May 2004 09:09:33 -0700 (PDT) Received: from britannica.bec.de (wlan033244.uni-rostock.de [139.30.33.244]) by mx1.FreeBSD.org (Postfix) with ESMTP id 0EB7043D5D for ; Sat, 15 May 2004 09:09:32 -0700 (PDT) (envelope-from joerg@britannica.bec.de) Received: from britannica.bec.de (localhost [127.0.0.1]) by britannica.bec.de (8.12.10/8.12.10) with ESMTP id i4FG6BAB001378 for ; Sat, 15 May 2004 18:06:11 +0200 (CEST) (envelope-from joerg@britannica.bec.de) Received: (from joerg@localhost) by britannica.bec.de (8.12.10/8.12.10/Submit) id i4FG6Brn001377 for hackers@freebsd.org; Sat, 15 May 2004 18:06:11 +0200 (CEST) Date: Sat, 15 May 2004 18:06:11 +0200 From: Joerg Sonnenberger To: hackers@freebsd.org Message-ID: <20040515160611.GA1170@britannica.bec.de> Mail-Followup-To: hackers@freebsd.org References: <20040515092114.GB67531@comp.chem.msu.su> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20040515092114.GB67531@comp.chem.msu.su> User-Agent: Mutt/1.4.1i Subject: Re: Interoperation of flock(2), fcntl(2), and lockf(3) X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 15 May 2004 16:09:33 -0000 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"