Seth Woolley's Man Viewer

Manual for flock - man 2 flock

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

FLOCK(2)                   Linux Programmer's Manual                  FLOCK(2)



NAME
       flock(1,2) - apply or remove an advisory lock on an open(2,3,n) file(1,n)

SYNOPSIS
       #include <sys/file.h>

       int flock(1,2)(int fd, int operation);

DESCRIPTION
       Apply or remove an advisory lock on the open(2,3,n) file(1,n) specified by fd.  The
       parameter operation is one of the following:


              LOCK_SH   Place a shared lock.  More than one process may hold a
                        shared lock for a given file(1,n) at a given time.

              LOCK_EX   Place an exclusive lock.  Only one process may hold an
                        exclusive lock for a given file(1,n) at a given time.

              LOCK_UN   Remove an existing lock held by this process.


       A call to flock(1,2)() may block if(3,n) an incompatible lock is held by  another
       process.   To  make  a non-blocking request, include LOCK_NB (by ORing)
       with any of the above operations.

       A single file(1,n) may not simultaneously have  both  shared  and  exclusive
       locks.

       Locks  created  by  flock(1,2)()  are  associated with a file(1,n), or, more pre-
       cisely, an open(2,3,n) file(1,n) table  entry.   This  means  that  duplicate  file(1,n)
       descriptors  (created  by, for example, fork(2) or dup(2)) refer to the
       same lock, and this lock may be modified or released using any of these
       descriptors.   Furthermore,  the lock is released either by an explicit
       LOCK_UN operation on any of these duplicate descriptors,  or  when  all
       such descriptors have been closed.

       A  process  may  only  hold one type of lock (shared or exclusive) on a
       file.  Subsequent flock(1,2)() calls on an already locked file(1,n) will  convert
       an existing lock to the new lock mode.

       Locks created by flock(1,2)() are preserved across an execve(2).

       A  shared  or  exclusive lock can be placed on a file(1,n) regardless of the
       mode in(1,8) which the file(1,n) was opened.

RETURN VALUE
       On success, zero is returned.  On error(8,n), -1 is returned, and  errno  is
       set(7,n,1 builtins) appropriately.

ERRORS
       EBADF  fd is not a not an open(2,3,n) file(1,n) descriptor.

       EINTR  While  waiting  to  acquire  a lock, the call was interrupted by
              delivery of a signal(2,7) caught by a handler.

       EINVAL operation is invalid.

       ENOLCK The kernel ran out of memory for allocating lock records.

       EWOULDBLOCK
              The file(1,n) is locked and the LOCK_NB flag was selected.

CONFORMING TO
       4.4BSD (the flock(1,2)(2) call first appeared  in(1,8)  4.2BSD).   A  version(1,3,5)  of
       flock(1,2)(2),  possibly  implemented  in(1,8) terms of fcntl(2), appears on most
       Unices.

NOTES
       flock(1,2)(2) does not lock files over NFS.  Use fcntl(2) instead: that does
       work  over  NFS,  given  a  sufficiently  recent version(1,3,5) of Linux and a
       server which supports locking.

       Since kernel 2.0, flock(1,2)(2) is implemented as a system call in(1,8)  its  own
       right  rather  than  being  emulated  in(1,8) the GNU C library as a call to
       fcntl(2).  This yields true BSD  semantics:  there  is  no  interaction
       between the types of lock placed by flock(1,2)(2) and fcntl(2), and flock(1,2)(2)
       does not detect deadlock.

       flock(1,2)(2) places advisory locks only; given suitable  permissions  on  a
       file(1,n),  a  process is free to ignore the use of flock(1,2)(2) and perform I/O
       on the file.

       flock(1,2)(2) and fcntl(2) locks have different semantics  with  respect  to
       forked processes and dup(2).

SEE ALSO
       close(2,7,n)(2), dup(2), execve(2), fcntl(2), fork(2), open(2,3,n)(2), lockf(3)

       There are also locks.txt and mandatory.txt in(1,8) /usr/src/linux/Documenta-
       tion.



Linux                             2002-04-24                          FLOCK(2)

References for this manual (incoming links)