Lock or unlock a section of a file
#include <unistd.h> int lockf( int filedes, int function, off_t size ); int lockf64( int filedes, int function, off64_t size );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
You can use the lockf() function to lock a section of a file, using advisory-mode locks. If other threads call lockf() to try to lock the locked file section, those calls either return an error value or block until the section becomes unlocked. The lockf64() function is a large-file version of lockf().
All the locks for a process are removed when the process terminates. Record locking with lockf() is supported for regular files and may be supported for other files.
The sections locked with F_LOCK or F_TLOCK may in whole or in part, contain or be contained by a previously locked section for the same process. When this occurs, or if adjacent locked sections occur, the sections are combined into a single locked section.
File locks are released on the first close by the locking process of any file descriptor for the file.
F_ULOCK requests may release (wholly or in part) one or more locked sections controlled by the process. Locked sections are unlocked starting at the current file offset through size bytes or to the end of file if size is 0. When all of a locked section isn't released (that is, when the beginning or end of the area to be unlocked falls within a locked section), the remaining portions of that section are still locked by the process. Releasing the center portion of a locked section causes the remaining locked beginning and end portions to become two separate locked sections.
A potential for deadlock occurs if the threads of a process controlling a locked section are blocked by accessing another process's locked section. If the system detects that deadlock could occur, lockf() fails with EDEADLK.
The interaction between fcntl() and lockf() locks is undefined. Blocking on a section is interrupted by any signal.
If size is the maximum value of type off_t (or off64_t for lockf64()) and the process has an existing lock of size 0 in this range (indicating a lock on the entire file), then an F_ULOCK request is treated the same as an F_ULOCK request of size 0. Otherwise an F_ULOCK request attempts to unlock only the requested section. Attempting to lock a section of a file that's associated with a buffered stream produces undefined results.
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | Yes |
Thread | Yes |