1CLOSE(2) System Calls Manual CLOSE(2)
2
3
4
6 close - delete a descriptor
7
9 close(d)
10 int d;
11
13 The close call deletes a descriptor from the per-process object refer‐
14 ence table. If this is the last reference to the underlying object,
15 then it will be deactivated. For example, on the last close of a file
16 the current seek pointer associated with the file is lost; on the last
17 close of a socket(2) associated naming information and queued data are
18 discarded; on the last close of a file holding an advisory lock the
19 lock is released (see further flock(2)).
20
21 A close of all of a process's descriptors is automatic on exit, but
22 since there is a limit on the number of active descriptors per process,
23 close is necessary for programs that deal with many descriptors.
24
25 When a process forks (see fork(2)), all descriptors for the new child
26 process reference the same objects as they did in the parent before the
27 fork. If a new process is then to be run using execve(2), the process
28 would normally inherit these descriptors. Most of the descriptors can
29 be rearranged with dup2(2) or deleted with close before the execve is
30 attempted, but if some of these descriptors will still be needed if the
31 execve fails, it is necessary to arrange for them to be closed if the
32 execve succeeds. For this reason, the call ``fcntl(d, F_SETFD, 1)'' is
33 provided, which arranges that a descriptor will be closed after a suc‐
34 cessful execve; the call ``fcntl(d, F_SETFD, 0)'' restores the default,
35 which is to not close the descriptor.
36
38 Upon successful completion, a value of 0 is returned. Otherwise, a
39 value of -1 is returned and the global integer variable errno is set to
40 indicate the error.
41
43 Close will fail if:
44
45 [EBADF] D is not an active descriptor.
46
48 accept(2), flock(2), open(2), pipe(2), socket(2), socketpair(2),
49 execve(2), fcntl(2)
50
51
52
534th Berkeley Distribution May 22, 1986 CLOSE(2)