Open System Services System Calls Reference Manual (G06.25+, H06.03+)
System Functions (f - i) fcntl(2)
opened with read access.
An exclusive lock prevents any other process from setting a shared lock or an exclusive lock on
any portion of the protected area. A request for an exclusive lock fails if the file descriptor was
not opened with write access.
The flock structure describes the type (l_type field), 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 value of l_whence is set to SEEK_SET, SEEK_CUR,orSEEK_END to indicate that the
relative offset of l_start bytes is measured from the start of the file, from the current position, or
from the end of the file, respectively. The value of l_len is the number of consecutive bytes to be
locked. The l_len value can be negative (where the definition of type off_t permits negative
values of l_len). The l_pid field is used only with F_GETLK to return the process ID of the pro-
cess holding a blocking lock. After a successful F_GETLK request, the value of l_whence
becomes SEEK_SET.
If l_len is positive, the area affected starts at l_start and ends at l_start + l_len - 1.Ifl_len is
negative, the area affected starts at l_start + l_len and ends at l_start - 1. Lock lengths can be
negative.
Locks can start and extend beyond the current end of a file, but they cannot be negative relative
to the beginning of the file. If l_len is set to 0 (zero), a lock can be set to always extend to the
largest possible value of the file offset for that file. If such a lock also has l_start set to 0 (zero)
and l_whence is set to SEEK_SET, the whole file is locked.
Changing or unlocking a portion from the middle of a larger locked segment leaves a smaller
segment at either end. Locking a segment that is 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
when the process holding that file descriptor terminates. Locks are not inherited by a child pro-
cess in a fork(), tdm_fork( ),ortdm_spawn( )-type function.
RETURN VALUES
Upon successful completion, the value returned by the fcntl( ) function depends on the value of
the request parameter, listed:
F_DUPFD Returns a new file descriptor.
F_GETFD Returns the value of the file descriptor flags. The return value is not negative.
F_GETFL Returns the value of file status flags and access modes. The return value is not
negative.
F_GETLK Returns the value 0 (zero).
F_GETOWN Returns the process ID or process group ID of the socket receiving a SIGURG
signal. A positive value is a process ID; a negative value is a process group ID.
F_SETFD Returns the value 0 (zero).
F_SETFL Returns the value 0 (zero).
F_SETLK Returns the value 0 (zero).
F_SETLKW Returns the value 0 (zero).
F_SETOWN Returns the value 0 (zero).
If the fcntl() function fails, the value -1 is returned, and errno is set to indicate the error.
527186-003 Hewlett-Packard Company 3−5