HP-UX Reference (11i v3 07/02) - 2 System Calls (vol 5)

f
flock(2) flock(2)
NAME
flock() - apply or remove an advisory or enforced lock on an open file
SYNOPSIS
#include <sys/fcntl.h>
int flock(
int filedes, int operation
);
Parameters
filedes Specifies a file descriptor returned by a successful
open(), creat(), dup(), dup2,or
fcntl() function, identifying the file on which the locking operation is to be performed.
operation Specifies one of the following constants for
flock(), defined in the <sys/fcntl.h>
file:
LOCK_SH Apply a shared lock.
LOCK_EX Apply an exclusive lock.
LOCK_UN Remove a lock.
In addition to the preceding constants, the following constant can be bitwise ORed with either
LOCK_SH or LOCK_EX:
LOCK_NB Do not block when locking.
DESCRIPTION
The flock() function applies or removes an advisory or enforced lock on the whole file associated with
the ledes le descriptor. Advisory locks allow cooperating processes to perform consistent operations on
files, but do not guarantee consistency (that is, processes may still access files without using advisory locks,
possibly resulting in inconsistencies).Enforced locks are in effect if the enforcement bit in the file mode is
set (see chmod(2)). This might affect read(), write(), and other system calls performing file opera-
tions.
You can use the flock() function to coordinate a file’s lock status on local and NFS file systems.
The locking mechanism allows two types of locks: shared locks and exclusive locks. At any time multi-
ple shared locks may be applied to a file, but at no time are multiple exclusive locks or both shared and
exclusive locks allowed simultaneously on a file.
A shared lock may be upgraded to an exclusive lock, and vice versa, simply by specifying the appropriate
lock type. This results in the previous lock being released and the new lock applied (possibly after other
processes have gained and released the lock).
Requesting a lock on an object that is already locked normally causes the caller to be blocked until the lock
may be acquired. If LOCK_NB is included in operation, this will not happen; instead, the call will fail and
errno will be set to [EWOULDBLOCK].
APPLICATION USAGE
Locks are on files, not file descriptors. This means that:
Locks are not inherited by a child process resulting from a fork() call.
All locks associated with a file for a given process are removed when any thread in the process closes
any file descriptor for that file.
Processes that are blocked awaiting a lock may be awakened by signals. An application needs to check the
return value to make sure that the lock is really acquired.
The
flock() interface is not part of any UNIX standard. Therefore, if you are designing and writing
applications to be portable across platforms, you should use the fcntl() file locking interface instead of
flock().
NETWORKING FEATURES
NFS
The advisory file locking capabilities of flock() are implemented throughout the network by the network
lock daemon (lockd). 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 process that held the lock is
issued a SIGLOST signal. See lockd(1M).
110 Hewlett-Packard Company 1 HP-UX 11i Version 3: February 2007