msync() — Synchronize memory with physical storage

Standards

Standards / Extensions C or C++ Dependencies

XPG4.2
Single UNIX Specification, Version 3

both  

Format

#define _XOPEN_SOURCE_EXTENDED 1
#include <sys/mman.h>

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

General description

The msync() function writes all modified copies of pages over the range [addr, addr + len) to the underlying hardware, or invalidates any copies so that further references to the pages will be obtained by the system from their permanent storage locations.

The flags argument is:
MS_ASYNC
Perform asynchronous writes
MS_INVALIDATE
Invalidate mappings
MS_SYNC
Perform synchronous writes
The function synchronizes the file contents to match the current contents to the memory region.
  • All write references to the memory region made before the call are visible by subsequent read operations on the file.
  • It is unspecified whether writes to the same portion of the file before the call are visible by read references to the memory region.
  • It is unspecified whether unmodified pages in the specified range are also written to the underlying hardware.

If flags is MS_ASYNC, the function may return immediately once all write operations are schedule; if flags is MS_SYNC, the function does not return until all write operations are completed.

MS_INVALIDATE synchronizes the contents of the memory region to match the current file contents.
  • All writes to the mapped portion of the file made before the call are visible by subsequent read references to the mapped memory region.
  • It is unspecified whether write references before the call, by any process, to memory regions mapped to the same portion of the file using MAP_SHARED, are visible by read references to the region.

If msync() causes any write to the file, then the file's st_ctime and st_mtime fields are marked for update.

Returned value

If successful, msync() returns 0.

If unsuccessful, msync() returns -1 and sets errno to one of the following values:
Error Code
Description
EINVAL
The addr argument is not a multiple of the page size as returned by sysnonf.
EIO
An I/O error occurred while reading from or writing to the file system.
ENOMEM
Some or all the addresses in the range [addr, addr + range [addr, addr + len) are len) are invalid for the address space of the process or pages not mapped are specified.

Related information