1lockf(3C) Standard C Library Functions lockf(3C)
2
3
4
6 lockf - record locking on files
7
9 #include <unistd.h>
10
11 int lockf(int fildes, int function, off_t size);
12
13
15 The lockf() function allows sections of a file to be locked; advisory
16 or mandatory write locks depending on the mode bits of the file (see
17 chmod(2)). Calls to lockf() from other threads that attempt to lock the
18 locked file section will either return an error value or be put to
19 sleep until the resource becomes unlocked. All the locks for a process
20 are removed when the process terminates. See fcntl(2) for more informa‐
21 tion about record locking.
22
23
24 The fildes argument is an open file descriptor. The file descriptor
25 must have O_WRONLY or O_RDWR permission in order to establish locks
26 with this function call.
27
28
29 The function argument is a control value that specifies the action to
30 be taken. The permissible values for function are defined in <unistd.h>
31 as follows:
32
33 #define F_ULOCK 0 /* unlock previously locked section */
34 #define F_LOCK 1 /* lock section for exclusive use */
35 #define F_TLOCK 2 /* test & lock section for exclusive use */
36 #define F_TEST 3 /* test section for other locks */
37
38
39
40 All other values of function are reserved for future extensions and
41 will result in an error if not implemented.
42
43
44 F_TEST is used to detect if a lock by another process is present on the
45 specified section. F_LOCK and F_TLOCK both lock a section of a file if
46 the section is available. F_ULOCK removes locks from a section of the
47 file.
48
49
50 The size argument is the number of contiguous bytes to be locked or
51 unlocked. The resource to be locked or unlocked starts at the current
52 offset in the file and extends forward for a positive size and backward
53 for a negative size (the preceding bytes up to but not including the
54 current offset). If size is zero, the section from the current offset
55 through the largest file offset is locked (that is, from the current
56 offset through the present or any future end-of-file). An area need not
57 be allocated to the file in order to be locked as such locks may exist
58 past the end-of-file.
59
60
61 The sections locked with F_LOCK or F_TLOCK may, in whole or in part,
62 contain or be contained by a previously locked section for the same
63 process. Locked sections will be unlocked starting at the point of the
64 offset through size bytes or to the end of file if size is (off_t) 0.
65 When this situation occurs, or if this situation occurs in adjacent
66 sections, the sections are combined into a single section. If the
67 request requires that a new element be added to the table of active
68 locks and this table is already full, an error is returned, and the new
69 section is not locked.
70
71
72 F_LOCK and F_TLOCK requests differ only by the action taken if the
73 resource is not available. F_LOCK blocks the calling thread until the
74 resource is available. F_TLOCK causes the function to return −1 and set
75 errno to EAGAIN if the section is already locked by another process.
76
77
78 File locks are released on first close by the locking process of any
79 file descriptor for the file.
80
81
82 F_ULOCK requests may, in whole or in part, release one or more locked
83 sections controlled by the process. When sections are not fully
84 released, the remaining sections are still locked by the process.
85 Releasing the center section of a locked section requires an additional
86 element in the table of active locks. If this table is full, an errno
87 is set to EDEADLK and the requested section is not released.
88
89
90 An F_ULOCK request in which size is non-zero and the offset of the last
91 byte of the requested section is the maximum value for an object of
92 type off_t, when the process has an existing lock in which size is 0
93 and which includes the last byte of the requested section, will be
94 treated as a request to unlock from the start of the requested section
95 with a size equal to 0. Otherwise, an F_ULOCK request will attempt to
96 unlock only the requested section.
97
98
99 A potential for deadlock occurs if the threads of a process controlling
100 a locked resource is put to sleep by requesting another process's
101 locked resource. Thus calls to lockf() or fcntl(2) scan for a deadlock
102 prior to sleeping on a locked resource. An error return is made if
103 sleeping on the locked resource would cause a deadlock.
104
105
106 Sleeping on a resource is interrupted with any signal. The alarm(2)
107 function may be used to provide a timeout facility in applications that
108 require this facility.
109
111 Upon successful completion, 0 is returned. Otherwise, −1 is returned
112 and errno is set to indicate the error.
113
115 The lockf() function will fail if:
116
117 EBADF The fildes argument is not a valid open file
118 descriptor; or function is F_LOCK or F_TLOCK and
119 fildes is not a valid file descriptor open for
120 writing.
121
122
123 EACCES or EAGAIN The function argument is F_TLOCK or F_TEST and the
124 section is already locked by another process.
125
126
127 EDEADLK The function argument is F_LOCK and a deadlock is
128 detected.
129
130
131 EINTR A signal was caught during execution of the func‐
132 tion.
133
134
135 ECOMM The fildes argument is on a remote machine and the
136 link to that machine is no longer active.
137
138
139 EINVAL The function argument is not one of F_LOCK,
140 F_TLOCK, F_TEST, or F_ULOCK; or size plus the cur‐
141 rent file offset is less than 0.
142
143
144 EOVERFLOW The offset of the first, or if size is not 0 then
145 the last, byte in the requested section cannot be
146 represented correctly in an object of type off_t.
147
148
149
150 The lockf() function may fail if:
151
152 EAGAIN The function argument is F_LOCK or F_TLOCK and
153 the file is mapped with mmap(2).
154
155
156 EDEADLK or ENOLCK The function argument is F_LOCK, F_TLOCK, or
157 F_ULOCK and the request would cause the number
158 of locks to exceed a system-imposed limit.
159
160
161 EOPNOTSUPP or EINVAL The locking of files of the type indicated by
162 the fildes argument is not supported.
163
164
166 Record-locking should not be used in combination with the fopen(3C),
167 fread(3C), fwrite(3C) and other stdio functions. Instead, the more
168 primitive, non-buffered functions (such as open(2)) should be used.
169 Unexpected results may occur in processes that do buffering in the user
170 address space. The process may later read/write data which is/was
171 locked. The stdio functions are the most common source of unexpected
172 buffering.
173
174
175 The alarm(2) function may be used to provide a timeout facility in
176 applications requiring it.
177
178
179 The lockf() function has a transitional interface for 64-bit file off‐
180 sets. See lf64(5).
181
183 See attributes(5) for descriptions of the following attributes:
184
185
186
187
188 ┌─────────────────────────────┬─────────────────────────────┐
189 │ ATTRIBUTE TYPE │ ATTRIBUTE VALUE │
190 ├─────────────────────────────┼─────────────────────────────┤
191 │Interface Stability │Standard │
192 ├─────────────────────────────┼─────────────────────────────┤
193 │MT-Level │MT-Safe │
194 └─────────────────────────────┴─────────────────────────────┘
195
197 Intro(2), alarm(2), chmod(2), close(2), creat(2), fcntl(2), mmap(2),
198 open(2), read(2), write(2), attributes(5), lf64(5), standards(5)
199
200
201
202SunOS 5.11 10 Apr 2002 lockf(3C)