⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 library_12.html

📁 Linux程序员的工作手册
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<P>In case of an error, <CODE>fcntl</CODE> returns <CODE>-1</CODE>.  The following<CODE>errno</CODE> error conditions are defined for this command:<P><DL COMPACT><DT><CODE>EBADF</CODE><DD>The <VAR>filedes</VAR> argument is invalid.</DL><P><A NAME="IDX698"></A><U>Macro:</U> int <B>F_SETFD</B><P>This macro is used as the <VAR>command</VAR> argument to <CODE>fcntl</CODE>, tospecify that it should set the file descriptor flags associated with the<VAR>filedes</VAR> argument.  This requires a third <CODE>int</CODE> argument tospecify the new flags, so the form of the call is:<P><PRE>fcntl (<VAR>filedes</VAR>, F_SETFD, <VAR>new_flags</VAR>)</PRE><P>The normal return value from <CODE>fcntl</CODE> with this command is anunspecified value other than <CODE>-1</CODE>, which indicates an error.The flags and error conditions are the same as for the <CODE>F_GETFD</CODE>command.<P>The following macro is defined for use as a file descriptor flag withthe <CODE>fcntl</CODE> function.  The value is an integer constant usableas a bit mask value.<P><A NAME="IDX699"></A><A NAME="IDX700"></A><U>Macro:</U> int <B>FD_CLOEXEC</B><P>This flag specifies that the file descriptor should be closed whenan <CODE>exec</CODE> function is invoked; see section <A HREF="library_23.html#SEC406" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_23.html#SEC406">Executing a File</A>.  Whena file descriptor is allocated (as with <CODE>open</CODE> or <CODE>dup</CODE>),this bit is initially cleared on the new file descriptor, meaning thatdescriptor will survive into the new program after <CODE>exec</CODE>.<P>If you want to modify the file descriptor flags, you should get thecurrent flags with <CODE>F_GETFD</CODE> and modify the value.  Don't assumethat the flag listed here is the only ones that are implemented; yourprogram may be run years from now and more flags may exist then.For example, here is a function to set or clear the flag <CODE>FD_CLOEXEC</CODE>without altering any other flags:<P><PRE>/* Set the <CODE>FD_CLOEXEC</CODE> flag of <VAR>desc</VAR> if <VAR>value</VAR> is nonzero,   or clear the flag if <VAR>value</VAR> is 0.   Return 0 on success, or -1 on error with <CODE>errno</CODE> set. */ intset_cloexec_flag (int desc, int value){  int oldflags = fcntl (desc, F_GETFD, 0);  /* If reading the flags failed, return error indication now.  if (oldflags &#60; 0)    return oldflags;  /* Set just the flag we want to set. */  if (value != 0)    oldflags |= FD_CLOEXEC;  else    oldflags &#38;= ~FD_CLOEXEC;  /* Store modified flag word in the descriptor. */  return fcntl (desc, F_SETFD, oldflags);}</PRE><P><A NAME="IDX701"></A><H2><A NAME="SEC184" HREF="library_toc.html#SEC184" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC184">File Status Flags</A></H2><P><DFN>File status flags</DFN> are used to specify attributes of the opening ofa file.  Unlike the file descriptor flags discussed in section <A HREF="library_12.html#SEC183" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_12.html#SEC183">File Descriptor Flags</A>, the file status flags are shared by duplicated file descriptorsresulting from a single opening of the file.<P>The file status flags are initialized by the <CODE>open</CODE> function fromthe <VAR>flags</VAR> argument of the <CODE>open</CODE> function.  Some of the flagsare meaningful only in <CODE>open</CODE> and are not remembered subsequently;many of the rest cannot subsequently be changed, though you can readtheir values by examining the file status flags.<P>A few file status flags can be changed at any time using <CODE>fcntl</CODE>.These include <CODE>O_APPEND</CODE> and <CODE>O_NONBLOCK</CODE>.<P>The symbols in this section are defined in the header file<TT>`fcntl.h'</TT>.<A NAME="IDX702"></A><P><A NAME="IDX703"></A><U>Macro:</U> int <B>F_GETFL</B><P>This macro is used as the <VAR>command</VAR> argument to <CODE>fcntl</CODE>, toread the file status flags for the open file with descriptor<VAR>filedes</VAR>.<P>The normal return value from <CODE>fcntl</CODE> with this command is anonnegative number which can be interpreted as the bitwise OR of theindividual flags.  The flags are encoded like the <VAR>flags</VAR> argumentto <CODE>open</CODE> (see section <A HREF="library_12.html#SEC172" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_12.html#SEC172">Opening and Closing Files</A>), but only the fileaccess modes and the <CODE>O_APPEND</CODE> and <CODE>O_NONBLOCK</CODE> flags aremeaningful here.  Since the file access modes are not single-bit values,you can mask off other bits in the returned flags with <CODE>O_ACCMODE</CODE>to compare them.<P>In case of an error, <CODE>fcntl</CODE> returns <CODE>-1</CODE>.  The following<CODE>errno</CODE> error conditions are defined for this command:<P><DL COMPACT><DT><CODE>EBADF</CODE><DD>The <VAR>filedes</VAR> argument is invalid.</DL><P><A NAME="IDX704"></A><U>Macro:</U> int <B>F_SETFL</B><P>This macro is used as the <VAR>command</VAR> argument to <CODE>fcntl</CODE>, to setthe file status flags for the open file corresponding to the<VAR>filedes</VAR> argument.  This command requires a third <CODE>int</CODE>argument to specify the new flags, so the call looks like this:<P><PRE>fcntl (<VAR>filedes</VAR>, F_SETFL, <VAR>new_flags</VAR>)</PRE><P>You can't change the access mode for the file in this way; that is,whether the file descriptor was opened for reading or writing.  You canonly change the <CODE>O_APPEND</CODE> and <CODE>O_NONBLOCK</CODE> flags.<P>The normal return value from <CODE>fcntl</CODE> with this command is anunspecified value other than <CODE>-1</CODE>, which indicates an error.  Theerror conditions are the same as for the <CODE>F_GETFL</CODE> command.<P>The following macros are defined for use in analyzing and constructingfile status flag values:<P><DL COMPACT><DT><CODE>O_APPEND</CODE><DD>The bit that enables append mode for the file.  If set, then all<CODE>write</CODE> operations write the data at the end of the file, extendingit, regardless of the current file position.<P><DT><CODE>O_NONBLOCK</CODE><DD>The bit that enables nonblocking mode for the file.  If this bit is set,<CODE>read</CODE> requests on the file can return immediately with a failurestatus if there is no input immediately available, instead of blocking.Likewise, <CODE>write</CODE> requests can also return immediately with afailure status if the output can't be written immediately.<P><DT><CODE>O_NDELAY</CODE><DD>This is a synonym for <CODE>O_NONBLOCK</CODE>, provided for compatibility withBSD.</DL><P><A NAME="IDX705"></A><U>Macro:</U> int <B>O_ACCMODE</B><P>This macro stands for a mask that can be bitwise-ANDed with the filestatus flag value to produce a value representing the file access mode.The mode will be <CODE>O_RDONLY</CODE>, <CODE>O_WRONLY</CODE>, or <CODE>O_RDWR</CODE>.<P><DL COMPACT><DT><CODE>O_RDONLY</CODE><DD>Open the file for read access.<P><DT><CODE>O_WRONLY</CODE><DD>Open the file for write access.<P><DT><CODE>O_RDWR</CODE><DD>Open the file for both reading and writing.</DL><P>If you want to modify the file status flags, you should get the currentflags with <CODE>F_GETFL</CODE> and modify the value.  Don't assume that theflags listed here are the only ones that are implemented; your programmay be run years from now and more flags may exist then.  For example,here is a function to set or clear the flag <CODE>O_NONBLOCK</CODE> withoutaltering any other flags:<P><PRE>/* Set the <CODE>O_NONBLOCK</CODE> flag of <VAR>desc</VAR> if <VAR>value</VAR> is nonzero,   or clear the flag if <VAR>value</VAR> is 0.   Return 0 on success, or -1 on error with <CODE>errno</CODE> set. */ intset_nonblock_flag (int desc, int value){  int oldflags = fcntl (desc, F_GETFL, 0);  /* If reading the flags failed, return error indication now. */  if (oldflags &#60; 0)    return oldflags;  /* Set just the flag we want to set. */  if (value != 0)    oldflags |= O_NONBLOCK;  else    oldflags &#38;= ~O_NONBLOCK;  /* Store modified flag word in the descriptor. */  return fcntl (desc, F_SETFL, oldflags);}</PRE><P><H2><A NAME="SEC185" HREF="library_toc.html#SEC185" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html#SEC185">File Locks</A></H2><A NAME="IDX706"></A><A NAME="IDX707"></A><P>The remaining <CODE>fcntl</CODE> commands are used to support <DFN>recordlocking</DFN>, which permits multiple cooperating programs to prevent eachother from simultaneously accessing parts of a file in error-proneways.<A NAME="IDX708"></A><A NAME="IDX709"></A><P>An <DFN>exclusive</DFN> or <DFN>write</DFN> lock gives a process exclusive accessfor writing to the specified part of the file.  While a write lock is inplace, no other process can lock that part of the file.<A NAME="IDX710"></A><A NAME="IDX711"></A><P>A <DFN>shared</DFN> or <DFN>read</DFN> lock prohibits any other process fromrequesting a write lock on the specified part of the file.  However,other processes can request read locks.<P>The <CODE>read</CODE> and <CODE>write</CODE> functions do not actually check to seewhether there are any locks in place.  If you want to implement alocking protocol for a file shared by multiple processes, your applicationmust do explicit <CODE>fcntl</CODE> calls to request and clear locks at theappropriate points.<P>Locks are associated with processes.  A process can only have one kindof lock set for each byte of a given file.  When any file descriptor forthat file is closed by the process, all of the locks that process holdson that file are released, even if the locks were made using otherdescriptors that remain open.  Likewise, locks are released when aprocess exits, and are not inherited by child processes created using<CODE>fork</CODE> (see section <A HREF="library_23.html#SEC405" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_23.html#SEC405">Creating a Process</A>).<P>When making a lock, use a <CODE>struct flock</CODE> to specify what kind oflock and where.  This data type and the associated macros for the<CODE>fcntl</CODE> function are declared in the header file <TT>`fcntl.h'</TT>.<A NAME="IDX712"></A><P><A NAME="IDX713"></A><U>struct Type:</U> <B>flock</B><P>This structure is used with the <CODE>fcntl</CODE> function to describe a filelock.  It has these members:<P><DL COMPACT><DT><CODE>short int l_type</CODE><DD>Specifies the type of the lock; one of <CODE>F_RDLCK</CODE>, <CODE>F_WRLCK</CODE>, or<CODE>F_UNLCK</CODE>.<P><DT><CODE>short int l_whence</CODE><DD>This corresponds to the <VAR>whence</VAR> argument to <CODE>fseek</CODE> or<CODE>lseek</CODE>, and specifies what the offset is relative to.  Its valuecan be one of <CODE>SEEK_SET</CODE>, <CODE>SEEK_CUR</CODE>, or <CODE>SEEK_END</CODE>.<P><DT><CODE>off_t l_start</CODE><DD>This specifies the offset of the start of the region to which the lockapplies, and is given in bytes relative to the point specified by<CODE>l_whence</CODE> member.<P><DT><CODE>off_t l_len</CODE><DD>This specifies the length of the region to be locked.  A value of<CODE>0</CODE> is treated specially; it means the region extends to the end ofthe file.<P><DT><CODE>pid_t l_pid</CODE><DD>This field is the process ID (see section <A HREF="library_23.html#SEC403" tppabs="http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_23.html#SEC403">Process Creation Concepts</A>) of theprocess holding the lock.  It is filled in by calling <CODE>fcntl</CODE> withthe <CODE>F_GETLK</CODE> command, but is ignored when making a lock.</DL><P><A NAME="IDX714"></A><U>Macro:</U> int <B>F_GETLK</B><P>This macro is used as the <VAR>command</VAR> argument to <CODE>fcntl</CODE>, tospecify that it should get information about a

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -