1msync(2)                      System Calls Manual                     msync(2)
2
3
4

NAME

6       msync - synchronize a file with a memory map
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <sys/mman.h>
13
14       int msync(void addr[.length], size_t length, int flags);
15

DESCRIPTION

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

RETURN VALUE

40       On success, zero is returned.  On error, -1 is returned, and  errno  is
41       set to indicate the error.
42

ERRORS

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

VERSIONS

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

STANDARDS

65       POSIX.1-2008.
66

HISTORY

68       POSIX.1-2001.
69
70       This call was introduced in Linux 1.3.21, and then used EFAULT  instead
71       of  ENOMEM.   In  Linux  2.4.19,  this  was  changed to the POSIX value
72       ENOMEM.
73
74       On   POSIX   systems   on   which   msync()    is    available,    both
75       _POSIX_MAPPED_FILES   and   _POSIX_SYNCHRONIZED_IO   are   defined   in
76       <unistd.h> to a value greater than 0.  (See also sysconf(3).)
77

SEE ALSO

79       mmap(2)
80
81       B.O. Gallmeister, POSIX.4, O'Reilly, pp. 128–129 and 389–391.
82
83
84
85Linux man-pages 6.05              2023-03-30                          msync(2)
Impressum