1FLOCK(2)                   Linux Programmer's Manual                  FLOCK(2)
2
3
4

NAME

6       flock - apply or remove an advisory lock on an open file
7

SYNOPSIS

9       #include <sys/file.h>
10
11       int flock(int fd, int operation);
12

DESCRIPTION

14       Apply or remove an advisory lock on the open file specified by fd.  The
15       argument operation is one of the following:
16
17           LOCK_SH  Place a shared lock.  More than one  process  may  hold  a
18                    shared lock for a given file at a given time.
19
20           LOCK_EX  Place  an  exclusive  lock.   Only one process may hold an
21                    exclusive lock for a given file at a given time.
22
23           LOCK_UN  Remove an existing lock held by this process.
24
25       A call to flock() may block if an incompatible lock is held by  another
26       process.   To  make  a non-blocking request, include LOCK_NB (by ORing)
27       with any of the above operations.
28
29       A single file may not simultaneously have  both  shared  and  exclusive
30       locks.
31
32       Locks  created by flock() are associated with an open file table entry.
33       This means that duplicate file descriptors (created  by,  for  example,
34       fork(2)  or  dup(2)) refer to the same lock, and this lock may be modi‐
35       fied or released using any of these descriptors.  Furthermore, the lock
36       is  released  either  by  an explicit LOCK_UN operation on any of these
37       duplicate descriptors, or when all such descriptors have been closed.
38
39       If a process uses open(2) (or similar) to obtain more than one descrip‐
40       tor  for  the same file, these descriptors are treated independently by
41       flock().  An attempt to lock the file using one of these file  descrip‐
42       tors  may  be  denied  by  a  lock that the calling process has already
43       placed via another descriptor.
44
45       A process may only hold one type of lock (shared  or  exclusive)  on  a
46       file.   Subsequent flock() calls on an already locked file will convert
47       an existing lock to the new lock mode.
48
49       Locks created by flock() are preserved across an execve(2).
50
51       A shared or exclusive lock can be placed on a file  regardless  of  the
52       mode in which the file was opened.
53

RETURN VALUE

55       On  success,  zero is returned.  On error, -1 is returned, and errno is
56       set appropriately.
57

ERRORS

59       EBADF  fd is not an open file descriptor.
60
61       EINTR  While waiting to acquire a lock, the  call  was  interrupted  by
62              delivery of a signal caught by a handler; see signal(7).
63
64       EINVAL operation is invalid.
65
66       ENOLCK The kernel ran out of memory for allocating lock records.
67
68       EWOULDBLOCK
69              The file is locked and the LOCK_NB flag was selected.
70

CONFORMING TO

72       4.4BSD  (the  flock()  call  first  appeared  in 4.2BSD).  A version of
73       flock(), possibly implemented in terms of  fcntl(2),  appears  on  most
74       Unix systems.
75

NOTES

77       flock()  does not lock files over NFS.  Use fcntl(2) instead: that does
78       work over NFS, given a sufficiently  recent  version  of  Linux  and  a
79       server which supports locking.
80
81       Since  kernel  2.0,  flock() is implemented as a system call in its own
82       right rather than being emulated in the GNU C  library  as  a  call  to
83       fcntl(2).   This  yields  true  BSD  semantics: there is no interaction
84       between the types of lock placed by flock() and fcntl(2),  and  flock()
85       does not detect deadlock.
86
87       flock()  places  advisory  locks  only; given suitable permissions on a
88       file, a process is free to ignore the use of flock() and perform I/O on
89       the file.
90
91       flock()  and  fcntl(2)  locks  have different semantics with respect to
92       forked processes and dup(2).  On systems that implement  flock()  using
93       fcntl(2),  the  semantics  of  flock()  will  be  different  from those
94       described in this manual page.
95
96       Converting a lock (shared to exclusive, or vice versa) is  not  guaran‐
97       teed  to  be atomic: the existing lock is first removed, and then a new
98       lock is established.  Between these two steps, a pending  lock  request
99       by  another process may be granted, with the result that the conversion
100       either blocks, or fails if LOCK_NB was specified.  (This is the  origi‐
101       nal BSD behavior, and occurs on many other implementations.)
102

SEE ALSO

104       close(2), dup(2), execve(2), fcntl(2), fork(2), open(2), lockf(3)
105
106       See also Documentation/filesystem/locks.txt in the kernel source (Docu‐
107       mentation/locks.txt in older kernels).
108

COLOPHON

110       This page is part of release 3.22 of the Linux  man-pages  project.   A
111       description  of  the project, and information about reporting bugs, can
112       be found at http://www.kernel.org/doc/man-pages/.
113
114
115
116Linux                             2009-07-25                          FLOCK(2)
Impressum