OSDN Git Service

(split) Import translated manuals from JM CVS Repository.
[linuxjm/LDP_man-pages.git] / original / man2 / close.2
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
4 .\"                               1993 Michael Haardt, Ian Jackson.
5 .\"
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\"
26 .\" Modified Wed Jul 21 22:40:25 1993 by Rik Faith <faith@cs.unc.edu>
27 .\" Modified Sat Feb 18 15:27:48 1995 by Michael Haardt
28 .\" Modified Sun Apr 14 11:40:50 1996 by Andries Brouwer <aeb@cwi.nl>:
29 .\"   corrected description of effect on locks (thanks to
30 .\"   Tigran Aivazian <tigran@sco.com>).
31 .\" Modified Fri Jan 31 16:21:46 1997 by Eric S. Raymond <esr@thyrsus.com>
32 .\" Modified 2000-07-22 by Nicolás Lichtmaier <nick@debian.org>
33 .\"   added note about close(2) not guaranteeing that data is safe on close.
34 .\"
35 .TH CLOSE 2 2007-12-28 "Linux" "Linux Programmer's Manual"
36 .SH NAME
37 close \- close a file descriptor
38 .SH SYNOPSIS
39 .nf
40 .B #include <unistd.h>
41 .sp
42 .BI "int close(int " fd );
43 .fi
44 .SH DESCRIPTION
45 .BR close ()
46 closes a file descriptor, so that it no longer refers to any file and
47 may be reused.
48 Any record locks (see
49 .BR fcntl (2))
50 held on the file it was associated with,
51 and owned by the process, are removed (regardless of the file
52 descriptor that was used to obtain the lock).
53 .PP
54 If
55 .I fd
56 is the last file descriptor referring to the underlying
57 open file description (see
58 .BR open (2)),
59 the resources associated with the open file description are freed;
60 if the descriptor was the last reference to a file which has been
61 removed using
62 .BR unlink (2)
63 the file is deleted.
64 .SH "RETURN VALUE"
65 .BR close ()
66 returns zero on success.
67 On error, \-1 is returned, and
68 .I errno
69 is set appropriately.
70 .SH ERRORS
71 .TP
72 .B EBADF
73 .I fd
74 isn't a valid open file descriptor.
75 .TP
76 .B EINTR
77 The
78 .BR close ()
79 call was interrupted by a signal; see
80 .BR signal (7).
81 .TP
82 .B EIO
83 An I/O error occurred.
84 .SH "CONFORMING TO"
85 SVr4, 4.3BSD, POSIX.1-2001.
86 .\" SVr4 documents an additional ENOLINK error condition.
87 .SH NOTES
88 Not checking the return value of
89 .BR close ()
90 is a common but nevertheless
91 serious programming error.
92 It is quite possible that errors on a
93 previous
94 .BR write (2)
95 operation are first reported at the final
96 .BR close ().
97 Not checking the return value when closing the file may lead to
98 silent loss of data.
99 This can especially be observed with NFS
100 and with disk quota.
101 .PP
102 A successful close does not guarantee that the data has been successfully
103 saved to disk, as the kernel defers writes.
104 It is not common for a file system
105 to flush the buffers when the stream is closed.
106 If you need to be sure that
107 the data is physically stored use
108 .BR fsync (2).
109 (It will depend on the disk hardware at this point.)
110 .PP
111 It is probably unwise to close file descriptors while
112 they may be in use by system calls in
113 other threads in the same process.
114 Since a file descriptor may be reused,
115 there are some obscure race conditions
116 that may cause unintended side effects.
117 .\" Date: Tue, 4 Sep 2007 13:57:35 +0200
118 .\" From: Fredrik Noring <noring@nocrew.org>
119 .\" One such race involves signals and ERESTARTSYS. If a file descriptor
120 .\" in use by a system call is closed and then reused by e.g. an
121 .\" independent open() in some unrelated thread, before the original system
122 .\" call has restared after ERESTARTSYS, the original system call will
123 .\" later restart with the reused file descriptor. This is most likely a
124 .\" serious programming error.
125 .SH "SEE ALSO"
126 .BR fcntl (2),
127 .BR fsync (2),
128 .BR open (2),
129 .BR shutdown (2),
130 .BR unlink (2),
131 .BR fclose (3)