posix_fadvise(2)
๐ posix_fadvise(2) โ the predeclare-an-access-pattern-for-file-data girlie ๐
okay so bestie, welcome to posix_fadvise(2), and lemme tell you, this is the lowkey most underrated syscall in the whole building. she's a hint. she's a vibe. she is not, and I cannot stress this enough, a promise. let's get into it ๐
โจ NAME โจ
posix_fadvise โ predeclare an access pattern for file data ๐ (yes it's literally "hey kernel, imma read this like this, just so you know")
๐ LIBRARY ๐
Standard C library (libc, -lc) โ the usual squad, no extras needed ๐ซถ
๐ธ SYNOPSIS ๐ธ
#include <fcntl.h>
int posix_fadvise(int fd, off_t offset, off_t size, int advice);
๐ง Feature Test Macro Requirements for glibc ๐ง
(peep feature_test_macros(7) if you wanna know more)
posix_fadvise()โ needs_POSIX_C_SOURCE >= 200112L
๐ซ DESCRIPTION ๐ซ
So like. Programs can use posix_fadvise() to announce an intention about how they're gonna access file data in the future, which lets the kernel go ahead and do the appropriate optimizations. it's giving "heads up bestie" energy, and the kernel is obsessed ๐ฅฐ
The advice applies to a region within the file referred to by fd โ and that region does not have to exist yet, no cap. It starts at offset and extends for size bytes, orrrr it just runs until the end of the file if size is 0.
โ ๏ธ and listen: the advice is not binding. It merely constitutes an expectation on behalf of the application. The kernel is allowed to completely ignore you. It's a suggestion, not a subpoena ๐
The permissible values for advice include:
POSIX_FADV_NORMALโ the application has no advice to give about its access pattern for the specified data. And if nobody gives any advice for an open file? This is the default assumption, periodt.POSIX_FADV_SEQUENTIALโ the application expects to access the specified data sequentially, i.e. lower offsets get read before higher ones. front to back, like a story ๐POSIX_FADV_RANDOMโ the specified data will be accessed in random order. chaos! go off! ๐ฒPOSIX_FADV_NOREUSEโ the specified data will be accessed only once. one and done, iconic behaviour.- Before Linux 2.6.18,
POSIX_FADV_NOREUSEhad the same semantics asPOSIX_FADV_WILLNEED. That was probably a bug ngl ๐ญ - From Linux 2.6.18 until Linux 6.2, this flag was a no-op (literally did nothing, embarrassing).
- Since Linux 6.3,
POSIX_FADV_NOREUSEsignals that the kernel page replacement algorithm can ignore access to mapped page cache marked by this flag. Useful, for example, while streaming large files. glow up complete โจ
- Before Linux 2.6.18,
POSIX_FADV_WILLNEEDโ the specified data will be accessed in the near future. ๐POSIX_FADV_WILLNEEDinitiates a nonblocking read of the specified region into the page cache. The amount of data read may be decreased by the kernel depending on virtual memory load. (A few megabytes will usually be fully satisfied, and more is rarely useful. so like, don't be greedy ๐)
POSIX_FADV_DONTNEEDโ the specified data will not be accessed in the near future. bye bitch ๐POSIX_FADV_DONTNEEDattempts to free cached pages associated with the specified region. Useful, for example, while streaming large files. A program may periodically request that the kernel free cached data that's already been used, so that more useful cached pages aren't discarded instead.- Requests to discard partial pages are ignored. It is preferable to preserve needed data than discard unneeded data. If the application requires that data be considered for discarding, then
offsetandsizemust be page-aligned. - The implementation may attempt to write back dirty pages in the specified region, but this is not guaranteed. Any unwritten dirty pages will not be freed. If the application wishes to ensure that dirty pages will be released, it should call
fsync(2)orfdatasync(2)first.
๐ฅ RETURN VALUE ๐ฅ
On success, zero is returned. On error, an error number is returned. (not -1, not errno โ the number itself, she's different like that)
๐ ERRORS ๐
EBADFโ Thefdargument was not a valid file descriptor. bestie that's not a file ๐ญEINVALโ An invalid value was specified foradvice. you made that up.ESPIPEโ The specified file descriptor refers to a pipe or FIFO. (ESPIPEis the error specified by POSIX, but before Linux 2.6.16, Linux returnedEINVALin this case. old Linux was just built different ๐)
๐ VERSIONS ๐
Under Linux, POSIX_FADV_NORMAL sets the readahead window to the default size for the backing device; POSIX_FADV_SEQUENTIAL doubles this size; and POSIX_FADV_RANDOM disables file readahead entirely. POSIX_FADV_NOREUSE does not modify the readahead window size.
โ ๏ธ heads up: these changes affect the entire file, not just the specified region. (But other open file handles to the same file are unaffected, so at least there's that ๐)
๐ ๏ธ C library/kernel differences ๐ ๏ธ
The name of the wrapper function in the C library is posix_fadvise(). The underlying system call is called fadvise64() (or, on some architectures, fadvise64_64()). The difference between the two? The former system call assumes the type of the size argument is size_t, while the latter expects loff_t there. tiny little type drama, we love her
๐งฌ Architecture-specific variants ๐งฌ
Some architectures require 64-bit arguments to be aligned in a suitable pair of registers (go peep syscall(2) for further detail). On such architectures, the call signature of posix_fadvise() shown in the SYNOPSIS would force a register to be wasted as padding between the fd and offset arguments. so rude. Therefore, these architectures define a version of the system call that orders the arguments suitably, but is otherwise exactly the same as posix_fadvise().
For example, since Linux 2.6.14, ARM has the following system call:
long arm_fadvise64_64(int fd, int advice,
loff_t offset, loff_t size);
These architecture-specific details are generally hidden from applications by the glibc posix_fadvise() wrapper function, which invokes the appropriate architecture-specific system call. so you don't even gotta think about it, she handles it ๐
๐ STANDARDS ๐
POSIX.1-2024.
๐ HISTORY ๐
POSIX.1-2001.- Kernel support first appeared in Linux 2.5.60; the underlying system call is called
fadvise64(). - Library support has been provided since glibc 2.2, via the wrapper function
posix_fadvise(). - Since Linux 3.18, support for the underlying system call is optional, depending on the setting of the
CONFIG_ADVISE_SYSCALLSconfiguration option. - The type of the
sizeargument was changed fromsize_ttooff_tinPOSIX.1-2001 TC1.
๐ NOTES ๐
- The contents of the kernel buffer cache can be cleared via the
/proc/sys/vm/drop_cachesinterface described inproc(5). (nuclear option, use responsibly โจ) - One can obtain a snapshot of which pages of a file are resident in the buffer cache by opening a file, mapping it with
mmap(2), and then applyingmincore(2)to the mapping. cute lil detective work ๐
๐ BUGS ๐
Before Linux 2.6.6, if size was specified as 0, then this was interpreted literally as "zero bytes", rather than as meaning "all bytes through to the end of the file". yeah she just did not get the assignment ๐ fixed since, we move on
๐ฆ SEE ALSO ๐ฆ
fincore(1), mincore(2), readahead(2), sync_file_range(2), posix_fallocate(3), posix_madvise(3)
and that's the whole page bestie, you ate it, no crumbs ๐ this was the posix_fadvise(2) man page from Linux man-pages 6.20, dated 2026-02-08 โ slay, go predeclare ur access patterns responsibly, mwah ๐ซถ๐
โจ
๐ฆ 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 2003, Abhijit Menon-Sen <ams@wiw.org>
- Copyright 2010-2017, 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