1 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu) and
2 .\" and Copyright 2002 Michael Kerrisk
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Modified Fri Jan 31 16:26:07 1997 by Eric S. Raymond <esr@thyrsus.com>
27 .\" Modified Fri Dec 11 17:57:27 1998 by Jamie Lokier <jamie@imbolc.ucc.ie>
28 .\" Modified 24 Apr 2002 by Michael Kerrisk <mtk.manpages@gmail.com>
29 .\" Substantial rewrites and additions
30 .\" 2005-05-10 mtk, noted that lock conversions are not atomic.
32 .\" FIXME Maybe document LOCK_MAND, LOCK_RW, LOCK_READ, LOCK_WRITE
33 .\" which only have effect for SAMBA.
35 .TH FLOCK 2 2017-09-15 "Linux" "Linux Programmer's Manual"
37 flock \- apply or remove an advisory lock on an open file
40 .B #include <sys/file.h>
42 .BI "int flock(int " fd ", int " operation );
45 Apply or remove an advisory lock on the open file specified by
49 is one of the following:
54 More than one process may hold a shared lock for a given file
58 Place an exclusive lock.
59 Only one process may hold an exclusive lock for a given
63 Remove an existing lock held by this process.
68 may block if an incompatible lock is held by another process.
69 To make a nonblocking request, include
72 with any of the above operations.
74 A single file may not simultaneously have both shared and exclusive locks.
78 are associated with an open file description (see
80 This means that duplicate file descriptors (created by, for example,
84 refer to the same lock, and this lock may be modified
85 or released using any of these file descriptors.
86 Furthermore, the lock is released either by an explicit
88 operation on any of these duplicate file descriptors, or when all
89 such file descriptors have been closed.
93 (or similar) to obtain more than one file descriptor for the same file,
94 these file descriptors are treated independently by
96 An attempt to lock the file using one of these file descriptors
97 may be denied by a lock that the calling process has
98 already placed via another file descriptor.
100 A process may hold only one type of lock (shared or exclusive)
104 calls on an already locked file will convert an existing lock to the new
109 are preserved across an
112 A shared or exclusive lock can be placed on a file regardless of the
113 mode in which the file was opened.
115 On success, zero is returned.
116 On error, \-1 is returned, and
118 is set to indicate the error.
123 is not an open file descriptor.
126 While waiting to acquire a lock, the call was interrupted by
127 delivery of a signal caught by a handler; see
135 The kernel ran out of memory for allocating lock records.
138 The file is locked and the
144 call first appeared in 4.2BSD).
147 possibly implemented in terms of
149 appears on most UNIX systems.
153 is implemented as a system call in its own right rather
154 than being emulated in the GNU C library as a call to
156 With this implementation,
157 there is no interaction between the types of lock
164 does not detect deadlock.
165 (Note, however, that on some systems, such as the modern BSDs,
166 .\" E.g., according to the flock(2) man page, FreeBSD since at least 5.3
172 interact with one another.)
175 places advisory locks only; given suitable permissions on a file,
176 a process is free to ignore the use of
178 and perform I/O on the file.
183 locks have different semantics with respect to forked processes and
185 On systems that implement
191 will be different from those described in this manual page.
194 (shared to exclusive, or vice versa) is not guaranteed to be atomic:
195 the existing lock is first removed, and then a new lock is established.
196 Between these two steps,
197 a pending lock request by another process may be granted,
198 with the result that the conversion either blocks, or fails if
201 (This is the original BSD behavior,
202 and occurs on many other implementations.)
203 .\" Kernel 2.5.21 changed things a little: during lock conversion
204 .\" it is now the highest priority process that will get the lock -- mtk
206 In Linux kernels up to 2.6.11,
208 does not lock files over NFS
209 (i.e., the scope of locks was limited to the local system).
210 Instead, one could use
212 byte-range locking, which does work over NFS,
213 given a sufficiently recent version of
214 Linux and a server which supports locking.
216 Since Linux 2.6.12, NFS clients support
218 locks by emulating them as
220 byte-range locks on the entire file.
227 interact with one another over NFS.
228 It also means that in order to place an exclusive lock,
229 the file must be opened for writing.
232 .\" commit 5eebde23223aeb0ad2d9e3be6590ff8bbfab0fc2
233 the kernel supports a compatibility mode that allows
237 byte region locks) to be treated as local;
238 see the discussion of the
253 .I Documentation/filesystems/locks.txt
254 in the Linux kernel source tree
255 .RI ( Documentation/locks.txt