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_NOREUSE had the same semantics as POSIX_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_NOREUSE signals 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 โœจ
  • POSIX_FADV_WILLNEED โ€” the specified data will be accessed in the near future. ๐Ÿ‘€
    • POSIX_FADV_WILLNEED initiates 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_DONTNEED attempts 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 offset and size must 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) or fdatasync(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 โ€” The fd argument was not a valid file descriptor. bestie that's not a file ๐Ÿ˜ญ
  • EINVAL โ€” An invalid value was specified for advice. you made that up.
  • ESPIPE โ€” The specified file descriptor refers to a pipe or FIFO. (ESPIPE is the error specified by POSIX, but before Linux 2.6.16, Linux returned EINVAL in 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_SYSCALLS configuration option.
  • The type of the size argument was changed from size_t to off_t in POSIX.1-2001 TC1.

๐ŸŽ€ NOTES ๐ŸŽ€

  • The contents of the kernel buffer cache can be cleared via the /proc/sys/vm/drop_caches interface described in proc(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 applying mincore(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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms