CLOSE(2)
NAME
close − delete a descriptor
USAGE
close(d) int d;
DESCRIPTION
Close deletes descriptor d from the per-process object reference table. If this 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(2), the associated naming information and any queued data are discarded; on the last close of a file holding an advisory lock, the lock is released; see flock(2).
All of a process’s descriptors close automatically upon an exit(2), but since there is a limit on the number of active descriptors per process, close is necessary for programs that use many descriptors.
When a process forks (see fork(2)), all descriptors held by the forked child process refer to the same objects as they did in the parent. If a new process is then run using execve(2), the process normally inherits these descriptors. Most of the descriptors can be rearranged with dup2(2) or deleted with close before the execve is attempted. However, if some of these descriptors are needed in case the execve fails, you must arrange to close them if the execve succeeds. Use fcntl(2) as shown here: fcntl(d, F_SETFD, 1) to arrange for descriptor d to be closed after a successful execve, and fcntl(d, F_SETFD, 0) to restore the default, i.e., that the descriptor does not close.
RETURN VALUE
A successful call returns zero. A failed call returns -1 and sets errno as indicated below.
ERRORS
Close will fail if:
[EBADF] d is not an active descriptor.
RELATED INFORMATION
accept(2), flock(2), open(2), pipe(2), socket(2), socketpair(2), execve(2), fcntl(2)