308 lines
12 KiB
HTML
308 lines
12 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
|
|
<html>
|
|
<head>
|
|
<meta name="generator" content="HTML Tidy, see www.w3.org">
|
|
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
|
|
<link type="text/css" rel="stylesheet" href="style.css"><!-- Generated by The Open Group's rhtm tool v1.2.1 -->
|
|
<!-- Copyright (c) 2001 The Open Group, All Rights Reserved -->
|
|
<title>lockf</title>
|
|
</head>
|
|
<body bgcolor="white">
|
|
<script type="text/javascript" language="JavaScript" src="../jscript/codes.js">
|
|
</script>
|
|
|
|
<basefont size="3"> <a name="lockf"></a> <a name="tag_03_342"></a><!-- lockf -->
|
|
<!--header start-->
|
|
<center><font size="2">The Open Group Base Specifications Issue 6<br>
|
|
IEEE Std 1003.1-2001<br>
|
|
Copyright © 2001 The IEEE and The Open Group, All Rights reserved.</font></center>
|
|
|
|
<!--header end-->
|
|
<hr size="2" noshade>
|
|
<h4><a name="tag_03_342_01"></a>NAME</h4>
|
|
|
|
<blockquote>lockf - record locking on files</blockquote>
|
|
|
|
<h4><a name="tag_03_342_02"></a>SYNOPSIS</h4>
|
|
|
|
<blockquote class="synopsis">
|
|
<div class="box"><code><tt><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt=
|
|
"[Option Start]" border="0"> #include <<a href="../basedefs/unistd.h.html">unistd.h</a>><br>
|
|
<br>
|
|
int lockf(int</tt> <i>fildes</i><tt>, int</tt> <i>function</i><tt>, off_t</tt> <i>size</i><tt>); <img src="../images/opt-end.gif"
|
|
alt="[Option End]" border="0"></tt></code></div>
|
|
|
|
<tt><br>
|
|
</tt></blockquote>
|
|
|
|
<h4><a name="tag_03_342_03"></a>DESCRIPTION</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>lockf</i>() function shall lock sections of a file with advisory-mode locks. Calls to <i>lockf</i>() from other threads
|
|
which attempt to lock the locked file section shall either return an error value or block until the section becomes unlocked. All
|
|
the locks for a process are removed when the process terminates. Record locking with <i>lockf</i>() shall be supported for regular
|
|
files and may be supported for other files.</p>
|
|
|
|
<p>The <i>fildes</i> argument is an open file descriptor. To establish a lock with this function, the file descriptor shall be
|
|
opened with write-only permission (O_WRONLY) or with read/write permission (O_RDWR).</p>
|
|
|
|
<p>The <i>function</i> argument is a control value which specifies the action to be taken. The permissible values for
|
|
<i>function</i> are defined in <a href="../basedefs/unistd.h.html"><i><unistd.h></i></a> as follows:</p>
|
|
|
|
<center>
|
|
<table border="1" cellpadding="3" align="center">
|
|
<tr valign="top">
|
|
<th align="center">
|
|
<p class="tent"><b>Function</b></p>
|
|
</th>
|
|
<th align="center">
|
|
<p class="tent"><b>Description</b></p>
|
|
</th>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent">F_ULOCK</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">Unlock locked sections.</p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent">F_LOCK</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">Lock a section for exclusive use.</p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent">F_TLOCK</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">Test and lock a section for exclusive use.</p>
|
|
</td>
|
|
</tr>
|
|
|
|
<tr valign="top">
|
|
<td align="left">
|
|
<p class="tent">F_TEST</p>
|
|
</td>
|
|
<td align="left">
|
|
<p class="tent">Test a section for locks by other processes.</p>
|
|
</td>
|
|
</tr>
|
|
</table>
|
|
</center>
|
|
|
|
<p>F_TEST shall detect if a lock by another process is present on the specified section.</p>
|
|
|
|
<p>F_LOCK and F_TLOCK shall both lock a section of a file if the section is available.</p>
|
|
|
|
<p>F_ULOCK shall remove locks from a section of the file.</p>
|
|
|
|
<p>The <i>size</i> argument is the number of contiguous bytes to be locked or unlocked. The section to be locked or unlocked starts
|
|
at the current offset in the file and extends forward for a positive size or backward for a negative size (the preceding bytes up
|
|
to but not including the current offset). If <i>size</i> is 0, the section from the current offset through the largest possible
|
|
file offset shall be locked (that is, from the current offset through the present or any future end-of-file). An area need not be
|
|
allocated to the file to be locked because locks may exist past the end-of-file.</p>
|
|
|
|
<p>The sections locked with F_LOCK or F_TLOCK may, in whole or in part, contain or be contained by a previously locked section for
|
|
the same process. When this occurs, or if adjacent locked sections would occur, the sections shall be combined into a single locked
|
|
section. If the request would cause the number of locks to exceed a system-imposed limit, the request shall fail.</p>
|
|
|
|
<p>F_LOCK and F_TLOCK requests differ only by the action taken if the section is not available. F_LOCK shall block the calling
|
|
thread until the section is available. F_TLOCK shall cause the function to fail if the section is already locked by another
|
|
process.</p>
|
|
|
|
<p>File locks shall be released on first close by the locking process of any file descriptor for the file.</p>
|
|
|
|
<p>F_ULOCK requests may release (wholly or in part) one or more locked sections controlled by the process. Locked sections shall be
|
|
unlocked starting at the current file offset through <i>size</i> bytes or to the end-of-file if <i>size</i> is (<b>off_t</b>)0.
|
|
When all of a locked section is not released (that is, when the beginning or end of the area to be unlocked falls within a locked
|
|
section), the remaining portions of that section shall remain locked by the process. Releasing the center portion of a locked
|
|
section shall cause the remaining locked beginning and end portions to become two separate locked sections. If the request would
|
|
cause the number of locks in the system to exceed a system-imposed limit, the request shall fail.</p>
|
|
|
|
<p>A potential for deadlock occurs if the threads of a process controlling a locked section are blocked by accessing another
|
|
process' locked section. If the system detects that deadlock would occur, <i>lockf</i>() shall fail with an [EDEADLK] error.</p>
|
|
|
|
<p>The interaction between <a href="../functions/fcntl.html"><i>fcntl</i>()</a> and <i>lockf</i>() locks is unspecified.</p>
|
|
|
|
<p>Blocking on a section shall be interrupted by any signal.</p>
|
|
|
|
<p>An F_ULOCK request in which <i>size</i> is non-zero and the offset of the last byte of the requested section is the maximum
|
|
value for an object of type <b>off_t</b>, when the process has an existing lock in which <i>size</i> is 0 and which includes the
|
|
last byte of the requested section, shall be treated as a request to unlock from the start of the requested section with a size
|
|
equal to 0. Otherwise, an F_ULOCK request shall attempt to unlock only the requested section.</p>
|
|
|
|
<p>Attempting to lock a section of a file that is associated with a buffered stream produces unspecified results.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_342_04"></a>RETURN VALUE</h4>
|
|
|
|
<blockquote>
|
|
<p>Upon successful completion, <i>lockf</i>() shall return 0. Otherwise, it shall return -1, set <i>errno</i> to indicate an error,
|
|
and existing locks shall not be changed.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_342_05"></a>ERRORS</h4>
|
|
|
|
<blockquote>
|
|
<p>The <i>lockf</i>() function shall fail if:</p>
|
|
|
|
<dl compact>
|
|
<dt>[EBADF]</dt>
|
|
|
|
<dd>The <i>fildes</i> argument is not a valid open file descriptor; or <i>function</i> is F_LOCK or F_TLOCK and <i>fildes</i> is
|
|
not a valid file descriptor open for writing.</dd>
|
|
|
|
<dt>[EACCES] or [EAGAIN]</dt>
|
|
|
|
<dd><br>
|
|
The <i>function</i> argument is F_TLOCK or F_TEST and the section is already locked by another process.</dd>
|
|
|
|
<dt>[EDEADLK]</dt>
|
|
|
|
<dd>The <i>function</i> argument is F_LOCK and a deadlock is detected.</dd>
|
|
|
|
<dt>[EINTR]</dt>
|
|
|
|
<dd>A signal was caught during execution of the function.</dd>
|
|
|
|
<dt>[EINVAL]</dt>
|
|
|
|
<dd>The <i>function</i> argument is not one of F_LOCK, F_TLOCK, F_TEST, or F_ULOCK; or <i>size</i> plus the current file offset is
|
|
less than 0.</dd>
|
|
|
|
<dt>[EOVERFLOW]</dt>
|
|
|
|
<dd>The offset of the first, or if <i>size</i> is not 0 then the last, byte in the requested section cannot be represented
|
|
correctly in an object of type <b>off_t</b>.</dd>
|
|
</dl>
|
|
|
|
<p>The <i>lockf</i>() function may fail if:</p>
|
|
|
|
<dl compact>
|
|
<dt>[EAGAIN]</dt>
|
|
|
|
<dd>The <i>function</i> argument is F_LOCK or F_TLOCK and the file is mapped with <a href=
|
|
"../functions/mmap.html"><i>mmap</i>()</a>.</dd>
|
|
|
|
<dt>[EDEADLK] or [ENOLCK]</dt>
|
|
|
|
<dd><br>
|
|
The <i>function</i> argument is F_LOCK, F_TLOCK, or F_ULOCK, and the request would cause the number of locks to exceed a
|
|
system-imposed limit.</dd>
|
|
|
|
<dt>[EOPNOTSUPP] or [EINVAL]</dt>
|
|
|
|
<dd><br>
|
|
The implementation does not support the locking of files of the type indicated by the <i>fildes</i> argument.</dd>
|
|
</dl>
|
|
</blockquote>
|
|
|
|
<hr>
|
|
<div class="box"><em>The following sections are informative.</em></div>
|
|
|
|
<h4><a name="tag_03_342_06"></a>EXAMPLES</h4>
|
|
|
|
<blockquote>
|
|
<h5><a name="tag_03_342_06_01"></a>Locking a Portion of a File</h5>
|
|
|
|
<p>In the following example, a file named <b>/home/cnd/mod1</b> is being modified. Other processes that use locking are prevented
|
|
from changing it during this process. Only the first 10000 bytes are locked, and the lock call fails if another process has any
|
|
part of this area locked already.</p>
|
|
|
|
<pre>
|
|
<tt>#include <fcntl.h>
|
|
#include <unistd.h>
|
|
<br>
|
|
int fildes;
|
|
int status;
|
|
...
|
|
fildes = open("/home/cnd/mod1", O_RDWR);
|
|
status = lockf(fildes, F_TLOCK, (off_t)10000);
|
|
</tt>
|
|
</pre>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_342_07"></a>APPLICATION USAGE</h4>
|
|
|
|
<blockquote>
|
|
<p>Record-locking should not be used in combination with the <a href="../functions/fopen.html"><i>fopen</i>()</a>, <a href=
|
|
"../functions/fread.html"><i>fread</i>()</a>, <a href="../functions/fwrite.html"><i>fwrite</i>()</a>, and other <i>stdio</i>
|
|
functions. Instead, the more primitive, non-buffered functions (such as <a href="../functions/open.html"><i>open</i>()</a>) should
|
|
be used. Unexpected results may occur in processes that do buffering in the user address space. The process may later read/write
|
|
data which is/was locked. The <i>stdio</i> functions are the most common source of unexpected buffering.</p>
|
|
|
|
<p>The <a href="../functions/alarm.html"><i>alarm</i>()</a> function may be used to provide a timeout facility in applications
|
|
requiring it.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_342_08"></a>RATIONALE</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_342_09"></a>FUTURE DIRECTIONS</h4>
|
|
|
|
<blockquote>
|
|
<p>None.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_342_10"></a>SEE ALSO</h4>
|
|
|
|
<blockquote>
|
|
<p><a href="alarm.html"><i>alarm</i>()</a> , <a href="chmod.html"><i>chmod</i>()</a> , <a href="close.html"><i>close</i>()</a> , <a
|
|
href="creat.html"><i>creat</i>()</a> , <a href="fcntl.html"><i>fcntl</i>()</a> , <a href="fopen.html"><i>fopen</i>()</a> , <a href=
|
|
"mmap.html"><i>mmap</i>()</a> , <a href="open.html"><i>open</i>()</a> , <a href="read.html"><i>read</i>()</a> , <a href=
|
|
"write.html"><i>write</i>()</a> , the Base Definitions volume of IEEE Std 1003.1-2001, <a href=
|
|
"../basedefs/unistd.h.html"><i><unistd.h></i></a></p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_342_11"></a>CHANGE HISTORY</h4>
|
|
|
|
<blockquote>
|
|
<p>First released in Issue 4, Version 2.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_342_12"></a>Issue 5</h4>
|
|
|
|
<blockquote>
|
|
<p>Moved from X/OPEN UNIX extension to BASE.</p>
|
|
|
|
<p>Large File Summit extensions are added. In particular, the description of [EINVAL] is clarified and moved from optional to
|
|
mandatory status.</p>
|
|
|
|
<p>A note is added to the DESCRIPTION indicating the effects of attempting to lock a section of a file that is associated with a
|
|
buffered stream.</p>
|
|
</blockquote>
|
|
|
|
<h4><a name="tag_03_342_13"></a>Issue 6</h4>
|
|
|
|
<blockquote>
|
|
<p>The DESCRIPTION is updated to avoid use of the term "must" for application requirements.</p>
|
|
</blockquote>
|
|
|
|
<div class="box"><em>End of informative text.</em></div>
|
|
|
|
<hr>
|
|
<hr size="2" noshade>
|
|
<center><font size="2"><!--footer start-->
|
|
UNIX ® is a registered Trademark of The Open Group.<br>
|
|
POSIX ® is a registered Trademark of The IEEE.<br>
|
|
[ <a href="../mindex.html">Main Index</a> | <a href="../basedefs/contents.html">XBD</a> | <a href=
|
|
"../utilities/contents.html">XCU</a> | <a href="../functions/contents.html">XSH</a> | <a href="../xrat/contents.html">XRAT</a>
|
|
]</font></center>
|
|
|
|
<!--footer end-->
|
|
<hr size="2" noshade>
|
|
</body>
|
|
</html>
|
|
|