'\"macro stdmacro
.if n .pH g2.close @(#)close	40.24 of 10/10/89
.\" Copyright 1989 AT&T
.nr X
.if \nX=0 .ds x} close 2 "" "\&"
.if \nX=1 .ds x} close 2 ""
.if \nX=2 .ds x} close 2 "" "\&"
.if \nX=3 .ds x} close "" "" "\&"
.TH \*(x}
.SH NAME
\f4close\f1 \- close a file descriptor
.SH SYNOPSIS
\f4#include <unistd.h>\f1
.br
\f4int close(int fildes);\f1
.SH DESCRIPTION
.I fildes\^
is a
file descriptor
obtained from a
\f4creat\fP,
\f4open\fP,
\f4dup\fP,
\f4fcntl\fP,
or
\f4pipe\fP
system call.
\f4close\fP
closes the file descriptor indicated by \f2fildes\fP.
All outstanding record locks owned by the process (on the file indicated by
\f2fildes\fP) are removed.
.PP
When all file descriptors associated with the open file description have
been closed, the open file description is freed.
.PP
If the link count of the file is zero, when all file descriptors 
associated with the file have been closed, the space occupied by the
file is freed and the file is no longer accessible.
.PP
If a \s-1STREAMS\s0-based
[see \f4intro\fP(2)] \f2fildes\fP is closed,
and the calling process had previously registered to receive
a \f4SIGPOLL\fP signal [see \f4signal\fP(2)] for
events associated with that stream [see \f4I_SETSIG\fP in \f4streamio\fP(7)],
the calling process will be unregistered for events associated with the stream.
The last \f4close\fP for a stream causes the stream associated with
\f2fildes\f1 to be dismantled.
If 
\f4O_NDELAY\fP and \f4O_NONBLOCK\fP
are clear and there have been no signals posted for the stream,
and if there are data on the module's write queue,
\f4close\fP
waits up to 15 seconds (for each module and driver) for any output to drain 
before dismantling the stream.
The time delay can be changed via an \f4I_SETCLTIME\f1 \f4ioctl\f1 request
[see \f4streamio\f1(7)].
If \f4O_NDELAY\fP or \f4O_NONBLOCK\fP is set,
or if there are any pending signals,
\f4close\fP does not wait for output to drain, and dismantles
the stream immediately.
.PP
If \f2fildes\fP is associated with one end of a pipe, the last \f4close\fP causes
a hangup to occur on the other end of the pipe.
In addition, if the other end of the pipe has been named [see \f4fattach\f1(3C)],
the last \f4close\fP forces the named end to be detached [see \f4fdetach\f1(3C)].
If the named end has no open processes associated with it and becomes detached,
the stream associated with that end is also dismantled.
.PP
The named file is closed unless one or more of the following are true:
.TP 15
\f4EBADF\fP
.I fildes\^
is not a valid open
file descriptor.
.TP 15
\f4EINTR\fP
A signal was caught during the \f4close\fP system call.
.TP 15
\f4ENOLINK\fP
\f2fildes\f1 is on a remote machine and the link
to that machine is no longer active.
.SH "SEE ALSO"
\f4creat\fP(2), \f4dup\fP(2), \f4exec\fP(2), \f4fcntl\fP(2), \f4intro\fP(2), \f4open\fP(2), \f4pipe\fP(2), \f4signal\fP(2), \f4signal\fP(5), \f4streamio\fP(7).
.br
\f4fattach\f1(3C), \f4fdetach\f1(3C) in the \f2Programmer's
Guide: Networking Interfaces\f1.
.SH "DIAGNOSTICS"
Upon successful completion, a value of 0 is returned.
Otherwise, a value of \-1 is returned and \f4errno\fP
is set to indicate the error.
.\"	@(#)close.2	6.2 of 9/6/83
.Ee
