close(2)
๐ close(2) โ bestie we're closing a file descriptor ๐
so like... this is the close(2) page from the Linux man-pages, section 2 (system calls, if you didn't know ๐ซถ). it lives in the System Calls Manual. buckle up, it's giving "short function, absolutely unhinged caveats" energy ๐
โจ NAME โจ
close โ close a file descriptor. that's it. that's the whole job. iconic behavior honestly ๐
๐ LIBRARY ๐
Standard C library (libc, -lc) โ the classic. she's always been there for you ๐
๐ธ SYNOPSIS ๐ธ
#include <unistd.h>
int close(int fd);
๐ฆ DESCRIPTION ๐ฆ
okay so close() closes a file descriptor, meaning it no longer refers to any file and it may be reused (key word: may, bestie, more on that chaos later ๐ญ).
here's the tea: any record locks (peep fcntl(2)) that are held on the file it was associated with, and that are owned by the process, get removed โ regardless of which file descriptor was actually used to grab that lock. the way this has unfortunate consequences... yeah. one should be extra careful when using advisory record locking. head to fcntl(2) for the full discussion of the risks and consequences, plus the (probably preferred) open file description locks โจ
now if fd is the last file descriptor referring to the underlying open file description (see open(2)), the resources tied to that open file description get freed. and if that file descriptor was the last reference to a file that's already been removed using unlink(2)? the file is deleted, periodt ๐
๐ RETURN VALUE ๐
- returns zero on success โจ
- on error,
-1is returned, anderrnois set to tell you what went wrong ๐ญ
๐ ERRORS ๐
EBADFโfdisn't a valid open file descriptor. it's giving "who even are you" ๐EINTRโ theclose()call was interrupted by a signal; seesignal(7)๐EIOโ an I/O error occurred. she's not well ๐ENOSPCEDQUOTโ on NFS, these errors are not normally reported against the firstwrite(2)which exceeds the available storage space, but instead against a subsequentwrite(2),fsync(2), orclose(). sneaky behavior fr fr
also go read CAVEATS for the discussion of why close() should not be retried after an error. this is important, bestie, don't skip it ๐จ
โญ STANDARDS โญ
POSIX.1-2008. certified, validated, standardized, slay โจ
๐ HISTORY ๐
4.3BSD, SVr4, POSIX.1-1988. she's been around since forever and she's still thriving ๐
๐ NOTES ๐
the close-on-exec file descriptor flag can be used to make sure a file descriptor is automatically closed upon a successful execve(2) โ see fcntl(2) for the details. set it and forget it energy ๐ง
๐จ CAVEATS ๐จ
okay this is the main character section, pay attention ๐ค
a successful close does not guarantee that the data has been successfully saved to disk, because the kernel uses the buffer cache to defer writes. typically, filesystems do not flush buffers when a file is closed. so if you need to be sure the data is physically stored on the underlying disk, use fsync(2). (and even then, at that point it'll depend on the disk hardware, so ๐ญ)
๐งต Multithreaded processes and close() ๐งต
ngl it is probably unwise to close file descriptors while they may be in use by system calls in other threads in the same process. since a file descriptor may be reused, there are some obscure race conditions that may cause unintended side effects. not the vibe. ๐
furthermore, consider the following scenario where two threads are performing operations on the same file descriptor:
- 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 toread(2)from a stream socket which currently has no available data. ๐ค - 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.) so the blocking system call in the first thread may successfully complete after the close() in the second thread. plot twist ๐ฅ
๐ญ Dealing with error returns from close() ๐ญ
a careful programmer will check the return value of close(), since it's quite possible that errors on a previous write(2) operation are reported only on the final close() that releases the open file description. failing to check the return value when closing a file may lead to silent loss of data. this can especially be observed with NFS and with disk quota. that's the scary part fr fr ๐
note, however, that a failure return should be used only for diagnostic purposes (i.e., a warning to the application that there may still be I/O pending or there may have been failed I/O) or remedial purposes (e.g., writing the file once more or creating a backup).
Retrying the close() after a failure return is the wrong thing to do, since this may cause a reused file descriptor from another thread to be closed. this can occur because the Linux kernel always releases the file descriptor early in the close operation, freeing it for reuse; the steps that may return an error, such as flushing data to the filesystem or device, occur only later in the close operation. so like... the fd is already gone, bestie, let her go ๐ญ
many other implementations similarly always close the file descriptor (except in the case of EBADF, meaning that the file descriptor was invalid) even if they subsequently report an error on return from close(). POSIX.1-2008 was silent on this point. ๐ค
a careful programmer who wants to know about I/O errors may precede close() with a call to fsync(2). do that. glow up your error handling โจ
the EINTR error is a somewhat special case. regarding the EINTR error, POSIX.1-2008 said:
Ifclose()is interrupted by a signal that is to be caught, it shall return-1witherrnoset toEINTRand the state offdis unspecified.
this permits the behavior that occurs on Linux and many other implementations, where, as with other errors that may be reported by close(), the file descriptor is guaranteed to be closed. however, it also permits another possibility: that the implementation returns an EINTR error and keeps the file descriptor open. (according to its documentation, HP-UX's close() does this.) the caller must then once more use close() to close the file descriptor, to avoid file descriptor leaks. this divergence in implementation behaviors provides a difficult hurdle for portable applications, since on many implementations close() must not be called again after an EINTR error, and on at least one, close() must be called again. absolutely cursed situation, no cap ๐ต
POSIX.1-2024 standardized the behavior of HP-UX, making Linux and many other implementations non-conforming. there are no plans to change the behavior on Linux. so... yeah. Linux said "we're staying like this" and honestly? respect ๐
๐ SEE ALSO ๐
close_range(2), fcntl(2), fsync(2), open(2), shutdown(2), unlink(2), fclose(3) โ go read them, they're the extended universe ๐ฆ
okay that's the whole page, bestie! this one came from Linux man-pages 6.20, dated 2026-02-08, and it's the close(2) page ๐ now go fsync() before you close, you absolute legend ๐ซถโจ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1992, Drew Eckhardt <drew@cs.colorado.edu>
- Copyright 2016, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms