add -s option which inserts/matches truncated member names
[unix-history] / usr / src / lib / libc / sys / flock.2
CommitLineData
88b3ccf2
KB
1.\" Copyright (c) 1983 The Regents of the University of California.
2.\" All rights reserved.
619ee695 3.\"
91cff1e1 4.\" %sccs.include.redist.man%
88b3ccf2 5.\"
91cff1e1 6.\" @(#)flock.2 6.6 (Berkeley) %G%
619ee695 7.\"
9b1f9fbd 8.TH FLOCK 2 ""
619ee695
KM
9.UC 5
10.SH NAME
11flock \- apply or remove an advisory lock on an open file
12.SH SYNOPSIS
13.nf
14.ft B
15#include <sys/file.h>
16.PP
17.ft B
18.DT
19#define LOCK_SH 1 /* shared lock */
20#define LOCK_EX 2 /* exclusive lock */
21#define LOCK_NB 4 /* don't block when locking */
22#define LOCK_UN 8 /* unlock */
23.PP
24.ft B
25flock(fd, operation)
26int fd, operation;
27.fi
28.SH DESCRIPTION
29.I Flock
30applies or removes an
31.I advisory
32lock on the file associated with the file descriptor
33.IR fd .
34A lock is applied by specifying an
35.I operation
38edb05c 36parameter that is the inclusive or of
619ee695
KM
37LOCK_SH or LOCK_EX and, possibly, LOCK_NB. To unlock
38an existing lock
39.I operation
40should be LOCK_UN.
41.PP
42Advisory locks allow cooperating processes to perform
43consistent operations on files, but do not guarantee
4decc76b 44consistency (i.e., processes may still access files
619ee695
KM
45without using advisory locks possibly resulting in
46inconsistencies).
47.PP
48The locking mechanism allows two types of locks:
49.I shared
50locks and
51.I exclusive
52locks.
53At any time multiple shared locks may be applied to a file,
54but at no time are multiple exclusive, or both shared and exclusive,
55locks allowed simultaneously on a file.
56.PP
57A shared lock may be
58.I upgraded
59to an exclusive lock, and vice versa, simply by specifying
60the appropriate lock type; this results in the previous
61lock being released and the new lock applied (possibly
62after other processes have gained and released the lock).
63.PP
38edb05c 64Requesting a lock on an object that is already locked
19ecbc2b 65normally causes the caller to be blocked until the lock may be
619ee695
KM
66acquired. If LOCK_NB is included in
67.IR operation ,
68then this will not happen; instead the call will fail and
69the error EWOULDBLOCK will be returned.
70.SH NOTES
71Locks are on files, not file descriptors. That is, file descriptors
72duplicated through
73.IR dup (2)
74or
75.IR fork (2)
76do not result in multiple instances of a lock, but rather multiple
77references to a single lock. If a process holding a lock on a file
78forks and the child explicitly unlocks the file, the parent will
79lose its lock.
80.PP
81Processes blocked awaiting a lock may be awakened by signals.
82.SH "RETURN VALUE
83Zero is returned if the operation was successful;
84on an error a \-1 is returned and an error code is left in
85the global location \fIerrno\fP.
86.SH "ERRORS
87The \fIflock\fP call fails if:
88.TP 20
89[EWOULDBLOCK]
90The file is locked and the LOCK_NB option was specified.
91.TP 20
92[EBADF]
93The argument \fIfd\fP is an invalid descriptor.
94.TP 20
95[EINVAL]
96The argument \fIfd\fP refers to an object other than a file.
97.SH "SEE ALSO"
98open(2), close(2), dup(2), execve(2), fork(2)