📄 lockf.3
字号:
.\" SCCSID: @(#)lockf.3 2.1 3/10/87.TH lockf 3.SH Namelockf \- record locking on files.SH Syntax\fB#include <unistd.h>\fP.PP\fBlockf\^(\^\fIfildes\fP, \fIfunction\fP, \fIsize\fP\^).br\fBlong \fIsize\fP\^;.br\fBint \fIfildes\fP, \fIfunction\fP\^;.SH Description.NXR "lockf subroutine".NXR "file" "locking region".NXA "fcntl system call" "lockf subroutine"The .PN lockf subroutine allows sections of a fileto be locked.These are advisory mode locks..\" Mandatory or enforcement mode record locks are.\" not available.Locking calls from other processes which attemptto lock the locked file section returneither an error value or are put to sleep until the resourcebecomes unlocked.All the locks for a process are removed when the process terminates.For more information about record locking, see .MS fcntl 2 ..PPThe\fIfildes\fR is an open file descriptor.The file descriptor must have.SM O_WRONLYor.SM O_RDWRpermission in order to establish lock with this function call..PPThe \fIfunction\fR is a control valuewhich specifies the action to be taken.The permissible values for \fIfunction\fRare defined in <unistd.h>as follows:.PP.EX 0.sp#define F_ULOCK 0 /\(** Unlock a previously locked section \(**/#define F_LOCK 1 /\(** Lock a section for exclusive use \(**/#define F_TLOCK 2 /\(** Test and lock a section for exclusive use \(**/#define F_TEST 3 /\(** Test section for other processes locks \(**/.EE.PPAll other values of \fIfunction\fR are reserved for future extensionsand result in an error return if not implemented..PP.SM F_TESTis used to detect if a lock by another process ispresent on the specified section..SM F_LOCKand.SM F_TLOCKboth lock a section of a file if the section is available..SM F_UNLOCKremoves locks from a section of the file..PPThe\fIsize\fR is the number of contiguous bytes to be locked or unlocked.The resource to be locked or unlocked starts at thecurrent offset in the file and extends forwardfor a positive size and backward for a negative size.If \fIsize\fR is zero,the section from the current offset through the largest fileoffset is locked (that is, from the current offset through thepresent or any future end-of-file).An area need not be allocated to the file in order to be locked, as such locksmay exist past the end-of-file..PPThe sections locked with.SM F_LOCKor.SM F_TLOCKmay, in whole or in part,contain or be contained by a previously locked section for the same process.When this occurs,or if adjacent sections occur,the sections are combined into a single section.If the request requires that a new element be added to the table of activelocks and this table is already full,an error is returned,and the new section is not locked..PP.SM F_LOCKand.SM F_TLOCKrequests differ only by the action taken if the resourceis not available..SM F_LOCKcauses the calling process to sleep until the resource is available..SM F_TLOCKcauses the function to return a \-1 and set\fIerrno\fR to.SM [EACCES]error if the section is already locked by another process..PP.SM F_ULOCKrequests may, in whole or in part,release one or more locked sections controlled by the process.When sections are not fully released,the remaining sections are still locked by the process.Releasing the center section of alocked section requires an additional element in the tableof active locks.If this table is full,an.SM [EDEADLK]error is returned and the requested section is not released..PPA potential for deadlock occurs if a process controlling a lockedresource is put to sleep by accessing another process's locked resource.Thus calls to .PN lock or .PN fcntl scan for a deadlock prior to sleeping on a locked resource.An error return is made if sleeping on the locked resource would cause a deadlock..PPSleeping on a resource is interrupted with any signal.You can use the.MS alarm 3 command to provide a timeout facilityin applications which require this facility..PPFile region locking is supported over NFS,if the NFS locking service has been enabled..SH Restrictions.NXR "lockf subroutine" "restrictions"Unexpected results may occur in processes that do bufferingin the user address space.The process may later read or write data which is or was locked.The standard I/O package is the most common source ofunexpected buffering..SH Return ValuesUpon successful completion,0 is returned.Otherwise, a \-1 is returned andthe global variable \fIerrno\fR is set toindicate the error..SH Diagnostics.NXR "lockf subroutine" "diagnostics"The .PN lockf subroutine fails if:.br.sp.TP 15.SM [EBADF]The \fIfildes\fR is not a valid open descriptor..TP 15.SM [EACCESS]The \fIcmd\fR is.SM F_TLOCKor.SM F_TESTand the section is already locked by another process.Or,the file is remotely mounted,and the NFS locking service has not been enabled..TP 15.SM [EDEADLK]The \fIcmd\fR is.SM F_LOCKor.SM F_TLOCKand a deadlock would occur.Also the \fIcmd\fR is either of the above or.SM F_ULOCKand the number of entries in the lock tablewould exceed the number allocated on the system..TP 15.SM [EINVAL]The value given for the \fIrequest\fRargument is invalid..SH See Alsoclose(2), creat(2), fcntl(2), intro(2), open(2), read(2), write(2), lockd(8c)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -