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@gmx.net>
29 .\" Substantial rewrites and additions
30 .\" 2005-05-10 mtk, noted that lock conversions are not atomic.
32 .TH FLOCK 2 2002-04-24 "Linux" "Linux Programmer's Manual"
34 flock \- apply or remove an advisory lock on an open file
36 .B #include <sys/file.h>
38 .BI "int flock(int " fd ", int " operation );
40 Apply or remove an advisory lock on the open file specified by
44 is one of the following:
49 More than one process may hold a shared lock for a given file
53 Place an exclusive lock.
54 Only one process may hold an exclusive lock for a given
58 Remove an existing lock held by this process.
63 may block if an incompatible lock is held by another process.
64 To make a non-blocking request, include
68 with any of the above operations.
70 A single file may not simultaneously have both shared and exclusive locks.
74 are associated with an open file table entry.
75 This means that duplicate file descriptors (created by, for example,
79 refer to the same lock, and this lock may be modified
80 or released using any of these descriptors.
81 Furthermore, the lock is released either by an explicit
83 operation on any of these duplicate descriptors, or when all
84 such descriptors have been closed.
88 (or similar) to obtain more than one descriptor for the same file,
89 these descriptors are treated independently by
91 An attempt to lock the file using one of these file descriptors
92 may be denied by a lock that the calling process has
93 already placed via another descriptor.
95 A process may only hold one type of lock (shared or exclusive)
99 calls on an already locked file will convert an existing lock to the new
104 are preserved across an
107 A shared or exclusive lock can be placed on a file regardless of the
108 mode in which the file was opened.
110 On success, zero is returned.
111 On error, \-1 is returned, and
113 is set appropriately.
118 is not a not an open file descriptor.
121 While waiting to acquire a lock, the call was interrupted by
122 delivery of a signal caught by a handler.
129 The kernel ran out of memory for allocating lock records.
132 The file is locked and the
138 call first appeared in 4.2BSD).
141 possibly implemented in terms of
143 appears on most Unix systems.
146 does not lock files over NFS.
149 instead: that does work over NFS, given a sufficiently recent version of
150 Linux and a server which supports locking.
154 is implemented as a system call in its own right rather
155 than being emulated in the GNU C library as a call to
157 This yields true BSD semantics:
158 there is no interaction between the types of lock
165 does not detect deadlock.
168 places advisory locks only; given suitable permissions on a file,
169 a process is free to ignore the use of
171 and perform I/O on the file.
176 locks have different semantics with respect to forked processes and
178 On systems that implement
184 will be different from those described in this manual page.
187 (shared to exclusive, or vice versa) is not guaranteed to be atomic:
188 the existing lock is first removed, and then a new lock is established.
189 Between these two steps,
190 a pending lock request by another process may be granted,
191 with the result that the conversion either blocks, or fails if
194 (This is the original BSD behavior,
195 and occurs on many other implementations.)
196 .\" Kernel 2.5.21 changed things a little: during lock conversion
197 .\" it is now the highest priority process that will get the lock -- mtk
208 .I Documentation/locks.txt
210 .I Documentation/mandatory.txt
211 in the kernel source.