Mmap
Encyclopedia
In computing
,
-compliant Unix
system call
that maps files or devices into memory. It is a method of memory-mapped file
I/O. It naturally implements demand paging
, because initially file contents are not entirely read from disk and do not use physical RAM at all. The actual reads from disk are performed in "lazy" manner, after a specific location is accessed.
In Linux
, Mac OS X
and the BSDs,
Anonymous mappings are mappings of that area of the process's virtual memory
backed by the swap space instead of by a file in the file system name space. In this respect an anonymous mapping is similar to
File-backed mappings are mappings of virtual memory to files. Access to those areas of memory causes the file to be read. If the mapping is shared, writes to that area in one process will affect other processes with that area mapped and the underlying file; otherwise, if the mapping is private, the changes will not 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
Memory shared by
s
facility.
The main difference between System V shared memory (shmem) and memory mapped I/O (mmap) is that SystemV 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).
Computing
Computing is usually defined as the activity of using and improving computer hardware and software. It is the computer-specific part of information technology...
,
mmap
is a POSIXPOSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...
-compliant Unix
Unix
Unix is a multitasking, multi-user computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs, including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna...
system call
System call
In computing, a system call is how a program requests a service from an operating system's kernel. This may include hardware related services , creating and executing new processes, and communicating with integral kernel services...
that maps files or devices into memory. It is a method of memory-mapped file
Memory-mapped file
A memory-mapped file is a segment of virtual memory which 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 be a device, shared memory object, or other resource...
I/O. It naturally implements demand paging
Demand paging
In computer operating systems, demand paging is an application of virtual memory. In a system that uses demand paging, the operating system copies a disk page into physical memory only if an attempt is made to access it...
, because initially file contents are not entirely read from disk and do not use physical RAM at all. The actual reads from disk are performed in "lazy" manner, after a specific location is accessed.
In Linux
Linux
Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. The defining component of any Linux system is the Linux kernel, an operating system kernel first released October 5, 1991 by Linus Torvalds...
, Mac OS X
Mac OS X
Mac OS X is a series of Unix-based operating systems and graphical user interfaces developed, marketed, and sold by Apple Inc. Since 2002, has been included with all new Macintosh computer systems...
and the BSDs,
mmap
can create several types of mappings.Anonymous mappings are mappings of that area of the process's virtual memory
Virtual memory
In computing, virtual memory is a memory management technique developed for multitasking kernels. This technique virtualizes a computer architecture's various forms of computer data storage , allowing a program to be designed as though there is only one kind of memory, "virtual" memory, which...
backed by the swap space instead of by a file in the file system name space. In this respect an anonymous mapping is similar to
mallocMallocC dynamic memory allocation refers to performing dynamic memory allocation in the C via a group of functions in the C standard library, namely malloc, realloc, calloc and free....
, and is used in some malloc
implementations for certain allocations. However, anonymous mappings are not part of the POSIX standard, though implemented by almost all systems.File-backed mappings are mappings of virtual memory to files. Access to those areas of memory causes the file to be read. If the mapping is shared, writes to that area in one process will affect other processes with that area mapped and the underlying file; otherwise, if the mapping is private, the changes will not 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
ing files can significantly reduce memory overhead for applications accessing the same file. If the file is mmap
ed, the applications can then share the memory area that the file encompasses, instead of loading the file for each application that wants access to it.Memory shared by
mmap
is kept visible across a fork.mmap
is sometimes used for Interprocess Communication (IPC). On modern operating systemOperating system
An operating system is a set of programs that manage computer hardware resources and provide common services for application software. The operating system is the most important type of system software in a computer system...
s
mmap
is typically preferred to the System V IPC Shared MemoryShared memory
In computing, shared memory is memory that may be simultaneously accessed by multiple programs with an intent to provide communication among them or avoid redundant copies. Depending on context, programs may run on a single processor or on multiple separate processors...
facility.
The main difference between System V shared memory (shmem) and memory mapped I/O (mmap) is that SystemV 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).
See also
- Virtual memoryVirtual memoryIn computing, virtual memory is a memory management technique developed for multitasking kernels. This technique virtualizes a computer architecture's various forms of computer data storage , allowing a program to be designed as though there is only one kind of memory, "virtual" memory, which...
for a general context of possessing more addresses than physical memory- SwappingSwappingSwapping can mean:* In computer systems, an older form of memory management, similar to Paging* Swapping * Hot swapping* Book swapping* Wife swapping* Cumswapping...
or PagingPagingIn computer operating systems, paging is one of the memory-management schemes by which a computer can store and retrieve data from secondary storage for use in main memory. In the paging memory-management scheme, the operating system retrieves data from secondary storage in same-size blocks called...
for implementation of virtual memory used in contemporary systems- Page cachePage cacheIn computing, page cache, sometimes ambiguously called disk cache, is a "transparent" buffer of disk-backed pages kept in main memory by the operating system for quicker access. Page cache is typically implemented in kernels with the paging memory management, and is completely transparent to...
, a disk caching mechanism utilized by mmap - Demand pagingDemand pagingIn computer operating systems, demand paging is an application of virtual memory. In a system that uses demand paging, the operating system copies a disk page into physical memory only if an attempt is made to access it...
a scheme implemented by mmap
- Page cache
- Swapping