1flock(3UCB) SunOS/BSD Compatibility Library Functions flock(3UCB)
2
3
4
6 flock - apply or remove an advisory lock on an open file
7
9 /usr/ucb/cc[ flag ... ] file ...
10 #include <sys/file.h>
11
12 int flock( fd, operation)
13
14 int fd, operation;
15
16
18 flock() applies or removes an advisory lock on the file associated with
19 the file descriptor fd. The compatibility version of flock() has been
20 implemented on top of fcntl(2) locking. It does not provide complete
21 binary compatibility.
22
23
24 Advisory locks allow cooperating processes to perform consistent opera‐
25 tions on files, but do not guarantee exclusive access (that is, pro‐
26 cesses may still access files without using advisory locks, possibly
27 resulting in inconsistencies).
28
29
30 The locking mechanism allows two types of locks: shared locks and
31 exclusive locks. More than one process may hold a shared lock for a
32 file at any given time, but multiple exclusive, or both shared and
33 exclusive, locks may not exist simultaneously on a file.
34
35
36 A lock is applied by specifying an operation parameter LOCK_SH for a
37 shared lock or LOCK_EX for an exclusive lock. The operation parameter
38 may be ORed with LOCK_NB to make the operation non-blocking. To unlock
39 an existing lock, the operation should be LOCK_UN.
40
41
42 Read permission is required on a file to obtain a shared lock, and
43 write permission is required to obtain an exclusive lock. Locking a
44 segment that is already locked by the calling process causes the old
45 lock type to be removed and the new lock type to take effect.
46
47
48 Requesting a lock on an object that is already locked normally causes
49 the caller to block until the lock may be acquired. If LOCK_NB is
50 included in operation, then this will not happen; instead, the call
51 will fail and the error EWOULDBLOCK will be returned.
52
54 flock() returns:
55
56 0 on success.
57
58
59 −1 on failure and sets errno to indicate the error.
60
61
63 EBADF The argument fd is an invalid descriptor.
64
65
66 EINVAL operation is not a valid argument.
67
68
69 EOPNOTSUPP The argument fd refers to an object other than a file.
70
71
72 EWOULDBLOCK The file is locked and the LOCK_NB option was speci‐
73 fied.
74
75
77 lockd(1M), chmod(2), close(2), dup(2), exec(2), fcntl(2), fork(2),
78 open(2), lockf(3C)
79
81 Use of these interfaces should be restricted to only applications writ‐
82 ten on BSD platforms. Use of these interfaces with any of the system
83 libraries or in multi-thread applications is unsupported.
84
85
86 Locks are on files, not file descriptors. That is, file descriptors
87 duplicated through dup(2) or fork(2) do not result in multiple
88 instances of a lock, but rather multiple references to a single lock.
89 If a process holding a lock on a file forks and the child explicitly
90 unlocks the file, the parent will lose its lock. Locks are not inher‐
91 ited by a child process.
92
93
94 Processes blocked awaiting a lock may be awakened by signals.
95
96
97 Mandatory locking may occur, depending on the mode bits of the file.
98 See chmod(2).
99
100
101 Locks obtained through the flock() mechanism under SunOS 4.1 were known
102 only within the system on which they were placed. This is no longer
103 true.
104
105
106
107SunOS 5.11 30 Oct 2007 flock(3UCB)