Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-UX Reference > F

flock(2)

HP-UX 11i Version 3: February 2007
» 

Technical documentation

» Feedback
Content starts here

 » Table of Contents

 » Index

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 filedes file 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 operations.

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 multiple 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).

File locking as implemented for NFS files is only advisory.

RETURN VALUES

flock() returns:

0

for successful completion

-1

for unsuccessful completion; errno is set to indicate the error

ERRORS

If the flock() function fails, errno may be set to one of the following values:

EBADF

The filedes parameter is not a valid open file descriptor.

A shared lock was requested and filedes was not opened for reading, or an exclusive lock was requested and filedes was not opened for writing.

EDEADLK

The lock is blocked by some lock from another process. Putting the calling process to sleep while waiting for that lock to become free would cause a deadlock.

EINTR

A signal interrupted the flock() call.

EINVAL

The filedes parameter refers to a file that does not support locking.

The operation parameter is not valid.

ENOLCK

The lock table is full. Too many files are already locked.

The filedes parameter refers to an NFS file with the access bits set for enforcement mode.

The filedes parameter refers to an NFS file, and a system error occurred on the remote node.

EWOULDBLOCK

The file is locked and the LOCK_NB option was specified.

The file is mapped into virtual memory via the mmap() system call (see mmap(2)).

STANDARDS CONFORMANCE

flock(): Not part of any UNIX standard.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1983-2007 Hewlett-Packard Development Company, L.P.