1LOCKF(P)                   POSIX Programmer's Manual                  LOCKF(P)
2
3
4

NAME

6       lockf - record locking on files
7

SYNOPSIS

9       #include <unistd.h>
10
11       int lockf(int fildes, int function, off_t size);
12
13

DESCRIPTION

15       The  lockf()  function shall lock sections of a file with advisory-mode
16       locks. Calls to lockf() from other threads which attempt  to  lock  the
17       locked  file  section shall either return an error value or block until
18       the section becomes unlocked. All the locks for a process  are  removed
19       when  the process terminates. Record locking with lockf() shall be sup‐
20       ported for regular files and may be supported for other files.
21
22       The fildes argument is an open file descriptor.  To  establish  a  lock
23       with this function, the file descriptor shall be opened with write-only
24       permission (O_WRONLY) or with read/write permission (O_RDWR).
25
26       The function argument is a control value which specifies the action  to
27       be taken. The permissible values for function are defined in <unistd.h>
28       as follows:
29
30               Function  Description
31               F_ULOCK   Unlock locked sections.
32               F_LOCK    Lock a section for exclusive use.
33               F_TLOCK   Test and lock a section for exclusive use.
34               F_TEST    Test a section for locks by other processes.
35
36       F_TEST shall detect if a lock by another  process  is  present  on  the
37       specified section.
38
39       F_LOCK  and  F_TLOCK shall both lock a section of a file if the section
40       is available.
41
42       F_ULOCK shall remove locks from a section of the file.
43
44       The size argument is the number of contiguous bytes  to  be  locked  or
45       unlocked.  The  section  to be locked or unlocked starts at the current
46       offset in the file and extends forward for a positive size or  backward
47       for  a  negative  size (the preceding bytes up to but not including the
48       current offset). If size is 0, the  section  from  the  current  offset
49       through the largest possible file offset shall be locked (that is, from
50       the current offset through the present or any future  end-of-file).  An
51       area  need  not be allocated to the file to be locked because locks may
52       exist past the end-of-file.
53
54       The sections locked with F_LOCK or F_TLOCK may, in whole  or  in  part,
55       contain  or  be  contained  by a previously locked section for the same
56       process. When this occurs, or if adjacent locked sections would  occur,
57       the  sections  shall  be  combined into a single locked section. If the
58       request would cause the number of  locks  to  exceed  a  system-imposed
59       limit, the request shall fail.
60
61       F_LOCK and F_TLOCK requests differ only by the action taken if the sec‐
62       tion is not available. F_LOCK shall block the calling thread until  the
63       section  is  available. F_TLOCK shall cause the function to fail if the
64       section is already locked by another process.
65
66       File locks shall be released on first close by the locking  process  of
67       any file descriptor for the file.
68
69       F_ULOCK  requests  may  release  (wholly or in part) one or more locked
70       sections controlled by the process. Locked sections shall  be  unlocked
71       starting  at  the current file offset through size bytes or to the end-
72       of-file if size is (off_t)0.  When all  of  a  locked  section  is  not
73       released (that is, when the beginning or end of the area to be unlocked
74       falls within a locked section), the remaining portions of that  section
75       shall  remain  locked by the process. Releasing the center portion of a
76       locked section shall cause the remaining locked beginning and end  por‐
77       tions  to  become  two  separate  locked sections. If the request would
78       cause the number of locks in the  system  to  exceed  a  system-imposed
79       limit, the request shall fail.
80
81       A potential for deadlock occurs if the threads of a process controlling
82       a locked section are blocked by accessing another process' locked  sec‐
83       tion.  If  the  system detects that deadlock would occur, lockf() shall
84       fail with an [EDEADLK] error.
85
86       The interaction between fcntl() and lockf() locks is unspecified.
87
88       Blocking on a section shall be interrupted by any signal.
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, shall 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 shall attempt to
96       unlock only the requested section.
97
98       Attempting to lock a section of  a  file  that  is  associated  with  a
99       buffered stream produces unspecified results.
100

