Imported upstream version 1.5
[manpages-zh.git] / raw / man2 / close.2
blobbec96d44cbe4b93aa7311233386103bd9e32b72d
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 2001-12-13 "" "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 .B close
46 closes a file descriptor, so that it no longer refers to any file and
47 may be reused. Any locks held on the file it was associated with,
48 and owned by the process, are removed (regardless of the file
49 descriptor that was used to obtain the lock).
50 .PP
52 .I fd
53 is the last copy of a particular file descriptor the resources
54 associated with it are freed;
55 if the descriptor was the last reference to a file which has been
56 removed using
57 .BR unlink (2)
58 the file is deleted.
59 .SH "RETURN VALUE"
60 .B close
61 returns zero on success, or \-1 if an error occurred.
62 .SH ERRORS
63 .TP
64 .B EBADF
65 .I fd
66 isn't a valid open file descriptor.
67 .TP
68 .B EINTR
69 The
70 .BR close ()
71 call was interrupted by a signal.
72 .TP
73 .B EIO
74 An I/O error occurred.
75 .SH "CONFORMING TO"
76 SVr4, SVID, POSIX, X/OPEN, BSD 4.3.  SVr4 documents an additional
77 ENOLINK error condition.
78 .SH NOTES
79 Not checking the return value of close is a common but nevertheless
80 serious programming error.  It is quite possible that errors on a
81 previous
82 .BR write (2)
83 operation are first reported at the final
84 .BR close .
85 Not checking the return value when closing the file may lead to
86 silent loss of data.  This can especially be observed with NFS
87 and disk quotas.
88 .PP
89 A successful close does not guarantee that the data has been successfully
90 saved to disk, as the kernel defers writes. It is not common for a filesystem
91 to flush the buffers when the stream is closed. If you need to be sure that
92 the data is physically stored use
93 .BR fsync (2).
94 (It will depend on the disk hardware at this point.)
95 .SH "SEE ALSO"
96 .BR open (2),
97 .BR fcntl (2),
98 .BR shutdown (2),
99 .BR unlink (2),
100 .BR fclose (3),
101 .BR fsync (2)