BSD 4_2 development
[unix-history] / usr / man / man2 / close.2
CommitLineData
ad2ca886
C
1.TH CLOSE 2 "27 July 1983"
2.UC 4
3.SH NAME
4close \- delete a descriptor
5.SH SYNOPSIS
6.B close(d)
7.br
8.B "int d;"
9.SH DESCRIPTION
10The
11\fIclose\fP call deletes a descriptor from the per-process object
12reference table.
13If this is the last reference to the underlying object, then
14it will be deactivated.
15For example, on the last close of a file
16the current \fIseek\fP pointer associated with the file is lost;
17on the last close of a
18.IR socket (2)
19associated naming information and queued data are discarded;
20on the last close of a file holding an advisory lock
21the lock is released; see further
22.IR flock (2).
23.PP
24A close of all of a process's descriptors is automatic on
25.IR exit ,
26but since
27there is a limit on the number of active descriptors per process,
28.I close
29is necessary for programs which deal with many descriptors.
30.PP
31When a process forks (see
32.IR fork (2)),
33all descriptors for the new child process reference the same
34objects as they did in the parent before the fork.
35If a new process is then to be run using
36.IR execve (2),
37the process would normally inherit these descriptors. Most
38of the descriptors can be rearranged with
39.IR dup2 (2)
40or deleted with
41.I close
42before the
43.I execve
44is attempted, but if some of these descriptors will still
45be needed if the execve fails, it is necessary to arrange for them
46to be closed if the execve succeeds.
47For this reason, the call ``fcntl(d, F_SETFD, 1)'' is provided
48which arranges that a descriptor will be closed after a successful
49execve; the call ``fcntl(d, F_SETFD, 0)'' restores the default,
50which is to not close the descriptor.
51.SH "RETURN VALUE
52Upon successful completion, a value of 0 is returned.
53Otherwise, a value of \-1 is returned and the global integer variable
54.I errno
55is set to indicate the error.
56.SH ERRORS
57.I Close
58will fail if:
59.TP 15
60[EBADF]
61\fID\fP is not an active descriptor.
62.SH "SEE ALSO"
63accept(2), flock(2), open(2), pipe(2), socket(2), socketpair(2),
64execve(2), fcntl(2)