Commit | Line | Data |
---|---|---|
88b3ccf2 KB |
1 | .\" Copyright (c) 1983 The Regents of the University of California. |
2 | .\" All rights reserved. | |
619ee695 | 3 | .\" |
91cff1e1 | 4 | .\" %sccs.include.redist.man% |
88b3ccf2 | 5 | .\" |
91cff1e1 | 6 | .\" @(#)flock.2 6.6 (Berkeley) %G% |
619ee695 | 7 | .\" |
9b1f9fbd | 8 | .TH FLOCK 2 "" |
619ee695 KM |
9 | .UC 5 |
10 | .SH NAME | |
11 | flock \- apply or remove an advisory lock on an open file | |
12 | .SH SYNOPSIS | |
13 | .nf | |
14 | .ft B | |
15 | #include <sys/file.h> | |
16 | .PP | |
17 | .ft B | |
18 | .DT | |
19 | #define LOCK_SH 1 /* shared lock */ | |
20 | #define LOCK_EX 2 /* exclusive lock */ | |
21 | #define LOCK_NB 4 /* don't block when locking */ | |
22 | #define LOCK_UN 8 /* unlock */ | |
23 | .PP | |
24 | .ft B | |
25 | flock(fd, operation) | |
26 | int fd, operation; | |
27 | .fi | |
28 | .SH DESCRIPTION | |
29 | .I Flock | |
30 | applies or removes an | |
31 | .I advisory | |
32 | lock on the file associated with the file descriptor | |
33 | .IR fd . | |
34 | A lock is applied by specifying an | |
35 | .I operation | |
38edb05c | 36 | parameter that is the inclusive or of |
619ee695 KM |
37 | LOCK_SH or LOCK_EX and, possibly, LOCK_NB. To unlock |
38 | an existing lock | |
39 | .I operation | |
40 | should be LOCK_UN. | |
41 | .PP | |
42 | Advisory locks allow cooperating processes to perform | |
43 | consistent operations on files, but do not guarantee | |
4decc76b | 44 | consistency (i.e., processes may still access files |
619ee695 KM |
45 | without using advisory locks possibly resulting in |
46 | inconsistencies). | |
47 | .PP | |
48 | The locking mechanism allows two types of locks: | |
49 | .I shared | |
50 | locks and | |
51 | .I exclusive | |
52 | locks. | |
53 | At any time multiple shared locks may be applied to a file, | |
54 | but at no time are multiple exclusive, or both shared and exclusive, | |
55 | locks allowed simultaneously on a file. | |
56 | .PP | |
57 | A shared lock may be | |
58 | .I upgraded | |
59 | to an exclusive lock, and vice versa, simply by specifying | |
60 | the appropriate lock type; this results in the previous | |
61 | lock being released and the new lock applied (possibly | |
62 | after other processes have gained and released the lock). | |
63 | .PP | |
38edb05c | 64 | Requesting a lock on an object that is already locked |
19ecbc2b | 65 | normally causes the caller to be blocked until the lock may be |
619ee695 KM |
66 | acquired. If LOCK_NB is included in |
67 | .IR operation , | |
68 | then this will not happen; instead the call will fail and | |
69 | the error EWOULDBLOCK will be returned. | |
70 | .SH NOTES | |
71 | Locks are on files, not file descriptors. That is, file descriptors | |
72 | duplicated through | |
73 | .IR dup (2) | |
74 | or | |
75 | .IR fork (2) | |
76 | do not result in multiple instances of a lock, but rather multiple | |
77 | references to a single lock. If a process holding a lock on a file | |
78 | forks and the child explicitly unlocks the file, the parent will | |
79 | lose its lock. | |
80 | .PP | |
81 | Processes blocked awaiting a lock may be awakened by signals. | |
82 | .SH "RETURN VALUE | |
83 | Zero is returned if the operation was successful; | |
84 | on an error a \-1 is returned and an error code is left in | |
85 | the global location \fIerrno\fP. | |
86 | .SH "ERRORS | |
87 | The \fIflock\fP call fails if: | |
88 | .TP 20 | |
89 | [EWOULDBLOCK] | |
90 | The file is locked and the LOCK_NB option was specified. | |
91 | .TP 20 | |
92 | [EBADF] | |
93 | The argument \fIfd\fP is an invalid descriptor. | |
94 | .TP 20 | |
95 | [EINVAL] | |
96 | The argument \fIfd\fP refers to an object other than a file. | |
97 | .SH "SEE ALSO" | |
98 | open(2), close(2), dup(2), execve(2), fork(2) |