|
|
#include <unistd.h>int close(int fildes);
Closing a file descriptor removes one reference to the associated file. When there are no more outstanding references to the file, if the link count of the file is zero, the space occupied by the file shall be freed and the file shall no longer be 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(5)] for events associated with that stream [see I_SETSIG in streamio(7)], 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 N_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 via an I_SETCLTIME ioctl request [see streamio(7)]. 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.
If filedes refers to a socket, close causes the socket to be destroyed. If the socket is connection-oriented, and SO_LINGER is set for the socket, and the socket has untransmitted data, then close will block for up to the current linger interval until all data is transmitted.
A close executed by one thread will render the file descriptor unusable by all siblings. The close system call will block a thread that attempts to close a file descriptor that is in use (mid-system call) by a sibling.