int close (fd) int fd;
close() deletes a descriptor from the per-process object reference table. If fd 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 (see socket.2 associated naming information and queued data are discarded. On the last close of a file holding an advisory lock applied by flock.2 the lock is released. (Record locks applied to the file by lockf.3 however, are released on any call to close() regardless of whether fd is the last reference to the underlying object.)
close() does not unmap any mapped pages of the object referred to by fd (see mmap(), munmap.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.2v 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.2v the process would normally inherit these descriptors. Most of the descriptors can be rearranged with dup.2v 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. The fcntl.2v operation F_SETFD can be used to arrange that a descriptor will be closed after a successful execve(), or to restore the default behavior, which is to not close the descriptor.
If a STREAMS (see intro.2 file is closed, and the calling process had previously registered to receive a SIGPOLL signal (see sigvec.2 for events associated with that file (see I_SETSIG in streamio.4 the calling process will be unregistered for events associated with the file. The last close() for a stream causes that stream to be dismantled. If the descriptor is not marked for no-delay mode and there have been no signals posted for the stream, close() waits up to 15 seconds, for each module and driver, for any output to drain before dismantling the stream. If the descriptor is marked for no-delay mode or if there are any pending signals, close() does not wait for output to drain, and dismantles the stream immediately.
Created by unroff & hp-tools. © by Hans-Peter Bischof. All Rights Reserved (1997).
Last modified 21/April/97