RETURN VALUE

102       Upon successful completion, lockf() shall return 0. Otherwise, it shall
103       return -1, set errno to indicate an error, and existing locks shall not
104       be changed.
105

ERRORS

107       The lockf() function shall fail if:
108
109       EBADF  The  fildes  argument  is  not  a valid open file descriptor; or
110              function is F_LOCK or F_TLOCK and fildes is  not  a  valid  file
111              descriptor open for writing.
112
113       EACCES or EAGAIN
114
115              The  function  argument  is F_TLOCK or F_TEST and the section is
116              already locked by another process.
117
118       EDEADLK
119              The function argument is F_LOCK and a deadlock is detected.
120
121       EINTR  A signal was caught during execution of the function.
122
123       EINVAL The function argument is not one of F_LOCK, F_TLOCK, F_TEST,  or
124              F_ULOCK; or size plus the current file offset is less than 0.
125
126       EOVERFLOW
127              The offset of the first, or if size is not 0 then the last, byte
128              in the requested section cannot be represented correctly  in  an
129              object of type off_t.
130
131
132       The lockf() function may fail if:
133
134       EAGAIN The  function  argument  is  F_LOCK  or  F_TLOCK and the file is
135              mapped with mmap().
136
137       EDEADLK or ENOLCK
138
139              The function argument is F_LOCK, F_TLOCK, or  F_ULOCK,  and  the
140              request  would  cause  the  number  of locks to exceed a system-
141              imposed limit.
142
143       EOPNOTSUPP or EINVAL
144
145              The implementation does not support the locking of files of  the
146              type indicated by the fildes argument.
147
148
149       The following sections are informative.
150

EXAMPLES

152   Locking a Portion of a File
153       In  the  following  example, a file named /home/cnd/mod1 is being modi‐
154       fied. Other processes that use locking are prevented from  changing  it
155       during  this  process.  Only  the first 10000 bytes are locked, and the
156       lock call fails if another process has any part  of  this  area  locked
157       already.
158
159
160              #include <fcntl.h>
161              #include <unistd.h>
162
163
164              int fildes;
165              int status;
166              ...
167              fildes = open("/home/cnd/mod1", O_RDWR);
168              status = lockf(fildes, F_TLOCK, (off_t)10000);
169

APPLICATION USAGE

171       Record-locking  should  not  be  used  in combination with the fopen(),
172       fread(), fwrite(), and other stdio functions. Instead, the more  primi‐
173       tive,  non-buffered  functions  (such  as open()) should be used. Unex‐
174       pected results may occur in processes that do  buffering  in  the  user
175       address  space.  The  process  may  later  read/write data which is/was
176       locked. The stdio functions are the most common  source  of  unexpected
177       buffering.
178
179       The  alarm()  function  may  be  used  to provide a timeout facility in
180       applications requiring it.
181

RATIONALE

183       None.
184

FUTURE DIRECTIONS

186       None.
187

SEE ALSO

189       alarm() , chmod() , close() , creat() , fcntl() , fopen()  ,  mmap()  ,
190       open()   ,   read()   ,  write()  ,  the  Base  Definitions  volume  of
191       IEEE Std 1003.1-2001, <unistd.h>
192
194       Portions of this text are reprinted and reproduced in  electronic  form
195       from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
196       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
197       Specifications  Issue  6,  Copyright  (C) 2001-2003 by the Institute of
198       Electrical and Electronics Engineers, Inc and The Open  Group.  In  the
199       event of any discrepancy between this version and the original IEEE and
200       The Open Group Standard, the original IEEE and The Open Group  Standard
201       is  the  referee document. The original Standard can be obtained online
202       at http://www.opengroup.org/unix/online.html .
203
204
205
206IEEE/The Open Group                  2003                             LOCKF(P)
Impressum