1FLOCK(2)                   Linux Programmer's Manual                  FLOCK(2)
2
3
4

NAME

6       flock - apply or remove an advisory lock on an open file
7

SYNOPSIS

9       #include <sys/file.h>
10
11       int flock(int fd, int operation);
12

DESCRIPTION

14       Apply or remove an advisory lock on the open file specified by fd.  The
15       parameter 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 non-blocking 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 table entry.
33       This means that duplicate file descriptors (created  by,  for  example,
34       fork(2)  or  dup(2)) refer to the same lock, and this lock may be modi‐
35       fied or released using any of these descriptors.  Furthermore, the lock
36       is  released  either  by  an explicit LOCK_UN operation on any of these
37       duplicate descriptors, or when all such descriptors have been closed.
38
39       If a process uses open(2) (or similar) to obtain more than one descrip‐
40       tor  for  the same file, these descriptors are treated independently by
41       flock().  An attempt to lock the file using one of these file  descrip‐
42       tors  may  be  denied  by  a  lock that the calling process has already
43       placed via another descriptor.
44
45       A process may only hold one type of lock (shared  or  exclusive)  on  a
46       file.   Subsequent flock() calls on an already locked file will convert
47       an existing lock to the new lock mode.
48
49       Locks created by flock() are preserved across an execve(2).
50
51       A shared or exclusive lock can be placed on a file  regardless  of  the
52       mode in which the file was opened.
53

RETURN VALUE

55       On  success,  zero is returned.  On error, -1 is returned, and errno is
56       set appropriately.
57

ERRORS

59       EBADF  fd is not a not an open file descriptor.
60
61       EINTR  While waiting to acquire a lock, the  call  was  interrupted  by
62              delivery of a signal caught by a handler.
63
64       EINVAL operation is invalid.
65
66       ENOLCK The kernel ran out of memory for allocating lock records.
67
68       EWOULDBLOCK
69              The file is locked and the LOCK_NB flag was selected.
70

CONFORMING TO

72       4.4BSD  (the  flock(2)  call  first  appeared in 4.2BSD).  A version of
73       flock(2), possibly implemented in terms of fcntl(2),  appears  on  most
74       Unix systems.
75

NOTES

77       flock(2) does not lock files over NFS.  Use fcntl(2) instead: that does
78       work over NFS, given a sufficiently  recent  version  of  Linux  and  a
79       server which supports locking.
80
81       Since  kernel  2.0, flock(2) is implemented as a system call in its own
82       right rather than being emulated in the GNU C  library  as  a  call  to
83       fcntl(2).   This  yields  true  BSD  semantics: there is no interaction
84       between the types of lock placed by flock(2) and fcntl(2), and flock(2)
85       does not detect deadlock.
86
87       flock(2)  places  advisory  locks only; given suitable permissions on a
88       file, a process is free to ignore the use of flock(2) and  perform  I/O
89       on the file.
90
91       flock(2)  and  fcntl(2)  locks have different semantics with respect to
92       forked processes and dup(2).  On systems that implement  flock()  using
93       fcntl(),  the  semantics  of  flock()  will  be  different  from  those
94       described in this manual page.
95
96       Converting a lock (shared to exclusive, or vice versa) is  not  guaran‐
97       teed  to  be atomic: the existing lock is first removed, and then a new
98       lock is established.  Between these two steps, a pending  lock  request
99       by  another process may be granted, with the result that the conversion
100       either blocks, or fails if LOCK_NB was specified.  (This is the  origi‐
101       nal BSD behaviour, and occurs on many other implementations.)
102

SEE ALSO

104       close(2), dup(2), execve(2), fcntl(2), fork(2), open(2), lockf(3)
105
106       There are also locks.txt and mandatory.txt in /usr/src/linux/Documenta‐
107       tion.
108
109
110
111Linux                             2002-04-24                          FLOCK(2)
Impressum