msync(2)
๐พ msync(2) โ the "flush ur mmap edits to disk bestie" page
okay so this is the manual page for msync, a system call (that's the (2) in msync(2), iykyk), living in the System Calls Manual. she's the one who makes sure the stuff you scribbled into a memory-mapped file actually makes it to the filesystem. real ones know her.
โจ LIBRARY โจ
Standard C library (libc, -lc). yeah bestie, she's in libc, no special hunting required. ๐
๐ธ SYNOPSIS ๐ธ
#include <sys/mman.h>
int msync(size_t length;
void addr[length], size_t length, int flags);
๐ฆ DESCRIPTION ๐ฆ
okay so picture this: you mapped a file into memory with mmap(2) and you've been โ๏ธ editing it, living your best life. msync() flushes those changes made to the in-core copy of that file back to the filesystem. Without use of this call, there is no guarantee that changes are written back before munmap(2) is called. ๐ซ so likeโฆ do it, bestie.
To be more precise: the part of the file that corresponds to the memory area starting at addr and having length length is the stuff that gets updated. ๐ฏ
The flags argument should specify exactly one of MS_ASYNC and MS_SYNC, and may additionally include the MS_INVALIDATE bit. (so like, one of them, plus optionally the third one. not both of the first two. we'll get to why. ๐ญ) These bits have the following meanings:
MS_ASYNCโ specifies that an update be scheduled, but the call returns immediately. she's giving "I'll deal with it later" energy ๐๏ธโจMS_SYNCโ requests an update and waits for it to complete. main character behavior, she stays until the job is done ๐MS_INVALIDATEโ asks to invalidate other mappings of the same file, so that they can be updated with the fresh values just written. out with the old cache, in with the new ๐ซ
๐ RETURN VALUE ๐
On success, zero is returned. (slay ๐) On error, -1 is returned, and errno is set to indicate the error. (not slay ๐ญ)
๐ฅ ERRORS ๐ฅ
EBUSYโMS_INVALIDATEwas specified inflags, and a memory lock exists for the specified address range. locked down, can't touch. ๐EINVALโaddris not a multiple ofPAGESIZE; or any bit other thanMS_ASYNC | MS_INVALIDATE | MS_SYNCis set inflags; or bothMS_SYNCandMS_ASYNCare set inflags. pick one, bestie, you can't have both ๐ซถENOMEMโ the indicated memory (or part of it) was not mapped. girl what are you even syncing, there's nothing there ๐
๐ VERSIONS ๐
According to POSIX, either MS_SYNC or MS_ASYNC must be specified in flags, and indeed failure to include one of these flags will cause msync() to fail on some systems. โ๐ค
However, Linux permits a call to msync() that specifies neither of these flags, with semantics that are (currently) equivalent to specifying MS_ASYNC. (Since Linux 2.6.19, MS_ASYNC is in fact a no-op, since the kernel properly tracks dirty pages and flushes them to storage as necessary. the kernel is doing it for you, she's so competent ๐ง)
Notwithstanding the Linux behavior, portable, future-proof applications should ensure that they specify either MS_SYNC or MS_ASYNC in flags. like, Linux will let you be messy but do NOT be messy in prod ๐จ
โญ STANDARDS โญ
POSIX.1-2024. certified iconic. ๐
๐ HISTORY ๐
SUSv1, POSIX.1-1996. she's a legend, she's been around.
This call was introduced in Linux 1.3.21, and then used EFAULT instead of ENOMEM. In Linux 2.4.19, this was changed to the POSIX value ENOMEM. glow-up arc complete โจ
On POSIX systems on which msync() is available, both _POSIX_MAPPED_FILES and _POSIX_SYNCHRONIZED_IO are defined in <unistd.h> to a value greater than 0. (See also sysconf(3).) go check them if you don't believe me ๐ซก
๐ SEE ALSO ๐
mmap(2)- B.O. Gallmeister, POSIX.4, O'Reilly, pp. 128โ129 and 389โ391.
โ ๏ธ bestie reminder:msync()failing quietly means your changes might not hit the disk beforemunmap(2). check your return values, fr fr. ๐ซถ
๐ that's the page, she's written by the Linux man-pages 6.20 girlies and dated 2026-02-11. msync(2), periodt. ๐
๐ธ
๐ฆ 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 1996, Andries E. Brouwer <aeb@cwi.nl>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms