POSIX and msync shared memory

I want to use POSIX Shared Memory and my question is about usage msync()

and munmap()

after call mmap(MAP_SHARED)

.

  • Changes made to the shared memory segment of one process are visible in the shared memory segment of another process without being used msync()

    ?
  • Are the changes in the shared memory segment only changed in the file after msync()

    ?
  • Are changes saved when the shared memory segment is not displayed without prior use msync()

    ?

My impression msync()

is that it is only useful to apply the changes to the base file and not to shared memory.

+3


source to share


1 answer


POSIX

See msync(2)

:

  • An implementation might decide that the changes are not visible in other processes and in the base file until msync()

    called with MS_ASYNC

    or MS_SYNC

    .
  • The implementation may decide that the changes will be removed if msync()

    not called before close()

    / munmap()

    .

This allows older implementations to use separate caches for memory mappings (aka page cache) and file I / O (aka buffer cache).

Modern implementations

Modern implementations (like modern Linux) usually implement a "unified virtual memory model" that uses the same cache for memory mappings and I / O. This is not required by POSIX or SUSv3.



In such implementations:

  • The changes are visible in other processes and the base file.
  • Changes are not discarded unless msync()

    called before close()

    / munmap()

    .
  • MS_ASYNC

    is a non-op.
  • MS_SYNC

    is similar fsync()

    .

In Linux msync()

, this is another interface for fsync()

and nothing more. See the msync(2)

man page and this thread .

Link

See "Linux Programming Interface", Section "49.4.4. Memory Protection and File Access Mode Interaction".

+2


source







All Articles