📄 close.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-2004 IEEE and The Open Group, All Rights Reserved --><title>close</title></head><body bgcolor="white"><script type="text/javascript" language="JavaScript" src="../jscript/codes.js"></script><basefont size="3"> <a name="close"></a> <a name="tag_03_80"></a><!-- close --> <!--header start--><center><font size="2">The Open Group Base Specifications Issue 6<br>IEEE Std 1003.1, 2004 Edition<br>Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.</font></center><!--header end--><hr size="2" noshade><h4><a name="tag_03_80_01"></a>NAME</h4><blockquote>close - close a file descriptor</blockquote><h4><a name="tag_03_80_02"></a>SYNOPSIS</h4><blockquote class="synopsis"><p><code><tt>#include <<a href="../basedefs/unistd.h.html">unistd.h</a>><br><br> int close(int</tt> <i>fildes</i><tt>);<br></tt></code></p></blockquote><h4><a name="tag_03_80_03"></a>DESCRIPTION</h4><blockquote><p>The <i>close</i>() function shall deallocate the file descriptor indicated by <i>fildes</i>. To deallocate means to make thefile descriptor available for return by subsequent calls to <a href="../functions/open.html"><i>open</i>()</a> or other functionsthat allocate file descriptors. All outstanding record locks owned by the process on the file associated with the file descriptorshall be removed (that is, unlocked).</p><p>If <i>close</i>() is interrupted by a signal that is to be caught, it shall return -1 with <i>errno</i> set to [EINTR] and thestate of <i>fildes</i> is unspecified. If an I/O error occurred while reading from or writing to the file system during<i>close</i>(), it may return -1 with <i>errno</i> set to [EIO]; if this error is returned, the state of <i>fildes</i> isunspecified.</p><p>When all file descriptors associated with a pipe or FIFO special file are closed, any data remaining in the pipe or FIFO shallbe discarded.</p><p>When all file descriptors associated with an open file description have been closed, the open file description shall befreed.</p><p>If the link count of the file is 0, when all file descriptors associated with the file are closed, the space occupied by thefile shall be freed and the file shall no longer be accessible.</p><p><sup>[<a href="javascript:open_code('XSR')">XSR</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">If a STREAMS-based <i>fildes</i> is closed and the calling process was previously registered to receive a SIGPOLL signal for eventsassociated with that STREAM, the calling process shall be unregistered for events associated with the STREAM. The last<i>close</i>() for a STREAM shall cause the STREAM associated with <i>fildes</i> to be dismantled. If O_NONBLOCK is not set andthere have been no signals posted for the STREAM, and if there is data on the module's write queue, <i>close</i>() shall wait foran unspecified time (for each module and driver) for any output to drain before dismantling the STREAM. The time delay can bechanged via an I_SETCLTIME <a href="../functions/ioctl.html"><i>ioctl</i>()</a> request. If the O_NONBLOCK flag is set, or if thereare any pending signals, <i>close</i>() shall not wait for output to drain, and shall dismantle the STREAM immediately.</p><p>If the implementation supports STREAMS-based pipes, and <i>fildes</i> is associated with one end of a pipe, the last<i>close</i>() shall cause a hangup to occur on the other end of the pipe. In addition, if the other end of the pipe has been namedby <a href="../functions/fattach.html"><i>fattach</i>()</a>, then the last <i>close</i>() shall force the named end to be detachedby <a href="../functions/fdetach.html"><i>fdetach</i>()</a>. If the named end has no open file descriptors associated with it andgets detached, the STREAM associated with that end shall also be dismantled. <img src="../images/opt-end.gif" alt="[Option End]"border="0"></p><p><sup>[<a href="javascript:open_code('XSI')">XSI</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">If <i>fildes</i> refers to the master side of a pseudo-terminal, and this is the last close, a SIGHUP signal shall be sent to thecontrolling process, if any, for which the slave side of the pseudo-terminal is the controlling terminal. It is unspecified whetherclosing the master side of the pseudo-terminal flushes all queued input and output. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p><sup>[<a href="javascript:open_code('XSR')">XSR</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">If <i>fildes</i> refers to the slave side of a STREAMS-based pseudo-terminal, a zero-length message may be sent to the master. <imgsrc="../images/opt-end.gif" alt="[Option End]" border="0"></p><p><sup>[<a href="javascript:open_code('AIO')">AIO</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">When there is an outstanding cancelable asynchronous I/O operation against <i>fildes</i> when <i>close</i>() is called, that I/Ooperation may be canceled. An I/O operation that is not canceled completes as if the <i>close</i>() operation had not yet occurred.All operations that are not canceled shall complete as if the <i>close</i>() blocked until the operations completed. The<i>close</i>() operation itself need not block awaiting such I/O completion. Whether any I/O operation is canceled, and which I/Ooperation may be canceled upon <i>close</i>(), is implementation-defined. <img src="../images/opt-end.gif" alt="[Option End]"border="0"></p><p><sup>[<a href="javascript:open_code('MF')">MF|SHM</a>]</sup> <img src="../images/opt-start.gif" alt="[Option Start]" border="0">If a shared memory object or a memory mapped file remains referenced at the last close (that is, a process has it mapped), then theentire contents of the memory object shall persist until the memory object becomes unreferenced. If this is the last close of ashared memory object or a memory mapped file and the close results in the memory object becoming unreferenced, and the memoryobject has been unlinked, then the memory object shall be removed. <img src="../images/opt-end.gif" alt="[Option End]" border="0"></p><p>If <i>fildes</i> refers to a socket, <i>close</i>() shall cause the socket to be destroyed. If the socket is in connection-mode,and the SO_LINGER option is set for the socket with non-zero linger time, and the socket has untransmitted data, then<i>close</i>() shall block for up to the current linger interval until all data is transmitted.</p></blockquote><h4><a name="tag_03_80_04"></a>RETURN VALUE</h4><blockquote><p>Upon successful completion, 0 shall be returned; otherwise, -1 shall be returned and <i>errno</i> set to indicate the error.</p></blockquote><h4><a name="tag_03_80_05"></a>ERRORS</h4><blockquote><p>The <i>close</i>() function shall fail if:</p><dl compact><dt>[EBADF]</dt><dd>The <i>fildes</i> argument is not a valid file descriptor.</dd><dt>[EINTR]</dt><dd>The <i>close</i>() function was interrupted by a signal.</dd></dl><p>The <i>close</i>() function may fail if:</p><dl compact><dt>[EIO]</dt><dd>An I/O error occurred while reading from or writing to the file system.</dd></dl></blockquote><hr><div class="box"><em>The following sections are informative.</em></div><h4><a name="tag_03_80_06"></a>EXAMPLES</h4><blockquote><h5><a name="tag_03_80_06_01"></a>Reassigning a File Descriptor</h5><p>The following example closes the file descriptor associated with standard output for the current process, re-assigns standardoutput to a new file descriptor, and closes the original file descriptor to clean up. This example assumes that the file descriptor0 (which is the descriptor for standard input) is not closed.</p><pre><tt>#include <unistd.h>...int pfd;...close(1);dup(pfd);close(pfd);...</tt></pre><p>Incidentally, this is exactly what could be achieved using:</p><pre><tt>dup2(pfd, 1);close(pfd);</tt></pre><h5><a name="tag_03_80_06_02"></a>Closing a File Descriptor</h5><p>In the following example, <i>close</i>() is used to close a file descriptor after an unsuccessful attempt is made to associatethat file descriptor with a stream.</p><pre><tt>#include <stdio.h>#include <unistd.h>#include <stdlib.h><br>#define LOCKFILE "/etc/ptmp"...int pfd;FILE *fpfd;...if ((fpfd = fdopen (pfd, "w")) == NULL) { close(pfd); unlink(LOCKFILE); exit(1);}...</tt></pre></blockquote><h4><a name="tag_03_80_07"></a>APPLICATION USAGE</h4><blockquote><p>An application that had used the <i>stdio</i> routine <a href="../functions/fopen.html"><i>fopen</i>()</a> to open a file shoulduse the corresponding <a href="../functions/fclose.html"><i>fclose</i>()</a> routine rather than <i>close</i>(). Once a file isclosed, the file descriptor no longer exists, since the integer corresponding to it no longer refers to a file.</p></blockquote><h4><a name="tag_03_80_08"></a>RATIONALE</h4><blockquote><p>The use of interruptible device close routines should be discouraged to avoid problems with the implicit closes of filedescriptors by <i><a href="../functions/exec.html">exec</a></i> and <a href="../functions/exit.html"><i>exit</i>()</a>. This volumeof IEEE Std 1003.1-2001 only intends to permit such behavior by specifying the [EINTR] error condition.</p></blockquote><h4><a name="tag_03_80_09"></a>FUTURE DIRECTIONS</h4><blockquote><p>None.</p></blockquote><h4><a name="tag_03_80_10"></a>SEE ALSO</h4><blockquote><p><a href="xsh_chap02_06.html#tag_02_06"><i>STREAMS</i></a>, <a href="fattach.html"><i>fattach</i>()</a>, <a href="fclose.html"><i>fclose</i>()</a>, <a href="fdetach.html"><i>fdetach</i>()</a>, <a href="fopen.html"><i>fopen</i>()</a>, <ahref="ioctl.html"><i>ioctl</i>()</a>, <a href="open.html"><i>open</i>()</a>, the Base Definitions volume ofIEEE Std 1003.1-2001, <a href="../basedefs/unistd.h.html"><i><unistd.h></i></a></p></blockquote><h4><a name="tag_03_80_11"></a>CHANGE HISTORY</h4><blockquote><p>First released in Issue 1. Derived from Issue 1 of the SVID.</p></blockquote><h4><a name="tag_03_80_12"></a>Issue 5</h4><blockquote><p>The DESCRIPTION is updated for alignment with the POSIX Realtime Extension.</p></blockquote><h4><a name="tag_03_80_13"></a>Issue 6</h4><blockquote><p>The DESCRIPTION related to a STREAMS-based file or pseudo-terminal is marked as part of the XSI STREAMS Option Group.</p><p>The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:</p><ul><li><p>The [EIO] error condition is added as an optional error.</p></li><li><p>The DESCRIPTION is updated to describe the state of the <i>fildes</i> file descriptor as unspecified if an I/O error occurs andan [EIO] error condition is returned.</p></li></ul><p>Text referring to sockets is added to the DESCRIPTION.</p><p>The DESCRIPTION is updated for alignment with IEEE Std 1003.1j-2000 by specifying that shared memory objects andmemory mapped files (and not typed memory objects) are the types of memory objects to which the paragraph on last closesapplies.</p><p>IEEE Std 1003.1-2001/Cor 1-2002, item XSH/TC1/D6/12 is applied, correcting the XSH shaded text relating to the masterside of a pseudo-terminal. The reason for the change is that the behavior of pseudo-terminals and regular terminals should be asmuch alike as possible in this case; the change achieves that and matches historical behavior.</p></blockquote><div class="box"><em>End of informative text.</em></div><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>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -