Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

lockd(1M)

close(2)

dup(2)

exec(2)

fcntl(2)

fork(2)

open(2)

lockf(3C)

flock(3B)

NAME

flock − apply or remove an advisory lock on an open file

SYNOPSIS

/usr/ucb/cc [ flag ... ] file ...

#include <sys/file.h>

int flock(int fildes, int operation);

DESCRIPTION

flock() applies or removes an advisory lock on the file associated with the file descriptor fildes. A lock is applied by specifying an operation parameter LOCK_SH or LOCK_EX for a shared or exclusive lock.  Additionally, this may be ORed with LOCK_NB to make the operation non-blocking.  To unlock an existing lock, the operation should be LOCK_UN.

Advisory locks allow cooperating processes to perform consistent operations on files, but do not guarantee exclusive access (that is, processes may still access files without using advisory locks, possibly resulting in inconsistencies). 

The locking mechanism allows two types of locks: shared locks and exclusive locks.  More than one process may hold a shared lock for a file at any given time, but multiple exclusive, or both shared and exclusive, locks may not exist simultaneously on a file. 

A shared lock may be upgraded to an exclusive lock, and vice versa, simply by specifying the appropriate lock type; the previous lock will be 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 block until the lock may be acquired.  If LOCK_NB is included in operation, then this will not happen; instead the call will fail and the error EWOULDBLOCK will be returned. 

RETURN VALUES

flock() returns:

0 on success. 

−1 on failure and sets errno to indicate the error. 

ERRORS

EBADF The argument fildes is an invalid descriptor. 

EINVAL cmd is not a valid argument. 

EOPNOTSUPP The argument fildes refers to an object other than a file. 

EWOULDBLOCK The file is locked and the LOCK_NB option was specified. 

SEE ALSO

lockd(1M), close(2), dup(2), exec(2), fcntl(2), fork(2), open(2), lockf(3C)

NOTES

Locks are on files, not file descriptors.  That is, file descriptors duplicated through dup(2) or fork(2) do not result in multiple instances of a lock, but rather multiple references to a single lock.  If a process holding a lock on a file forks and the child explicitly unlocks the file, the parent will lose its lock. 

Processes blocked awaiting a lock may be awakened by signals. 

BUGS

Locks obtained through the flock() mechanism are known only within the system on which they were placed.  Thus, multiple clients may successfully acquire exclusive locks on the same remote file.  If this behavior is not explicitly desired, the fcntl(2) or lockf(3C) functions should be used instead; these make use of the services of the network lock manager (see lockd(1M)). 

SunOS 5.1/SPARC  —  Last change: 29 Jul 1991

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026