1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright 1993 Rickard E. Faith (faith@cs.unc.edu) and
4 .\" and Copyright 2002 Michael Kerrisk
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" 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.
34 .TH FLOCK 2 2009-07-25 "Linux" "Linux Programmer's Manual"
36 flock \- apply or remove an advisory lock on an open file
38 .B #include <sys/file.h>
40 .BI "int flock(int " fd ", int " operation );
42 Apply or remove an advisory lock on the open file specified by
46 is one of the following:
51 More than one process may hold a shared lock for a given file
55 Place an exclusive lock.
56 Only one process may hold an exclusive lock for a given
60 Remove an existing lock held by this process.
65 may block if an incompatible lock is held by another process.
66 To make a nonblocking request, include
69 with any of the above operations.
71 A single file may not simultaneously have both shared and exclusive locks.
75 are associated with an open file table entry.
76 This means that duplicate file descriptors (created by, for example,
80 refer to the same lock, and this lock may be modified
81 or released using any of these descriptors.
82 Furthermore, the lock is released either by an explicit
84 operation on any of these duplicate descriptors, or when all
85 such descriptors have been closed.
89 (or similar) to obtain more than one descriptor for the same file,
90 these descriptors are treated independently by
92 An attempt to lock the file using one of these file descriptors
93 may be denied by a lock that the calling process has
94 already placed via another descriptor.
96 A process may only hold one type of lock (shared or exclusive)
100 calls on an already locked file will convert an existing lock to the new
105 are preserved across an
108 A shared or exclusive lock can be placed on a file regardless of the
109 mode in which the file was opened.
111 On success, zero is returned.
112 On error, \-1 is returned, and
114 is set appropriately.
119 is not an open file descriptor.
122 While waiting to acquire a lock, the call was interrupted by
123 delivery of a signal caught by a handler; see
131 The kernel ran out of memory for allocating lock records.
134 The file is locked and the
140 call first appeared in 4.2BSD).
143 possibly implemented in terms of
145 appears on most UNIX systems.
148 does not lock files over NFS.
151 instead: that does work over NFS, given a sufficiently recent version of
152 Linux and a server which supports locking.
156 is implemented as a system call in its own right rather
157 than being emulated in the GNU C library as a call to
159 This yields true BSD semantics:
160 there is no interaction between the types of lock
167 does not detect deadlock.
170 places advisory locks only; given suitable permissions on a file,
171 a process is free to ignore the use of
173 and perform I/O on the file.
178 locks have different semantics with respect to forked processes and
180 On systems that implement
186 will be different from those described in this manual page.
189 (shared to exclusive, or vice versa) is not guaranteed to be atomic:
190 the existing lock is first removed, and then a new lock is established.
191 Between these two steps,
192 a pending lock request by another process may be granted,
193 with the result that the conversion either blocks, or fails if
196 (This is the original BSD behavior,
197 and occurs on many other implementations.)
198 .\" Kernel 2.5.21 changed things a little: during lock conversion
199 .\" it is now the highest priority process that will get the lock -- mtk
210 .I Documentation/filesystem/locks.txt
212 .RI ( Documentation/locks.txt