Files
oldlinux-files/Ref-docs/POSIX/susv3/functions/close.html
2024-02-19 00:21:47 -05:00

274 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>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-2001<br>
Copyright &copy; 2001 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 &lt;<a href="../basedefs/unistd.h.html">unistd.h</a>&gt;<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 the
file descriptor available for return by subsequent calls to <a href="../functions/open.html"><i>open</i>()</a> or other functions
that allocate file descriptors. All outstanding record locks owned by the process on the file associated with the file descriptor
shall 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 the
state 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> is
unspecified.</p>
<p>When all file descriptors associated with a pipe or FIFO special file are closed, any data remaining in the pipe or FIFO shall
be discarded.</p>
<p>When all file descriptors associated with an open file description have been closed, the open file description shall be
freed.</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 the
file 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 events
associated 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 and
there have been no signals posted for the STREAM, and if there is data on the module's write queue, <i>close</i>() shall wait for
an unspecified time (for each module and driver) for any output to drain before dismantling the STREAM. The time delay can be
changed via an I_SETCLTIME <a href="../functions/ioctl.html"><i>ioctl</i>()</a> request. If the O_NONBLOCK flag is set, or if there
are 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 named
by <a href="../functions/fattach.html"><i>fattach</i>()</a>, then the last <i>close</i>() shall force the named end to be detached
by <a href="../functions/fdetach.html"><i>fdetach</i>()</a>. If the named end has no open file descriptors associated with it and
gets 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 the
process group, if any, for which the slave side of the pseudo-terminal is the controlling terminal. It is unspecified whether
closing 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. <img
src="../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/O
operation 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/O
operation 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 the
entire contents of the memory object shall persist until the memory object becomes unreferenced. If this is the last close of a
shared memory object or a memory mapped file and the close results in the memory object becoming unreferenced, and the memory
object 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 standard
output to a new file descriptor, and closes the original file descriptor to clean up. This example assumes that the file descriptor
0 (which is the descriptor for standard input) is not closed.</p>
<pre>
<tt>#include &lt;unistd.h&gt;
...
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 associate
that file descriptor with a stream.</p>
<pre>
<tt>#include &lt;stdio.h&gt;
#include &lt;unistd.h&gt;
#include &lt;stdlib.h&gt;
<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 should
use the corresponding <a href="../functions/fclose.html"><i>fclose</i>()</a> routine rather than <i>close</i>(). Once a file is
closed, 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 file
descriptors by <i><a href="../functions/exec.html">exec</a></i> and <a href="../functions/exit.html"><i>exit</i>()</a>. This volume
of IEEE&nbsp;Std&nbsp;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> , <a
href="ioctl.html"><i>ioctl</i>()</a> , <a href="open.html"><i>open</i>()</a> , the Base Definitions volume of
IEEE&nbsp;Std&nbsp;1003.1-2001, <a href="../basedefs/unistd.h.html"><i>&lt;unistd.h&gt;</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 and
an [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&nbsp;Std&nbsp;1003.1j-2000 by specifying that shared memory objects and
memory mapped files (and not typed memory objects) are the types of memory objects to which the paragraph on last closes
applies.</p>
</blockquote>
<div class="box"><em>End of informative text.</em></div>
<hr>
<hr size="2" noshade>
<center><font size="2"><!--footer start-->
UNIX &reg; is a registered Trademark of The Open Group.<br>
POSIX &reg; 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>