📄 fcntl.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-2003 The Open Group, All Rights Reserved --><title>fcntl</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="fcntl"></a> <a name="tag_03_141"></a><!-- fcntl --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2003 Edition<br>Copyright © 2001-2003 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_141_01"></a>NAME</h4><blockquote>fcntl - file control</blockquote><h4><a name="tag_03_141_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><div class="box"><code><tt><sup>[<a href="javascript:open_code('OH')">OH</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0"> #include <unistd.h> <img src="../images/opt-end.gif" alt="[Option End]" border="0"></tt></code></div><tt><br> #include <<a href="../basedefs/fcntl.h.html">fcntl.h</a>><br><br> int fcntl(int</tt> <i>fildes</i><tt>, int</tt> <i>cmd</i><tt>, ...);<br></tt></blockquote><h4><a name="tag_03_141_03"></a>DESCRIPTION</h4><blockquote><p>The <i>fcntl</i>() function shall perform the operations described below on open files. The <i>fildes</i> argument is a filedescriptor.</p><p>The available values for <i>cmd</i> are defined in <a href="../basedefs/fcntl.h.html"><i><fcntl.h></i></a> and are asfollows:</p><dl compact><dt>F_DUPFD</dt><dd>Return a new file descriptor which shall be the lowest numbered available (that is, not already open) file descriptor greaterthan or equal to the third argument, <i>arg</i>, taken as an integer of type <b>int</b>. The new file descriptor shall refer to thesame open file description as the original file descriptor, and shall share any locks. The FD_CLOEXEC flag associated with the newfile descriptor shall be cleared to keep the file open across calls to one of the <i><a href="../functions/exec.html">exec</a></i>functions.</dd><dt>F_GETFD</dt><dd>Get the file descriptor flags defined in <a href="../basedefs/fcntl.h.html"><i><fcntl.h></i></a> that are associated withthe file descriptor <i>fildes</i>. File descriptor flags are associated with a single file descriptor and do not affect other filedescriptors that refer to the same file.</dd><dt>F_SETFD</dt><dd>Set the file descriptor flags defined in <a href="../basedefs/fcntl.h.html"><i><fcntl.h></i></a>, that are associatedwith <i>fildes</i>, to the third argument, <i>arg</i>, taken as type <b>int</b>. If the FD_CLOEXEC flag in the third argument is 0,the file shall remain open across the <i><a href="../functions/exec.html">exec</a></i> functions; otherwise, the file shall beclosed upon successful execution of one of the <i><a href="../functions/exec.html">exec</a></i> functions.</dd><dt>F_GETFL</dt><dd>Get the file status flags and file access modes, defined in <a href="../basedefs/fcntl.h.html"><i><fcntl.h></i></a>, forthe file description associated with <i>fildes</i>. The file access modes can be extracted from the return value using the maskO_ACCMODE, which is defined in <a href="../basedefs/fcntl.h.html"><i><fcntl.h></i></a>. File status flags and file accessmodes are associated with the file description and do not affect other file descriptors that refer to the same file with differentopen file descriptions.</dd><dt>F_SETFL</dt><dd>Set the file status flags, defined in <a href="../basedefs/fcntl.h.html"><i><fcntl.h></i></a>, for the file descriptionassociated with <i>fildes</i> from the corresponding bits in the third argument, <i>arg</i>, taken as type <b>int</b>. Bitscorresponding to the file access mode and the file creation flags, as defined in <a href="../basedefs/fcntl.h.html"><i><fcntl.h></i></a>, that are set in <i>arg</i> shall be ignored. If any bits in <i>arg</i> otherthan those mentioned here are changed by the application, the result is unspecified.</dd><dt>F_GETOWN</dt><dd>If <i>fildes</i> refers to a socket, get the process or process group ID specified to receive SIGURG signals when out-of-banddata is available. Positive values indicate a process ID; negative values, other than -1, indicate a process group ID. If<i>fildes</i> does not refer to a socket, the results are unspecified.</dd><dt>F_SETOWN</dt><dd>If <i>fildes</i> refers to a socket, set the process or process group ID specified to receive SIGURG signals when out-of-banddata is available, using the value of the third argument, <i>arg</i>, taken as type <b>int</b>. Positive values indicate a processID; negative values, other than -1, indicate a process group ID. If <i>fildes</i> does not refer to a socket, the results areunspecified.</dd></dl><p>The following values for <i>cmd</i> are available for advisory record locking. Record locking shall be supported for regularfiles, and may be supported for other files.</p><dl compact><dt>F_GETLK</dt><dd>Get the first lock which blocks the lock description pointed to by the third argument, <i>arg</i>, taken as a pointer to type<b>struct flock</b>, defined in <a href="../basedefs/fcntl.h.html"><i><fcntl.h></i></a>. The information retrieved shalloverwrite the information passed to <i>fcntl</i>() in the structure <b>flock</b>. If no lock is found that would prevent this lockfrom being created, then the structure shall be left unchanged except for the lock type which shall be set to F_UNLCK.</dd><dt>F_SETLK</dt><dd>Set or clear a file segment lock according to the lock description pointed to by the third argument, <i>arg</i>, taken as apointer to type <b>struct flock</b>, defined in <a href="../basedefs/fcntl.h.html"><i><fcntl.h></i></a>. F_SETLK canestablish shared (or read) locks (F_RDLCK) or exclusive (or write) locks (F_WRLCK), as well as to remove either type of lock(F_UNLCK). F_RDLCK, F_WRLCK, and F_UNLCK are defined in <a href="../basedefs/fcntl.h.html"><i><fcntl.h></i></a>. If a sharedor exclusive lock cannot be set, <i>fcntl</i>() shall return immediately with a return value of -1.</dd><dt>F_SETLKW</dt><dd>This command shall be equivalent to F_SETLK except that if a shared or exclusive lock is blocked by other locks, the threadshall wait until the request can be satisfied. If a signal that is to be caught is received while <i>fcntl</i>() is waiting for aregion, <i>fcntl</i>() shall be interrupted. Upon return from the signal handler, <i>fcntl</i>() shall return -1 with <i>errno</i>set to [EINTR], and the lock operation shall not be done.</dd></dl><p>Additional implementation-defined values for <i>cmd</i> may be defined in <a href="../basedefs/fcntl.h.html"><i><fcntl.h></i></a>. Their names shall start with F_.</p><p>When a shared lock is set on a segment of a file, other processes shall be able to set shared locks on that segment or a portionof it. A shared lock prevents any other process from setting an exclusive lock on any portion of the protected area. A request fora shared lock shall fail if the file descriptor was not opened with read access.</p><p>An exclusive lock shall prevent any other process from setting a shared lock or an exclusive lock on any portion of theprotected area. A request for an exclusive lock shall fail if the file descriptor was not opened with write access.</p><p>The structure <b>flock</b> describes the type ( <i>l_type</i>), starting offset ( <i>l_whence</i>), relative offset (<i>l_start</i>), size ( <i>l_len</i>), and process ID ( <i>l_pid</i>) of the segment of the file to be affected.</p><p>The value of <i>l_whence</i> is SEEK_SET, SEEK_CUR, or SEEK_END, to indicate that the relative offset <i>l_start</i> bytes shallbe measured from the start of the file, current position, or end of the file, respectively. The value of <i>l_len</i> is the numberof consecutive bytes to be locked. The value of <i>l_len</i> may be negative (where the definition of <b>off_t</b> permits negativevalues of <i>l_len</i>). The <i>l_pid</i> field is only used with F_GETLK to return the process ID of the process holding ablocking lock. After a successful F_GETLK request, when a blocking lock is found, the values returned in the <b>flock</b> structureshall be as follows:</p><dl compact><dt><i>l_type</i></dt><dd>Type of blocking lock found.</dd><dt><i>l_whence</i></dt><dd>SEEK_SET.</dd><dt><i>l_start</i></dt><dd>Start of the blocking lock.</dd><dt><i>l_len</i></dt><dd>Length of the blocking lock.</dd><dt><i>l_pid</i></dt><dd>Process ID of the process that holds the blocking lock.</dd></dl><p>If the command is F_SETLKW and the process must wait for another process to release a lock, then the range of bytes to be lockedshall be determined before the <i>fcntl</i>() function blocks. If the file size or file descriptor seek offset change while<i>fcntl</i>() is blocked, this shall not affect the range of bytes locked.</p><p>If <i>l_len</i> is positive, the area affected shall start at <i>l_start</i> and end at <i>l_start</i>+ <i>l_len</i>-1. If<i>l_len</i> is negative, the area affected shall start at <i>l_start</i>+ <i>l_len</i> and end at <i>l_start</i>-1. Locks maystart and extend beyond the current end of a file, but shall not extend before the beginning of the file. A lock shall be set toextend to the largest possible value of the file offset for that file by setting <i>l_len</i> to 0. If such a lock also has<i>l_start</i> set to 0 and <i>l_whence</i> is set to SEEK_SET, the whole file shall be locked.</p><p>There shall be at most one type of lock set for each byte in the file. Before a successful return from an F_SETLK or an F_SETLKWrequest when the calling process has previously existing locks on bytes in the region specified by the request, the previous locktype for each byte in the specified region shall be replaced by the new lock type. As specified above under the descriptions ofshared locks and exclusive locks, an F_SETLK or an F_SETLKW request (respectively) shall fail or block when another process hasexisting locks on bytes in the specified region and the type of any of those locks conflicts with the type specified in therequest.</p><p>All locks associated with a file for a given process shall be removed when a file descriptor for that file is closed by thatprocess or the process holding that file descriptor terminates. Locks are not inherited by a child process.</p><p>A potential for deadlock occurs if a process controlling a locked region is put to sleep by attempting to lock another process'locked region. If the system detects that sleeping until a locked region is unlocked would cause a deadlock, <i>fcntl</i>() shallfail with an [EDEADLK] error.</p><p>An unlock (F_UNLCK) request in which <i>l_len</i> is non-zero and the offset of the last byte of the requested segment is themaximum value for an object of type <b>off_t</b>, when the process has an existing lock in which <i>l_len</i> is 0 and whichincludes the last byte of the requested segment, shall be treated as a request to unlock from the start of the requested segmentwith an <i>l_len</i> equal to 0. Otherwise, an unlock (F_UNLCK) request shall attempt to unlock only the requested segment.</p><p><sup>[<a href="javascript:open_code('SHM')">SHM</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">When the file descriptor <i>fildes</i> refers to a shared memory object, the behavior of <i>fcntl</i>() shall be the same as for aregular file except the effect of the following values for the argument <i>cmd</i> shall be unspecified: F_SETFL, F_GETLK, F_SETLK,and F_SETLKW. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p><sup>[<a href="javascript:open_code('TYM')">TYM</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">If <i>fildes</i> refers to a typed memory object, the result of the <i>fcntl</i>() function is unspecified. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p></blockquote><h4><a name="tag_03_141_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, the value returned shall depend on <i>cmd</i> as follows:</p><dl compact><dt>F_DUPFD</dt><dd>A new file descriptor.</dd><dt>F_GETFD</dt><dd>Value of flags defined in <a href="../basedefs/fcntl.h.html"><i><fcntl.h></i></a>. The return value shall not benegative.</dd><dt>F_SETFD</dt><dd>Value other than -1.</dd><dt>F_GETFL</dt><dd>Value of file status flags and access modes. The return value is not negative.</dd><dt>F_SETFL</dt><dd>Value other than -1.</dd><dt>F_GETLK</dt><dd>Value other than -1.</dd><dt>F_SETLK</dt><dd>Value other than -1.</dd><dt>F_SETLKW</dt><dd>Value other than -1.</dd><dt>F_GETOWN</dt><dd>Value of the socket owner process or process group; this will not be -1.</dd><dt>F_SETOWN</dt><dd>Value other than -1.</dd></dl>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -