Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

msync - synchronize memory

&pagelevel(4)&pagelevel

Syntax

#include <sys/mman.h>

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

Description

The msync() function writes all modified copies of pages in the [addr, addr + len) area back to the appropriate storage media or makes copies in the memory invalid so that later accesses to these pages will access the storage medium.
The storage medium for a modified mapping of type MAP_SHARED is the file to which the page is mapped; the storage medium for a modified mapping of type MAP_PRIVATE is its paging area.

flags must have one of the following values: 

 

MS_ASYNC

Perform asynchronous write accesses

 

MS_SYNC

Perform synchronous write accesses

 

MS_INVALIDATE

Mark mappings as invalid

 

If MS_ASYNC or MS_SYNC are set, msync() synchronizes the file contents with the current contents of the allocated storage area: All write accesses to the storage area that have taken place before the msync() call are visible during read accesses to the file after msync(). Before msync() is called, however, it is undefined whether write accesses to the corresponding file section will be visible during subsequent read accesses.

If MS_ASYNC is set, msync() returns as soon as all write operations have been initiated; if MS_SYNC is set, msync() does not return until all write operations are completed.

If MS_INVALIDATE is set, msync() synchronizes the memory area with the current contents of the assigned file section. Afterwards, all copies of data that are located in a cache memory are marked as invalid. Later references to these pages are handled by the system via the underlying storage medium. All write accesses to the mapped file section that took place before the msync() call are visible during subsequent read accesses to the allocated memory area. Before msync() is called, however, it is undefined whether write accesses to the corresponding file section will be visible during subsequent read accesses.

Return val.

0

if successful.


-1

if an error occurs. errno is set to indicate the error.

Errors

Under the conditions described below, the msync() function will fail and set errno to the following values:

 

EINVAL

addr is not a multiple of the page size defined by sysconf().

 

ENOMEM

Addresses in the [addr, addr + len) area are invalid for the address area of the process, or one or more pages have been specified which are not mapped.

 

EIO

An I/O error occurred during read or write access to the file.

Notes

msync() should be used if it is required that a memory object be in a known state, e.g. in transaction processing.

Memory pages can also be written to disk in the course of normal system operations.
Therefore it cannot be guaranteed that memory pages are only written to disk when msync() is called.

See also

mmap(), sysconf(), sys/mman.h