1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" This manpage is Copyright (C) 1992 Drew Eckhardt;
4 .\" 1993 Michael Haardt, Ian Jackson.
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.
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
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
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" 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 2001-12-13 "" "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
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).
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
61 returns zero on success, or \-1 if an error occurred.
66 isn't a valid open file descriptor.
71 call was interrupted by a signal.
74 An I/O error occurred.
76 SVr4, SVID, POSIX, X/OPEN, BSD 4.3. SVr4 documents an additional
77 ENOLINK error condition.
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
83 operation are first reported at the final
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
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
94 (It will depend on the disk hardware at this point.)