close(2)


close -- close a file descriptor

Synopsis

   #include <unistd.h>
   

int close(int fildes);

Description

close closes a file. fildes is a file descriptor obtained from a creat, open, dup, fcntl, pipe, or iocntl system call. close closes the file descriptor indicated by fildes. All outstanding record locks owned by the process (on the file indicated by fildes) are removed.

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.

Return values

On success, close returns 0. On failure, close returns -1 and sets errno to identify the error.

Errors

In the following conditions, close fails and sets errno to:

EBADF
fildes is not a valid open file descriptor.

EINTR
A signal was caught during the close system call.

ENOLINK
fildes is on a remote machine and the link to that machine is no longer active.

References

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

Notices

Considerations for threads programming

Open file descriptors are global to the process and accessible to any sibling thread. If used concurrently, actions by one thread can interfere with those of a sibling.

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.


© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 25 April 2004