lockf (3)
PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.NAME
lockf --- record locking on filesSYNOPSIS
#include <unistd.h> int lockf(int fildes, int function, off_t size);
DESCRIPTION
The lockf() function shall lock sections of a file with advisory-mode locks. Calls to lockf() from threads in other processes which attempt to lock the locked file section shall either return an error value or block until the section becomes unlocked. All the locks for a process are removed when the process terminates. Record locking with lockf() shall be supported for regular files and may be supported for other files. The fildes argument is an open file descriptor. To establish a lock with this function, the file descriptor shall be opened with write-only permission (O_WRONLY) or with read/write permission (O_RDWR). The function argument is a control value which specifies the action to be taken. The permissible values for function are defined in <unistd.h> as follows:Function | Description |
F_ULOCK | Unlock locked sections. |
F_LOCK | Lock a section for exclusive use. |
F_TLOCK | Test and lock a section for exclusive use. |
F_TEST | Test a section for locks by other processes. |
RETURN VALUE
Upon successful completion, lockf() shall return 0. Otherwise, it shall return -1, set errno to indicate an error, and existing locks shall not be changed.ERRORS
The lockf() function shall fail if:- EBADF
- The fildes argument is not a valid open file descriptor; or function is F_LOCK or F_TLOCK and fildes is not a valid file descriptor open for writing.
- EACCES or EAGAIN
-
The function argument is F_TLOCK or F_TEST and the section is already locked by another process. - EDEADLK
- The function argument is F_LOCK and a deadlock is detected.
- EINTR
- A signal was caught during execution of the function.
- EINVAL
- The function argument is not one of F_LOCK, F_TLOCK, F_TEST, or F_ULOCK; or size plus the current file offset is less than 0.
- EOVERFLOW
- The offset of the first, or if size is not 0 then the last, byte in the requested section cannot be represented correctly in an object of type off_t. The lockf() function may fail if:
- EAGAIN
- The function argument is F_LOCK or F_TLOCK and the file is mapped with mmap().
- EDEADLK or ENOLCK
-
The function argument is F_LOCK, F_TLOCK, or F_ULOCK, and the request would cause the number of locks to exceed a system-imposed limit. - EOPNOTSUPP or EINVAL
-
The implementation does not support the locking of files of the type indicated by the fildes argument.
The following sections are informative.
EXAMPLES
Locking a Portion of a File
In the following example, a file named /home/cnd/mod1 is being modified. Other processes that use locking are prevented from changing it during this process. Only the first 10000 bytes are locked, and the lock call fails if another process has any part of this area locked already.
-
#include <fcntl.h> #include <unistd.h> int fildes; int status; ... fildes = open("/home/cnd/mod1", O_RDWR); status = lockf(fildes, F_TLOCK, (off_t)10000);
APPLICATION USAGE
Record-locking should not be used in combination with the fopen(), fread(), fwrite(), and other stdio functions. Instead, the more primitive, non-buffered functions (such as open()) should be used. Unexpected results may occur in processes that do buffering in the user address space. The process may later read/write data which is/was locked. The stdio functions are the most common source of unexpected buffering. The alarm() function may be used to provide a timeout facility in applications requiring it.RATIONALE
None.FUTURE DIRECTIONS
None.SEE ALSO
alarm(), chmod(), close(), creat(), fcntl(), fopen(), mmap(), open(), read(), write() The Base Definitions volume of POSIX.1-2008, <unistd.h>COPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, Copyright (C) 2013 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. (This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at www.unix.org/online.html .Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see www.kernel.org/doc/man-pages/reporting_bugs.html .