SYNOPSIS
#include <unistd.h>
int lockf(int fildes, int cmd, off_t size);
int lockf64(int fildes, int cmd, off64_t size);
DESCRIPTION
The
The following lock commands are available:
- F_ULOCK
-
Unlock a previously locked section of the file.
- F_LOCK
-
Lock a section for exclusive use. If the section is already locked by another process, wait until it is available, or until
lockf() is interrupted by a signal. - F_TLOCK
-
Test a section for another process' locks. If the section is already locked, return a failure rather than blocking until the lock becomes available
- F_TEST
-
Test a section for another process' locks.
The section to be locked or unlocked starts at the current offset in the file and extends forward for a positive size or backward for a negative size (the preceding bytes up to but not including the current offset). If the specified lock size is 0, the section from the current offset through the largest possible file offset is locked. An area need not be allocated to the file to be locked because locks may exist past the end-of-file.
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 would occur, the sections are combined into a single locked section. If the request would cause the number of locks to exceed a system-imposed limit, the request fails.
File locks are released on the first close by the locking process of any file descriptor for the file.
The F_ULOCK request 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 the specified size bytes , or to the end of file if the size is 0. When all of a locked section is not released, 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. If the request would cause the number of locks in the system to exceed a system-imposed limit, the request fails.
A potential for deadlock occurs
if the threads of a process controlling a locked section are
blocked by accessing another process' locked section. If the
system detects that deadlock would occur,
The
PARAMETERS
- fildes
-
Is an open file descriptor. It must have O_WRONLY or O_RDWR permission for a successful locking call.
- cmd
-
Is a lock command, as described in the DESCRIPTION section.
- size
-
Is the number of contiguous bytes to be locked or unlocked.
RETURN VALUES
If successful,
- EACCES
-
The cmd parameter is F_TLOCK or F_TEST and the section is already locked by another process.
- EBADF
-
The fildes parameter is not a valid open descriptor. Or, the cmd parameter is F_LOCK or F_TLOCK and the fildes argument is not a valid file descriptor open for writing.
- EDEADLK
-
The cmd parameter is F_LOCK, the lock is blocked by some lock from another process, and putting the calling process to sleep, waiting for that lock to become free would cause a deadlock.
- EINTR
-
The cmd parameter is F_LOCK and a signal interrupted the process while it was waiting to complete the lock.
- EINVAL
-
The cmd parameter is not one of F_LOCK, F_TLOCK, F_TEST, or F_ULOCK.
The size parameter plus the current file offset is less than 0.
The fildes parameter refers to a file that does not support locking.
- ENOLCK
-
The cmd parameter is F_LOCK, F_TLOCK, or F_ULOCK and the number of locked regions available in the system would be exceeded by the request.
- 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.
MULTITHREAD SAFETY LEVEL
MT-Safe.
PORTING ISSUES
Standard locks apply only to the local system.
Locks are, by default, advisory. The NuTCRACKER Platform also supports a form of mandatory
locking on 8.1/2012R2/10/2016/2019/11/2022.
If the S_ISGID access mode bit is set for a file, and
S_IXGRP is not set, then the NuTCRACKER Platform enables mandatory locking
for the file using Win32 APIs.
Mandatory locks are not inherited across
The NuTCRACKER Platform also supports an advisory locking mode
that uses the native Win32 locking APIs (
AVAILABILITY
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition
SEE ALSO
- Functions:
_NutConf() ,_NutForkExecl() ,_NutForkExecle() ,_NutForkExeclp() ,_NutForkExeclpe() ,_NutForkExecv() ,_NutForkExecve() ,_NutForkExecvp() ,_NutForkExecvpe() ,chmod() ,execl() ,execle() ,execlp() ,execlpe() ,execv() ,execve() ,execvp() ,execvpe() ,fcntl() ,fork()
- Miscellaneous:
- lf64, struct stat
PTC MKS Toolkit 10.4 Documentation Build 39.