[Go to CFHT Home Page] Man Pages
Back to Software Index  BORDER=0Manpage Top Level
    close(2) manual page Table of Contents

Name

close - close a file descriptor

Synopsis

#include <unistd.h>

int close(int fildes);

MT-Level

Async-Signal-Safe

Description

close() closes the file descriptor indicated by fildes. All outstanding record locks owned by the process (on the file indicated by fildes) are removed. fildes is an open file descriptor.

When all file descriptors associated with the open file description have been closed, the open file description is freed.

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.

If a STREAMS -based (see intro(2) ) fildes is closed, and the calling process had previously registered to receive a SIGPOLL signal (see signal(3C) ) for events associated with that stream (see I_SETSIG in streamio(7I) ), the calling process will be unregistered for events associated with the stream. The last close() for a stream causes the stream associated with fildes to be dismantled. If O_NDELAY and O_NONBLOCK are clear and there have been no signals posted for the stream, and if there are data on the module’s write queue, close() 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 using an I_SETCLTIME ioctl request (see streamio(7I) ). If O_NDELAY or O_NONBLOCK is set, or if there are any pending signals, close() does not wait for output to drain, and dismantles the stream immediately.

If fildes is associated with one end of a pipe, the last close() 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 fattach(3C) ), the last close() forces the named end to be detached (see fdetach(3C) ). If the named end has no open processes associated with it and becomes detached, the stream associated with that end is also dismantled.

Return Values

Upon successful completion, a value of 0 is returned. Otherwise, a value of -1 is returned and errno is set to indicate the error.

Errors

The named file is closed unless one or more of the following are true:

EBADF
fildes is not a valid open file descriptor.
EINTR
A signal was caught during the close() function.
ENOLINK
fildes is on a remote machine and the link to that machine is no longer active.
EIO
Data was not written out properly.

See Also

intro(2) , creat(2) , dup(2) , exec(2) , fcntl(2) , open(2) , pipe(2) , fattach(3C) , fdetach(3C) , signal(3C) , signal(5) , streamio(7I)

See ‘‘File Description’’ in intro(2) .


Table of Contents