close(2) System Calls Manual close(2) close - LIBRARY Standard C library (libc, -lc) #include int close(int fd); close() closes a file descriptor, so that it no longer refers to any file and may be reused. Any record locks (see fcntl(2)) held on the file it was associated with, and owned by the process, are removed regardless of the file descriptor that was used to obtain the lock. This has some unfortunate consequences and one should be extra careful when using advisory record locking. See fcntl(2) for discussion of the risks and consequences as well as for the (probably preferred) open file description locks. fd - , , (. open(2)), , , ; , unlink(2), . close() returns zero on success. On error, -1 is returned, and errno is set to indicate the error. EBADF fd . EINTR close() ; . signal(7). EIO -. ENOSPC EDQUOT NFS , , , , write(), fsync(2) close(). close() . POSIX.1-2008. POSIX.1-2001, SVr4, 4.3BSD. , , . , . , fsync(2) ( ). --exec , execve(2); fcntl(2). Multithreaded processes and close() , . , , . Furthermore, consider the following scenario where two threads are performing operations on the same file descriptor: (1) One thread is blocked in an I/O system call on the file descriptor. For example, it is trying to write(2) to a pipe that is already full, or trying to read(2) from a stream socket which currently has no available data. (2) Another thread closes the file descriptor. The behavior in this situation varies across systems. On some systems, when the file descriptor is closed, the blocking system call returns immediately with an error. On Linux (and possibly some other systems), the behavior is different: the blocking I/O system call holds a reference to the underlying open file description, and this reference keeps the description open until the I/O system call completes. (See open(2) for a discussion of open file descriptions.) Thus, the blocking system call in the first thread may successfully complete after the close() in the second thread. , close() close() , , write(2) close(), . . NFS . , (. ., , , , - -) (, ). close() , . - , Linux , ; , , , . ( EBADF, ) , close(). POSIX.1 , . , -, close() fsync(2). EINTR . EINTR POSIX.1-2008 : close() , , -1, errno EINTR; fildes . , Linux , , close(), . , : EINTR ( , close() HP-UX). close() , . , close() EINTR , , close() . POSIX.1. . close_range(2), fcntl(2), fsync(2), open(2), shutdown(2), unlink(2), fclose(3) Azamat Hackimov , Dmitriy S. Seregin , Dmitry Bolkhovskikh , Katrin Kutepova , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . close(2)