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 ex‐
21 clusive 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 de‐
38 scriptors have been closed.
39
40 If a process uses open(2) (or similar) to obtain more than one file de‐
41 scriptor for the same file, these file descriptors are treated indepen‐
42 dently by flock(). An attempt to lock the file using one of these file
43 descriptors may be denied by a lock that the calling process has al‐
44 ready 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 to indicate the error.
58
60 EBADF fd is not an open file descriptor.
61
62 EINTR While waiting to acquire a lock, the call was interrupted by de‐
63 livery 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 fc‐
80 ntl(2). With this implementation, there is no interaction between the
81 types of lock placed by flock() and fcntl(2), and flock() does not de‐
82 tect deadlock. (Note, however, that on some systems, such as the mod‐
83 ern 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 de‐
92 scribed 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 fc‐
110 ntl(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 al‐
115 lows flock() locks (and also fcntl(2) byte region locks) to be treated
116 as local; see the discussion of the local_lock option in nfs(5).
117
118 CIFS details
119 In Linux kernels up to 5.4, flock() is not propagated over SMB. A file
120 with such locks will not appear locked for remote clients.
121
122 Since Linux 5.5, flock() locks are emulated with SMB byte-range locks
123 on the entire file. Similarly to NFS, this means that fcntl(2) and
124 flock() locks interact with one another. Another important side-effect
125 is that the locks are not advisory anymore: any IO on a locked file
126 will always fail with EACCES when done from a separate file descriptor.
127 This difference originates from the design of locks in the SMB proto‐
128 col, which provides mandatory locking semantics.
129
130 Remote and mandatory locking semantics may vary with SMB protocol,
131 mount options and server type. See mount.cifs(8) for additional infor‐
132 mation.
133
135 flock(1), close(2), dup(2), execve(2), fcntl(2), fork(2), open(2),
136 lockf(3), lslocks(8)
137
138 Documentation/filesystems/locks.txt in the Linux kernel source tree
139 (Documentation/locks.txt in older kernels)
140
142 This page is part of release 5.13 of the Linux man-pages project. A
143 description of the project, information about reporting bugs, and the
144 latest version of this page, can be found at
145 https://www.kernel.org/doc/man-pages/.
146
147
148
149Linux 2021-03-22 FLOCK(2)