up | Inhaltsverzeichniss | Kommentar

Manual page for CLOSE(2)

close - delete a descriptor

SYNOPSIS

#include <libc.h> int close(int fd);

DESCRIPTION

The close system call deletes a descriptor from the per-process object reference table. If this is the last reference to the underlying object, then it will be deactivated. For example, on the last close of a file the current lseek.2 pointer associated with the file is lost; on the last close of a socket.2 its associated naming information and queued data are discarded; on the last close of a file holding any locks the locks are released (see further flock.2 and fcntl.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.2 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, the process would normally inherit these descriptors. Most of the descriptors can be rearranged with dup2 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. For this reason, the call ``fcntl(d, F_SETFD, FD_CLOEXEC)'' (POSIX) or ``fcntl(d, F_SETFD, 1)'' (BSD) is provided, which arranges that a descriptor will be closed after a successful execve; the call ``fcntl(d, F_SETFD, 0)'' restores the default, which is to not close the descriptor.

You should always check the return value of close when closing a file that might be on a remote file system, such as NFS or AFS. The reason is this: remote file systems often cache writes on the client side and return a successful error code to the process calling write.2 However, an error may occur when the cached file data is finally written. For example, the server could be out of disk space. close forces all cached writes to be completed to the server. If the call to close fails, then the file has not been written out successfully, and you should give the user the option of saving the file to a different place.

When all file descriptors associated with a pipe or FIFO special file have been closed, any data remaining in the pipe or FIFO is discarded.

RETURN VALUE

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

ERRORS

The close function will fail if:
[EBADF]
fd is not an active descriptor.
[EINTR]
The close function was interrupted by a signal.

SEE ALSO

accept(2), flock(2), open(2), pipe(2), socket(2), socketpair(2), execve(2), fcntl(2), creat(2), dup(2), fork(2), unlink(2)


index | Inhaltsverzeichniss | Kommentar

Created by unroff & hp-tools. © somebody (See intro for details). All Rights Reserved. Last modified 11/5/97