1MSYNC(2)                   Linux Programmer's Manual                  MSYNC(2)
2
3
4

NAME

6       msync - synchronize a file with a memory map
7

SYNOPSIS

9       #include <sys/mman.h>
10
11       int msync(void *addr, size_t length, int flags);
12

DESCRIPTION

14       msync()  flushes  changes  made  to the in-core copy of a file that was
15       mapped into memory using mmap(2) back to the filesystem.   Without  use
16       of  this  call, there is no guarantee that changes are written back be‐
17       fore munmap(2) is called.  To be more precise, the  part  of  the  file
18       that  corresponds to the memory area starting at addr and having length
19       length is updated.
20
21       The flags argument should specify exactly one of MS_ASYNC and  MS_SYNC,
22       and  may  additionally  include the MS_INVALIDATE bit.  These bits have
23       the following meanings:
24
25       MS_ASYNC
26              Specifies that an update be scheduled, but the call returns  im‐
27              mediately.
28
29       MS_SYNC
30              Requests an update and waits for it to complete.
31
32       MS_INVALIDATE
33              Asks to invalidate other mappings of the same file (so that they
34              can be updated with the fresh values just written).
35

RETURN VALUE

37       On success, zero is returned.  On error, -1 is returned, and  errno  is
38       set to indicate the error.
39

ERRORS

41       EBUSY  MS_INVALIDATE  was  specified in flags, and a memory lock exists
42              for the specified address range.
43
44       EINVAL addr is not a multiple  of  PAGESIZE;  or  any  bit  other  than
45              MS_ASYNC  |  MS_INVALIDATE  |  MS_SYNC  is set in flags; or both
46              MS_SYNC and MS_ASYNC are set in flags.
47
48       ENOMEM The indicated memory (or part of it) was not mapped.
49

CONFORMING TO

51       POSIX.1-2001, POSIX.1-2008.
52
53       This call was introduced in Linux 1.3.21, and then used EFAULT  instead
54       of  ENOMEM.   In  Linux  2.4.19,  this  was  changed to the POSIX value
55       ENOMEM.
56
57       On   POSIX   systems   on   which   msync()    is    available,    both
58       _POSIX_MAPPED_FILES   and   _POSIX_SYNCHRONIZED_IO   are   defined   in
59       <unistd.h> to a value greater than 0.  (See also sysconf(3).)
60

NOTES

62       According to POSIX, either MS_SYNC or MS_ASYNC  must  be  specified  in
63       flags,  and  indeed  failure  to  include one of these flags will cause
64       msync() to fail on some systems.  However,  Linux  permits  a  call  to
65       msync()  that specifies neither of these flags, with semantics that are
66       (currently) equivalent to specifying MS_ASYNC.   (Since  Linux  2.6.19,
67       MS_ASYNC  is  in  fact  a no-op, since the kernel properly tracks dirty
68       pages and flushes them to storage as necessary.)   Notwithstanding  the
69       Linux  behavior, portable, future-proof applications should ensure that
70       they specify either MS_SYNC or MS_ASYNC in flags.
71

SEE ALSO

73       mmap(2)
74
75       B.O. Gallmeister, POSIX.4, O'Reilly, pp. 128–129 and 389–391.
76

COLOPHON

78       This page is part of release 5.13 of the Linux  man-pages  project.   A
79       description  of  the project, information about reporting bugs, and the
80       latest    version    of    this    page,    can     be     found     at
81       https://www.kernel.org/doc/man-pages/.
82
83
84
85Linux                             2021-03-22                          MSYNC(2)
Impressum