1CLOSE(2)                   Linux Programmer's Manual                  CLOSE(2)
2
3
4

NAME

6       close - close a file descriptor
7

SYNOPSIS

9       #include <unistd.h>
10
11       int close(int fd);
12

DESCRIPTION

14       close()  closes  a  file descriptor, so that it no longer refers to any
15       file and may be reused.  Any record locks (see fcntl(2))  held  on  the
16       file  it  was  associated  with,  and owned by the process, are removed
17       (regardless of the file descriptor that was used to obtain the lock).
18
19       If fd is the last file descriptor referring to the underlying open file
20       description  (see open(2)), the resources associated with the open file
21       description are freed; if the descriptor was the last  reference  to  a
22       file which has been removed using unlink(2) the file is deleted.
23

RETURN VALUE

25       close()  returns  zero on success.  On error, -1 is returned, and errno
26       is set appropriately.
27

ERRORS

29       EBADF  fd isn't a valid open file descriptor.
30
31       EINTR  The close() call was interrupted by a signal; see signal(7).
32
33       EIO    An I/O error occurred.
34

CONFORMING TO

36       SVr4, 4.3BSD, POSIX.1-2001.
37

NOTES

39       Not checking the return value of close() is a common  but  nevertheless
40       serious  programming error.  It is quite possible that errors on a pre‐
41       vious write(2) operation are first reported at the final close().   Not
42       checking the return value when closing the file may lead to silent loss
43       of data.  This can especially be observed with NFS and with disk quota.
44
45       A successful close does not guarantee that the data has  been  success‐
46       fully saved to disk, as the kernel defers writes.  It is not common for
47       a file system to flush the buffers when the stream is closed.   If  you
48       need  to  be sure that the data is physically stored use fsync(2).  (It
49       will depend on the disk hardware at this point.)
50
51       It is probably unwise to close file descriptors while they  may  be  in
52       use by system calls in other threads in the same process.  Since a file
53       descriptor may be re-used, there are some obscure race conditions  that
54       may cause unintended side effects.
55
56       When dealing with sockets, you have to be sure that there is no recv(2)
57       still blocking on it on another thread, otherwise it might  block  for‐
58       ever,  since  no  more messages will be sent via the socket. Be sure to
59       use shutdown(2) to shut down all parts the  connection  before  closing
60       the socket.
61

SEE ALSO

63       fcntl(2), fsync(2), open(2), shutdown(2), unlink(2), fclose(3)
64

COLOPHON

66       This  page  is  part of release 3.22 of the Linux man-pages project.  A
67       description of the project, and information about reporting  bugs,  can
68       be found at http://www.kernel.org/doc/man-pages/.
69
70
71
72Linux                             2007-12-28                          CLOSE(2)
Impressum