#include <sys/types.h>
/* POSIX applications #include <unistd.h> */
#include <fcntl.h> int fcntl(int fd, int cmd, ...);
int fcntl(int fd, int cmd, int arg);
int fcntl(int fd, int cmd, struct flock *arg);
Lowest numbered available descriptor greater than or equal to arg.
References the same object as the original descriptor.
New descriptor shares the same file pointer if the object was a file.
Same access mode (read, write or read/write).
Same file status flags (that is, both descriptors share the same file status flags).
The close-on-exec flag associated with the new descriptor is cleared to keep the file open across execve.2 system calls.
POSIX applications can extract the file access modes from the return value using the mask O_ACCMODE (see <fcntl.h>). The file status flags are O_APPEND (set append mode) and O_NONBLOCK (no delay). The file access modes are O_RDONLY (open for reading only), O_RDWR (open for reading and writing) and O_WRONLY (open for writing only).
The SIGIO facilities are enabled by setting the FASYNC flag with F_SETFL.
The following commands are POSIX-specific:
The record locking mechanism allows two types of locks: shared locks (F_RDLCK) and exclusive locks (F_WRLCK). More than one process may hold a shared lock for a particular segment of a file at any given time, but multiple exclusive, or both shared and exclusive, locks may not exist simultaneously on any segment.
In order to claim a shared lock, the descriptor must have been opened with read access. The descriptor on which an exclusive lock is being placed must have been opened with write access.
A shared lock may be upgraded to an exclusive lock, and vice versa, simply by specifying the appropriate lock type with a cmd of F_SETLK or F_SETLKW; the previous lock will be released and the new lock applied (possibly after other processes have gained and released the lock).
If the cmd is F_SETLKW and the requested lock cannot be claimed immediately (e.g., another process holds an exclusive lock that partially or completely overlaps the current request) then the calling process will block until the lock may be acquired. Processes blocked awaiting a lock may be awakened by signals.
Care should be taken to avoid deadlock situations in applications in which multiple processes perform blocking locks on a set of common records.
The record that is to be locked or unlocked is described by the flock structure, which is defined in <fcntl.h> as follows:
struct flock { short l_type; /* F_RDLCK, F_WRLCK, or F_UNLCK */ short l_whence; /* flag to choose starting offset */ off_t l_start; /* relative offset, in bytes */ off_t l_len; /* length, in bytes; 0 means lock to EOF */ pid_t l_pid; /* returned with F_GETLK */ };
The flock structure describes the type (l_type), starting offset (l_whence), relative offset (l_start), and size (l_len) of the segment of the file to be affected. L_whence must be set to 0, 1, or 2 to indicate that the relative offset will be measured from the start of the file, current position, or end-of-file, respectively. The process id field (l_pid) is only used with the F_GETLK cmd to return the description of a lock held by another process.
Locks may start and extend beyond the current end-of-file, but may not be negative relative to the beginning of the file. A lock may be set to always extend to the end-of-file by setting l_len to zero (0). If such a lock also has l_whence and l_start set to zero (0), the entire file will be locked. Changing or unlocking a segment from the middle of a larger locked segment leaves two smaller segments 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 affect. All locks associated with a file for a given process are removed when the file is closed or the process terminates. Locks are not inherited by the child process in a fork(2) system call.
F_DUPFD A new descriptor. F_GETFD Value of flag (only the low-order bit is defined). F_GETFL Value of flags. F_GETOWN Value of descriptor owner. other Value other than -1.
F_GETLK returns F_UNLCK if the requesting process holds the specified lock. Thus, there is no way for a process to determine if it is still holding a specific lock after catching a SIGLOST signal.
Created by unroff & hp-tools. © somebody (See intro for details). All Rights Reserved. Last modified 11/5/97