close - delete a descriptor
int close (fd)
close() deletes a descriptor from the per-process object reference ta-
ble. If fd is the last reference to the underlying object, then the
object will be deactivated. For example, on the last close of a file
the current seek pointer associated with the file is lost. On the last
close of a socket (see socket(2)), associated naming information and
queued data are discarded. On the last close of a file holding an
advisory lock applied by flock(2), the lock is released. (Record locks
applied to the file by lockf(3), however, are released on any call to
close() regardless of whether fd is the last reference to the underly-
close() does not unmap any mapped pages of the object referred to by fd
(see mmap(), munmap(2)).
A close of all of a process's descriptors is automatic on exit(), but
since there is a limit on the number of active descriptors per process,
close() is necessary for programs that deal with many descriptors.
When a process forks (see fork(2v)), all descriptors for the new child
process reference the same objects as they did in the parent before the
fork. If a new process is then to be run using execve(2V), the process
would normally inherit these descriptors. Most of the descriptors can
be rearranged with dup(2V) or deleted with close() before the execve()
is attempted, but if some of these descriptors will still be needed if
the execve() fails, it is necessary to arrange for them to be closed if
the execve() succeeds. The fcntl(2V) operation F_SETFD can be used to
arrange that a descriptor will be closed after a successful execve(),
or to restore the default behavior, which is to not close the descrip-
If a STREAMS (see intro(2)) file is closed, and the calling process had
previously registered to receive a SIGPOLL signal (see sigvec(2)) for
events associated with that file (see I_SETSIG in streamio(4)), the
calling process will be unregistered for events associated with the
file. The last close() for a stream causes that stream to be disman-
tled. If the descriptor is not marked for no-delay mode and there have
been no signals posted for the stream, close() waits up to 15 seconds,
for each module and driver, for any output to drain before dismantling
the stream. If the descriptor is marked for no-delay mode or if there
are any pending signals, close() does not wait for output to drain, and
dismantles the stream immediately.
0 on success.
-1 on failure and sets errno to indicate the error.
EBADF fd is not an active descriptor.
EINTR A signal was caught before the close completed.
accept(2), dup(2V), execve(2V), fcntl(2V), flock(2), intro(2),
open(2V), pipe(2V), sigvec(2), socket(2), socketpair(2), streamio(4)
21 January 1990 CLOSE(2V)