MSYNC(2)

πŸ’Ύ MSYNC(2) β€” the manual page, bestie edition πŸ’Ύ

okay so this is msync, a system call from the System Calls Manual, and it's literally the "hey file system, catch up bestie" button. let's get into it 🫢


✨ NAME ✨

msync β€” synchronize a mapped region

no cap, that's the whole job description.


πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc)


🌸 SYNOPSIS 🌸

#include <sys/mman.h>

int
msync(void *addr, size_t len, int flags);

πŸ’… DESCRIPTION πŸ’…

The msync() system call writes any modified pages back to the file system and updates the file modification time. it's giving "flushed and timestamped, mwah" πŸ’–

now here's the tea about len:

  • if len is 0, all modified pages within the region containing addr will be flushed.
  • if len is non-zero, only those pages containing addr and len-1 succeeding locations will be examined.

The flags argument may be specified as follows:

  • MS_ASYNC β€” Return immediately (girl said "I'll deal with it later, bye" πŸ’¨)
  • MS_SYNC β€” Perform synchronous writes (she stays. she waits. she's committed πŸ’ͺ)
  • MS_INVALIDATE β€” Invalidate all cached data (yanking the cache out of existence, iconic behavior)

πŸŽ€ RETURN VALUES πŸŽ€

The msync() function returns the value 0 if successful. otherwise, the value -1 is returned and the global variable errno is set to indicate the error. so basically 0 = slay, -1 = check errno, bestie 😭


πŸ”₯ ERRORS πŸ”₯

The msync() system call will fail if:

  • [EBUSY] β€” Some or all of the pages in the specified region are locked and MS_INVALIDATE is specified. (you asked to nuke the cache but the pages are literally holding on. rude.)
  • [EINVAL] β€” The addr argument is not a multiple of the hardware page size. (alignment check said no πŸ’€)
  • [ENOMEM] β€” The addresses in the range starting at addr and continuing for len bytes are outside the range allowed for the address space of a process, or specify one or more pages that are not mapped. (that address is not in your house, ma'am πŸ™ˆ)
  • [EINVAL] β€” The flags argument was both MS_ASYNC and MS_INVALIDATE. Only one of these flags is allowed. (pick a lane, babe. you cannot be both.)
  • [EIO] β€” An error occurred while writing at least one of the pages in the specified region. (the write itself fumbled. not your fault. probably.)

πŸ¦‹ SEE ALSO πŸ¦‹

madvise(2), mincore(2), mlock(2), mprotect(2), munmap(2)

the whole mmap friend group, honestly. go read them too, iykyk ✨


πŸ‘‘ HISTORY πŸ‘‘

The msync() system call first appeared in 4.4BSD. a true vintage queen, older than basically everyone reading this 🌈


πŸ’€ BUGS πŸ’€

The msync() system call is usually not needed, since BSD implements a coherent file system buffer cache. so like, she's a little extra sometimes. πŸ™ˆ

However, it may be used to associate dirty VM pages with file system buffers and thus cause them to be flushed to physical media sooner rather than later. so when you do need her, she's the one making sure your stuff actually hits the disk before the lights go out. main character energy tbh.


and that's the page, bestie!! πŸ’– this version came from the Debian distro's man pages, dated March 18, 2012, and it's page MSYNC(2) β€” go forth and flush responsibly 🫢✨

πŸ“¦ where this page comes from