In
computing
Computing is any goal-oriented activity requiring, benefiting from, or creating computer, computing machinery. It includes the study and experimentation of algorithmic processes, and the development of both computer hardware, hardware and softw ...
,
mmap(2)
is a
POSIX
The Portable Operating System Interface (POSIX; ) is a family of standards specified by the IEEE Computer Society for maintaining compatibility between operating systems. POSIX defines application programming interfaces (APIs), along with comm ...
-compliant
Unix
Unix (, ; trademarked as UNIX) is a family of multitasking, multi-user computer operating systems that derive from the original AT&T Unix, whose development started in 1969 at the Bell Labs research center by Ken Thompson, Dennis Ritchie, a ...
system call
In computing, a system call (syscall) is the programmatic way in which a computer program requests a service from the operating system on which it is executed. This may include hardware-related services (for example, accessing a hard disk drive ...
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 that has been assigned a direct byte-for-byte correlation with some portion of a file or file-like resource. This resource is typically a file that is physically present on disk, but can also b ...
I/O. It implements
demand paging
In computer operating systems, demand paging (as opposed to anticipatory paging) is a method of virtual memory management. In a system that uses demand paging, the operating system copies a disk page into physical memory only when an attempt is m ...
because file contents are not immediately read from disk and initially use no physical RAM at all. The actual reads from disk are performed after a specific location is accessed, in a
lazy manner. After the mapping is no longer needed, the pointers must be unmapped with
munmap(2)
.
Protection
Protection is any measure taken to guard something against damage caused by outside forces. Protection can be provided to physical objects, including organisms, to systems, and to intangible things like civil and political rights. Although ...
information—for example, marking mapped regions as executable—can be managed using
mprotect(2)
, and special treatment can be enforced using
madvise(2)
.
In
Linux
Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
,
macOS
macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
and the
BSD
The Berkeley Software Distribution (BSD), also known as Berkeley Unix or BSD Unix, is a discontinued Unix operating system developed and distributed by the Computer Systems Research Group (CSRG) at the University of California, Berkeley, beginni ...
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 by Digital Equipment Corporation (DEC) is a proprietary OS used on some of DEC's 36-bit mainframe computers. The Hardware Reference Manual was described as for "DECsystem-10/DECSYSTEM-20 Processor" (meaning the DEC PDP ...
operating system.
mmap
and associated systems calls were designed as part of the
Berkeley Software Distribution
The Berkeley Software Distribution (BSD), also known as Berkeley Unix or BSD Unix, is a discontinued Unix operating system developed and distributed by the Computer Systems Research Group (CSRG) at the University of California, Berkeley, beginn ...
(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., often known as Sun for short, was an American technology company that existed from 1982 to 2010 which developed and sold computers, computer components, software, and information technology services. Sun contributed sig ...
had implemented this API, though, in the 4.0 release of their
SunOS
SunOS is a Unix-branded operating system developed by Sun Microsystems for their workstation and server computer systems from 1982 until the mid-1990s. The ''SunOS'' name is usually only used to refer to versions 1.0 to 4.1.4, which were based ...
operating system. The BSD developers at
University of California, Berkeley
The University of California, Berkeley (UC Berkeley, Berkeley, Cal, or California), is a Public university, public Land-grant university, land-grant research university in Berkeley, California, United States. Founded in 1868 and named after t ...
unsuccessfully requested Sun to donate its implementation; 4.3BSD-Reno was instead shipped with an implementation based on the virtual memory system of
Mach
The Mach number (M or Ma), often only Mach, (; ) is a dimensionless quantity in fluid dynamics representing the ratio of flow velocity past a Boundary (thermodynamic), boundary to the local speed of sound.
It is named after the Austrian physi ...
.
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 technique that provides an "idealized abstraction of the storage resources that are actually available on a given machine" which "creates the illusion to users of a ver ...
to files; that is, 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, made available via the
MAP_ANONYMOUS
/
MAP_ANON
flags. 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 for dynamic memory allocation in the C programming language via a group of functions in the C standard library, namely , , , and .
The C++ programming language includ ...
, and is used in some malloc implementations for certain allocations, particularly large ones.
Memory visibility
If the mapping is ''shared'' (the
MAP_SHARED
flag is set), then it is preserved when a process is forked (using a
fork(2)
system call). Therefore, 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 passed to the
msync(2)
system call. In contrast, 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 different process that has mapped the file, since segments of the file are copied into RAM and only periodically flushed to disk. Synchronization can be forced with a call to
msync(2)
.
Using mmap on 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, interprocess communication (IPC) is the sharing of data between running processes in a computer system. Mechanisms for IPC may be provided by an operating system. Applications which use IPC are often categorized as clients ...
(IPC). On modern
operating system
An operating system (OS) is system software that manages computer hardware and software resources, and provides common daemon (computing), services for computer programs.
Time-sharing operating systems scheduler (computing), schedule tasks for ...
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 operating system. It was originally developed by AT&T and first released in 1983. Four major versions of System V were released, numbered 1, 2, 3, an ...
IPC
Shared Memory 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
Usage in database implementations
The mmap system call has been used in various database implementations as an alternative for implementing a buffer pool, although this created a different set of problems that could realistically only be fixed using a buffer pool.
See also
*
Virtual memory
In computing, virtual memory, or virtual storage, is a memory management technique that provides an "idealized abstraction of the storage resources that are actually available on a given machine" which "creates the illusion to users of a ver ...
for when there is more address space than physical memory
*
Paging
In computer operating systems, memory paging is a memory management scheme that allows the physical Computer memory, memory used by a program to be non-contiguous. This also helps avoid the problem of memory fragmentation and requiring compact ...
for the implementation of virtual memory
*
Page cache
In computing, a page cache, sometimes also called disk cache, is a transparent cache for the pages originating from a secondary storage device such as a hard disk drive (HDD) or a solid-state drive (SSD). The operating system keeps a page ca ...
for a disk caching mechanism utilized by mmap
*
Demand paging
In computer operating systems, demand paging (as opposed to anticipatory paging) is a method of virtual memory management. In a system that uses demand paging, the operating system copies a disk page into physical memory only when an attempt is m ...
for a scheme implemented by mmap
References
Further reading
Description from POSIX standard*Differences:
DragonFly BSDFreeBSDNetBSDOpenBSDillumosHP-UX*Windows
MapViewOfFilewin32 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