Re: Interoperation of flock(2), fcntl(2), and lockf(3)

From: Joerg Sonnenberger (joerg_at_britannica.bec.de)
Date: 05/15/04

  • Next message: Søren Schmidt: "Re: Third IDE hard disk"
    Date: Sat, 15 May 2004 18:06:11 +0200
    To: hackers@freebsd.org
    
    

    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"
    _______________________________________________
    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"


  • Next message: Søren Schmidt: "Re: Third IDE hard disk"

    Relevant Pages

    • Re: [PATCH 0/3] 64-bit futexes: Intro
      ... i suspect _any_ abstract locking functionality around a data structure ... tightly coupled to the exact details of which atomics are fast on a ... For example, some loads are almost entirely read-read locks, with only ... There we can just do a write to unlock. ...
      (Linux-Kernel)
    • Re: Transaction Isolation Level
      ... what size servers are you working ... and only have to specify locking hints when we ... need locks. ...
      (microsoft.public.sqlserver.server)
    • Re: Back end database lock-up
      ... Check the open mode and locking strategy of *each* front end. ... database into front end and back end databases and put the front end ... only lock the records that are being updated, but it locks the entire ... The locking file is not created until a user updates the data via ...
      (comp.databases.ms-access)
    • Re: row vs page locking...
      ... so they automatically escalate to page level locks. ... 'Lock Escalation' - see Books Online. ... SQL Server 2005 is going to have a Snapshot ... Good old fashioned locking is less sexy, but I find, more productive! ...
      (microsoft.public.sqlserver.server)
    • Re: Lock method for 1 million items
      ... implement the writer and/or reader locking; ... The hashed locks can help with both... ... What about write-locking and then read-locking the same object by the same thread? ... What are the limits of your "hashed locks". ...
      (comp.programming.threads)