Memory Mappings 1043
individual pages. It was intended that remap_file_pages() would allow permis-
sions on individual pages within a VMA to be changed, but this facility has not
so far been implemented.
z The flags argument is currently unused.
As currently implemented, remap_file_pages() can be applied only to shared
(MAP_SHARED) mappings.
The remap_file_pages() system call is Linux-specific; it is not specified in SUSv3
and is not available on other UNIX implementations.
49.12 Summary
The mmap() system call creates a new memory mapping in the calling process’s vir-
tual address space. The munmap() system call performs the converse operation,
removing a mapping from a process’s address space.
A mapping may be of two types: file-based or anonymous. A file mapping maps
the contents of a file region into the process’s virtual address space. An anonymous
mapping (created by using the MAP_ANONYMOUS flag or by mapping /dev/zero) doesn’t
have a corresponding file region; the bytes of the mapping are initialized to 0.
Mappings can be either private (MAP_PRIVATE) or shared (MAP_SHARED). This distinction
determines the visibility of changes made to the shared memory, and, in the case of
file mappings, determines whether the kernel propagates changes to the contents
of the mapping to the underlying file. When a process maps a file with the
MAP_PRIVATE flag, any changes it makes to the contents of the mapping are not visible
to other processes and are not carried through to the mapped file. A MAP_SHARED file
mapping is the converse—changes to the mapping are visible to other processes
and are carried through to the mapped file.
Although the kernel automatically propagates changes to the contents of a
MAP_SHARED mapping to the underlying file, it doesn’t provide any guarantees about
when this is done. An application can use the msync() system call to explicitly con-
trol when the contents of a mapping are synchronized with the mapped file.
Memory mappings serve a variety of uses, including:
z allocating process-private memory (private anonymous mappings);
z initializing the contents of the text and initialized data segments of a process
(private file mappings);
z sharing memory between processes related via fork() (shared anonymous map-
pings); and
z performing memory-mapped I/O, optionally combined with memory sharing
between unrelated processes (shared file mappings).
Two signals may come into play when accessing the contents of a mapping. SIGSEGV
is generated if we attempt access in a manner that violates the protections on the
mapping (or if we access any currently unmapped address). SIGBUS is generated for
file-based mappings if we access a part of the mapping for which no corresponding
region exists in the file (i.e., the mapping is larger than the underlying file).