HP-UX Reference (11i v2 04/09) - 2 System Calls (vol 5)

f
fcntl(2) fcntl(2)
negative values, other than -1, indicate a process group ID.
F_GETLK64 Same as F_GETLK, except arg is a pointer to
struct flock64 instead of
struct flock.
F_SETLK64 Same as F_SETLK, except arg is a pointer to
struct flock64 instead of
struct flock.
F_SETLKW64 Same as F_SETLKW, except arg is a pointer to
struct flock64 instead of
struct flock.
A read lock prevents any other process from write-locking the protected area. More than one read
lock can exist for a given segment of a file at a given time. The file descriptor on which a read lock
is being placed must have been opened with read access.
A write lock prevents any other process from read-locking or write-locking the protected area. Only
one write lock may exist for a given segment of a file at a given time. The file descriptor on which a
write lock is being placed must have been opened with write access.
The structure
flock describes the type (l_type
), starting offset (l_whence), relative offset
(
l_start), size (l_len), and process ID (
l_pid) of the segment of the file to be affected. The pro-
cess ID field is only used with the
F_GETLK cmd to return the value of a block in lock. Locks can
start and extend beyond the current end of a file, but cannot be negative relative to the beginning of
the file. A lock can be set to always extend to the end of file by setting
l_len to zero (0). If such a
lock also has
l_start set to zero (0), the whole file will be locked. Changing or unlocking a seg-
ment from the middle of a larger locked segment leaves two smaller segments for either end. Lock-
ing a segment already locked by the calling process causes the old lock type to be removed and the
new lock type to take effect. All locks associated with a file for a given process are removed when a
file descriptor for that file is closed by that process or the process holding that file descriptor ter-
minates. Locks are not inherited by a child process in a fork(2) system call.
When enforcement-mode file and record locking is activated on a file (see chmod(2)), future
read() and write() system calls on the file are affected by the record locks in effect.
Application Usage
Because in the future the external variable
errno will be set to [EAGAIN] rather than [EACCES] when
a section of a file is already locked by another process, portable application programs should expect and
test for either value. For example:
flk->l_type = F_RDLCK;
if (fcntl(fd, F_SETLK, flk) == -1)
if ((errno == EACCES) || (errno == EAGAIN))
/*
* section locked by another process,
* check for either EAGAIN or EACCES
* due to different implementations
*/
else if ...
/*
* check for other errors
*/
NETWORKING FEATURES
NFS
The advisory record-locking capabilities of fcntl() are implemented throughout the network by the
‘‘network lock daemon’’ (see lockd (1M)). If the file server crashes and is rebooted, the lock daemon
attempts to recover all locks associated with the crashed server. If a lock cannot be reclaimed, the pro-
cess that held the lock is issued a SIGLOST signal.
Record locking, as implemented for NFS files, is only advisory.
RETURN VALUE
Upon successful completion, the value returned depends on cmd as follows:
F_DUPFD A new file descriptor.
F_GETFD Value of close-on-exec flag (only the low-order bit is defined).
HP-UX 11i Version 2: September 2004 2 Hewlett-Packard Company Section 269