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_INVALIDATE was specified in flags, and a memory lock exists for the specified address range. locked down, can't touch. ๐Ÿ”’
  • EINVAL โ€” addr is not a multiple of PAGESIZE; or any bit other than MS_ASYNC | MS_INVALIDATE | MS_SYNC is set in flags; or both MS_SYNC and MS_ASYNC are set in flags. 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 before munmap(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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms