HOME

TheInfoList




In
computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes and development of both computer hardware , hardware and software. It has sci ...

computing
, mmap(2) is a
POSIX The Portable Operating System Interface (POSIX) is a family of standard Standard may refer to: Flags * Colours, standards and guidons * Standard (flag), a type of flag used for personal identification Norm, convention or requirement * Sta ...
-compliant
Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser Multi-user software is computer software Software is a collection of Instruction (computer science), instructions and data (computing), data that tell a computer how to ...

Unix
system call In computing Computing is any goal-oriented activity requiring, benefiting from, or creating computing machinery. It includes the study and experimentation of algorithmic processes and development of both computer hardware , hardware and soft ...
that maps files or devices into memory. It is a method of
memory-mapped file A memory-mapped file is a segment of virtual memory In computing, virtual memory, or virtual storage is a Memory management (operating systems), memory management technique that provides an "idealized abstraction of the storage resources that ...
I/O. It implements
demand paging In computer A computer is a machine that can be programmed to carry out sequences of arithmetic or logical operations automatically. Modern computers can perform generic sets of operations known as Computer program, programs. These programs ...
because file contents are not read from disk directly and initially do not use physical RAM at all. The actual reads from disk are performed in a " lazy" manner, after a specific location is accessed. After the memory is no longer needed, it is important to munmap(2) the pointers to it. Protection information can be managed using mprotect(2), and special treatment can be enforced using madvise(2). In
Linux Linux ( or ) is a family of open-source Open source is source code that is made freely available for possible modification and redistribution. Products include permission to use the source code, design documents, or content of the product ...

Linux
,
macOS macOS (; previously Mac OS X and later OS X) is a Proprietary software, proprietary graphical user interface, graphical operating system developed and marketed by Apple Inc. since 2001. It is the primary operating system for ...
and the
BSD The Berkeley Software Distribution (BSD) is a discontinued based on , developed and distributed by the (CSRG) at the . The term "BSD" commonly refers to its descendants, including , , , and . BSD was initially called Berkeley Unix because it ...

BSD
s, mmap can create several types of mappings. Other operating systems may only support a subset of these; for example, shared mappings may not be practical in an operating system without a global VFS or I/O cache.


History

