1FLOCK(2) Linux Programmer's Manual FLOCK(2)
2
3
4
6 flock - apply or remove an advisory lock on an open file
7
9 #include <sys/file.h>
10
11 int flock(int fd, int operation);
12
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 nonblocking 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 description
33 (see open(2)). This means that duplicate file descriptors (created by,
34 for example, fork(2) or dup(2)) refer to the same lock, and this lock
35 may be modified or released using any of these file descriptors. Fur‐
36 thermore, the lock is released either by an explicit LOCK_UN operation
37 on any of these duplicate file descriptors, or when all such file
38 descriptors have been closed.
39
40 If a process uses open(2) (or similar) to obtain more than one file
41 descriptor for the same file, these file descriptors are treated inde‐
42 pendently by flock(). An attempt to lock the file using one of these
43 file descriptors may be denied by a lock that the calling process has
44 already placed via another file descriptor.
45
46 A process may hold only one type of lock (shared or exclusive) on a
47 file. Subsequent flock() calls on an already locked file will convert
48 an existing lock to the new lock mode.
49
50 Locks created by flock() are preserved across an execve(2).
51
52 A shared or exclusive lock can be placed on a file regardless of the
53 mode in which the file was opened.
54
56 On success, zero is returned. On error, -1 is returned, and errno is
57 set appropriately.
58
60 EBADF fd is not an open file descriptor.
61
62 EINTR While waiting to acquire a lock, the call was interrupted by
63 delivery of a signal caught by a handler; see signal(7).
64
65 EINVAL operation is invalid.
66
67 ENOLCK The kernel ran out of memory for allocating lock records.
68
69 EWOULDBLOCK
70 The file is locked and the LOCK_NB flag was selected.
71
73 4.4BSD (the flock() call first appeared in 4.2BSD). A version of
74 flock(), possibly implemented in terms of fcntl(2), appears on most
75 UNIX systems.
76
78 Since kernel 2.0, flock() is implemented as a system call in its own
79 right rather than being emulated in the GNU C library as a call to
80 fcntl(2). With this implementation, there is no interaction between
81 the types of lock placed by flock() and fcntl(2), and flock() does not
82 detect deadlock. (Note, however, that on some systems, such as the
83 modern BSDs, flock() and fcntl(2) locks do interact with one another.)
84
85 flock() places advisory locks only; given suitable permissions on a
86 file, a process is free to ignore the use of flock() and perform I/O on
87 the file.
88
89 flock() and fcntl(2) locks have different semantics with respect to
90 forked processes and dup(2). On systems that implement flock() using
91 fcntl(2), the semantics of flock() will be different from those
92 described in this manual page.
93
94 Converting a lock (shared to exclusive, or vice versa) is not guaran‐
95 teed to be atomic: the existing lock is first removed, and then a new
96 lock is established. Between these two steps, a pending lock request
97 by another process may be granted, with the result that the conversion
98 either blocks, or fails if LOCK_NB was specified. (This is the origi‐
99 nal BSD behavior, and occurs on many other implementations.)
100
101 NFS details
102 In Linux kernels up to 2.6.11, flock() does not lock files over NFS
103 (i.e., the scope of locks was limited to the local system). Instead,
104 one could use fcntl(2) byte-range locking, which does work over NFS,
105 given a sufficiently recent version of Linux and a server which sup‐
106 ports locking.
107
108 Since Linux 2.6.12, NFS clients support flock() locks by emulating them
109 as fcntl(2) byte-range locks on the entire file. This means that
110 fcntl(2) and flock() locks do interact with one another over NFS. It
111 also means that in order to place an exclusive lock, the file must be
112 opened for writing.
113
114 Since Linux 2.6.37, the kernel supports a compatibility mode that
115 allows flock() locks (and also fcntl(2) byte region locks) to be
116 treated as local; see the discussion of the local_lock option in
117 nfs(5).
118
120 flock(1), close(2), dup(2), execve(2), fcntl(2), fork(2), open(2),
121 lockf(3), lslocks(8)
122
123 Documentation/filesystems/locks.txt in the Linux kernel source tree
124 (Documentation/locks.txt in older kernels)
125
127 This page is part of release 5.07 of the Linux man-pages project. A
128 description of the project, information about reporting bugs, and the
129 latest version of this page, can be found at
130 https://www.kernel.org/doc/man-pages/.
131
132
133
134Linux 2017-09-15 FLOCK(2)