Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 27 Nov 1997 23:51:22 -0700 (MST)
From:      Marc Slemko <marcs@znep.com>
To:        Charles Mott <cmott@srv.net>
Cc:        hackers@FreeBSD.ORG
Subject:   Re: flock() question
Message-ID:  <Pine.BSF.3.95.971127234335.27255C-100000@alive.znep.com>
In-Reply-To: <Pine.BSF.3.96.971127223541.29176B-100000@darkstar.home>

next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, 27 Nov 1997, Charles Mott wrote:

> Below are two test programs.  What I observe is that if a file descriptor
> is opened and a process forks, then advisory locking does not appear to
> work.  On the other hand, if the process forks, and then the file is
> opened independently by each process, advisory locking works perfectly
> well.
> 
> Is this correct?  From reading the man page on flock(2), I would have

Locking works fine.  It just doesn't do what you think it does.

> guessed that locking would have worked in both situations.  I am running
> 2.2.2-R.

from the man page:

     Locks are on files, not file descriptors.  That is, file descriptors du-
     plicated through dup(2) or fork(2) do not result in multiple instances of
     a lock, but rather multiple references to a single lock.  If a process
     holding a lock on a file forks and the child explicitly unlocks the file,
     the parent will lose its lock.

See the USE_FLOCK_SERIALIZED_ACCEPT stuff in Apache before 1..3b3
for an example of how not to do it.

See the USE_FLOCK_SERIALIZED_ACCEPT stuff in Apache 1.3b3 for an
example of how to do it.

Essentially it boils down to what you say.  It is a pain and is a
horrible broken thing about flock() locking.  OTOH, fcntl() locking
doesn't work that way and it is also horrible broken.  Neither of
the semantics are great, and both are arguably dumb in certain
ways.

Hmm.  It looks like if you have multiple processes blocked on the
same lock in FreeBSD (well, 2.2 anyway), they are all woken up when
the lock is freed.  Yes, only one will get the lock but they will
all be woken.  Unless I am reading the code wrong...  This is in
contrast to multiple processes blocking in accept(), where only
one will be woken up.




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.3.95.971127234335.27255C-100000>