The original design of memory mapped files came from the
TOPS-20 The TOPS-20 operating system An operating system (OS) is system software System software is software designed to provide a platform for other software. Examples of system software include operating systems like macOS macOS (; previousl ...

TOPS-20
operating system. mmap and associated systems calls were designed as part of the
Berkeley Software Distribution The Berkeley Software Distribution (BSD) is a discontinued operating system An operating system (OS) is system software that manages computer hardware, computer software, software resources, and provides common daemon (computing), services f ...
(BSD) version of Unix. Their API was already described in the 4.2BSD System Manual, even though it was neither implemented in that release, nor in 4.3BSD.
Sun Microsystems Sun Microsystems, Inc. (Sun for short) was an American technology company that sold computer A computer is a machine that can be programmed to Execution (computing), carry out sequences of arithmetic or logical operations automatically. M ...
had implemented this very API, though, in their
SunOS SunOS is a Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser Multi-user software is computer software Software is a collection of Instruction (computer science), instructions and data (computing), data that tell ...
operating system. The BSD developers at
U.C. Berkeley
U.C. Berkeley
requested Sun to donate its implementation, but these talks never led to any transfer of code; 4.3BSD-Reno was shipped instead with an implementation based on the virtual memory system of Mach.


File-backed and anonymous

''File-backed mapping'' maps an area of the process's
virtual memory In computing, virtual memory, or virtual storage is a Memory management (operating systems), memory management technique that provides an "idealized abstraction of the storage resources that are actually available on a given machine" which "cre ...

virtual memory
to files; i.e. reading those areas of memory causes the file to be read. It is the default mapping type. ''Anonymous mapping'' maps an area of the process's virtual memory not backed by any file. The contents are initialized to zero. In this respect an anonymous mapping is similar to
malloc C dynamic memory allocation refers to performing manual memory management In computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical t ...
, and is used in some malloc(3) implementations for certain allocations. However, anonymous mappings are not part of the POSIX standard, though implemented by almost all operating systems by the MAP_ANONYMOUS and MAP_ANON flags.


Memory visibility

If the mapping is ''shared'' (the MAP_SHARED flag is set), then it is preserved across a fork(2) system call. This means that writes to a mapped area in one process are immediately visible in all related (parent, child or sibling) processes. If the mapping is ''shared'' and backed by a file (not MAP_ANONYMOUS) the underlying file medium is only guaranteed to be written after it is msync(2)'ed. If the mapping is ''private'' (the MAP_PRIVATE flag is set), the changes will neither be seen by other processes nor written to the file. A process reading from or writing to the underlying file will not always see the same data as a process that has mapped the file, since the segment of the file is copied into RAM and periodically flushed to disk. Synchronization can be forced with the msync system call. mmap(2)ing files can significantly reduce memory overhead for applications accessing the same file; they can share the memory area the file encompasses, instead of loading the file for each application that wants access to it. This means that mmap(2) is sometimes used for
Interprocess Communication In computer science Computer science deals with the theoretical foundations of information, algorithms and the architectures of its computation as well as practical techniques for their application. Computer science is the study of , ...
(IPC). On modern
operating system An operating system (OS) is system software System software is software designed to provide a platform for other software. Examples of system software include operating systems (OS) like macOS, Linux, Android (operating system), Android and Mi ...

operating system
s, mmap(2) is typically preferred to the
System V Unix System V (pronounced: "System Five") is one of the first commercial versions of the Unix Unix (; trademarked as UNIX) is a family of multitasking, multiuser Multi-user software is computer software Software is a collection of I ...
IPC
Shared Memory In computer science, shared memory is random-access memory, memory that may be simultaneously accessed by multiple programs with an intent to provide communication among them or avoid redundant copies. Shared memory is an efficient means of pass ...
facility. The main difference between System V shared memory (shmem) and memory mapped I/O (mmap) is that System V shared memory is persistent: unless explicitly removed by a process, it is kept in memory and remains available until the system is shut down. mmap'd memory is not persistent between application executions (unless it is backed by a file).


Example of usage under the C programming language

#include #include #include #include #include #include #include #include /* This example shows how an mmap of /dev/zero is equivalent to using anonymous memory (MAP_ANON) not connected to any file. N.B. MAP_ANONYMOUS or MAP_ANON are supported by most UNIX versions, removing the original purpose of /dev/zero. */ /* Does not work on OS X or macOS, where you can't mmap over /dev/zero */ int main(void) sample output:

PID 22475:      anonymous string 1, zero-backed string 1
PID 22476:      anonymous string 1, zero-backed string 1
PID 22475:      anonymous string 2, zero-backed string 2
PID 22476:      anonymous string 2, zero-backed string 2


See also

*
Virtual memory In computing, virtual memory, or virtual storage is a Memory management (operating systems), memory management technique that provides an "idealized abstraction of the storage resources that are actually available on a given machine" which "cre ...

Virtual memory
for when there is more address space than physical memory *
Paging In computer A computer is a machine that can be programmed to carry out sequences of arithmetic or logical operations automatically. Modern computers can perform generic sets of operations known as Computer program, programs. These prog ...

Paging
for the implementation of virtual memory *
Page cache In computing, a page cache, sometimes also called disk cache (disambiguation), disk cache, is a transparent cache (computing), cache for the page (computer memory), pages originating from a secondary storage device such as a hard disk drive (HDD) o ...
for a disk caching mechanism utilized by mmap *
Demand paging In computer A computer is a machine that can be programmed to Execution (computing), carry out sequences of arithmetic or logical operations automatically. Modern computers can perform generic sets of operations known as Computer program, pro ...
for a scheme implemented by mmap


References


Further reading


Description from POSIX standard
*Differences:
DragonFly BSD

FreeBSD

NetBSD

OpenBSD

illumos





HP-UX


*Windows
MapViewOfFile
win32 function is somewhat equivalent to mmap. *More example source code: *
SharedHashFile
An open source, shared memory hash table implemented using mmap(). {{Inter-process communication Inter-process communication C POSIX library