1 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
2 .\" and Copyright (C) 1993 Michael Haardt, Ian Jackson.
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
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.
35 .TH CLOSE 2 2013-12-30 "Linux" "Linux Programmer's Manual"
37 close \- close a file descriptor
40 .B #include <unistd.h>
42 .BI "int close(int " fd );
46 closes a file descriptor, so that it no longer refers to any file and
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).
56 is the last file descriptor referring to the underlying
57 open file description (see
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
66 returns zero on success.
67 On error, \-1 is returned, and
74 isn't a valid open file descriptor.
79 call was interrupted by a signal; see
83 An I/O error occurred.
85 SVr4, 4.3BSD, POSIX.1-2001.
86 .\" SVr4 documents an additional ENOLINK error condition.
88 Not checking the return value of
90 is a common but nevertheless
91 serious programming error.
92 It is quite possible that errors on a
95 operation are first reported at the final
97 Not checking the return value when closing the file may lead to
99 This can especially be observed with NFS
101 Note that the return value should only be used for diagnostics.
104 should not be retried after an
106 since this may cause a reused descriptor from another thread to be closed.
108 A successful close does not guarantee that the data has been successfully
109 saved to disk, as the kernel defers writes.
110 It is not common for a filesystem
111 to flush the buffers when the stream is closed.
112 If you need to be sure that
113 the data is physically stored, use
115 (It will depend on the disk hardware at this point.)
117 It is probably unwise to close file descriptors while
118 they may be in use by system calls in
119 other threads in the same process.
120 Since a file descriptor may be reused,
121 there are some obscure race conditions
122 that may cause unintended side effects.
123 .\" Date: Tue, 4 Sep 2007 13:57:35 +0200
124 .\" From: Fredrik Noring <noring@nocrew.org>
125 .\" One such race involves signals and ERESTARTSYS. If a file descriptor
126 .\" in use by a system call is closed and then reused by e.g. an
127 .\" independent open() in some unrelated thread, before the original system
128 .\" call has restarted after ERESTARTSYS, the original system call will
129 .\" later restart with the reused file descriptor. This is most likely a
130 .\" serious programming error.
139 This page is part of release 3.78 of the Linux
142 A description of the project,
143 information about reporting bugs,
144 and the latest version of this page,
146 \%http://www.kernel.org/doc/man\-pages/.