Mmap: Difference between revisions
No edit summary |
Add hatnote |
||
(16 intermediate revisions by 13 users not shown) | |||
Line 1: | Line 1: | ||
{{Short description|Memory map - POSIX-compliant system call}} |
|||
{{lowercase|title=mmap}} |
{{lowercase|title=mmap}} |
||
{{Distinguish|nmap}} |
|||
In [[computing]], '''<code>mmap(2)</code>''' is a [[POSIX]]-compliant [[Unix]] [[system call]] that maps files or devices into memory. It is a method of [[memory-mapped file]] I/O. It implements [[demand paging]] |
In [[computing]], '''<code>mmap(2)</code>''' is a [[POSIX]]-compliant [[Unix]] [[system call]] that maps files or devices into memory. It is a method of [[memory-mapped file]] I/O. It implements [[demand paging]] 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 evaluation|lazy]] manner. After the mapping is no longer needed, the pointers must be unmapped with <code>munmap(2)</code>. [[Memory protection|Protection]] information—for example, marking mapped regions as executable—can be managed using <code>mprotect(2)</code>, and special treatment can be enforced using <code>madvise(2)</code>. |
||
In [[Linux]], [[macOS]] and the [[BSD]]s, <code>mmap</code> can create several types of mappings. Other operating systems may only support a subset of these |
In [[Linux]], [[macOS]] and the [[BSD]]s, <code>mmap</code> 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 [[Virtual file system|VFS]] or I/O cache. |
||
==History== |
==History== |
||
The original design of memory |
The original design of memory-mapped files came from the [[TOPS-20]] operating system. <code>mmap</code> and associated systems calls were designed as part of the [[Berkeley Software Distribution]] (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.<ref>{{cite report |author1=William Joy |author-link=Bill Joy |author2=Eric Cooper |author3=Robert Fabry |author3-link=Bob Fabry |author4=Samuel Leffler |author4-link=Samuel Leffler |author5=Kirk McKusick |author5-link=Marshall Kirk McKusick |author6=David Mosher |title=4.2BSD System Manual |url=http://www.cilinder.be/docs/bsd/4.2BSD_Unix_system_manual.pdf |year=1983 |publisher=[[Computer Systems Research Group]], [[University of California, Berkeley]]}}</ref> [[Sun Microsystems]] had implemented this very API, though, in their [[SunOS]] operating system. The BSD developers at [[University of California, Berkeley]] unsuccessfully requested Sun to donate its implementation; 4.3BSD-Reno was instead shipped with an implementation based on the virtual memory system of [[Mach (kernel)|Mach]].<ref name="opensources">{{cite encyclopedia |title=Twenty Years of Berkeley Unix: From AT&T-Owned to Freely Redistributable |first=Marshall Kirk |last=McKusick |authorlink=Marshall Kirk McKusick |encyclopedia=Open Sources: Voices from the Open Source Revolution |year=1999 |publisher=O'Reilly}}</ref> |
||
== File-backed and anonymous == |
== File-backed and anonymous == |
||
''File-backed mapping'' maps an area of the process's [[virtual memory]] to files; |
''File-backed mapping'' maps an area of the process's [[virtual memory]] 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. The contents are initialized to zero.<ref>{{cite web |url=https://www.kernel.org/doc/man-pages/online/pages/man2/mmap.2.html |title=mmap(2) - Linux manual page}}</ref> In this respect an anonymous mapping is similar to |
''Anonymous mapping'' maps an area of the process's virtual memory not backed by any file. The contents are initialized to zero.<ref>{{cite web |url=https://www.kernel.org/doc/man-pages/online/pages/man2/mmap.2.html |title=mmap(2) - Linux manual page}}</ref> In this respect an anonymous mapping is similar to [[C dynamic memory allocation|malloc]], and is used in some malloc implementations for certain allocations, particularly large ones. Anonymous mappings are not part of the POSIX standard, but are implemented in almost all operating systems by the <code>MAP_ANONYMOUS</code> and <code>MAP_ANON</code> flags. |
||
== Memory visibility == |
== Memory visibility == |
||
If the mapping is ''shared'' (the <code>MAP_SHARED</code> flag is set), then it is preserved |
If the mapping is ''shared'' (the <code>MAP_SHARED</code> flag is set), then it is preserved when a process is forked (using a [[fork (system call)|<code>fork(2)</code>]] 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 <code>MAP_ANONYMOUS</code>) the underlying file medium is only guaranteed to be written after it is passed to the <code>msync(2)</code> system call. In contrast, if the mapping is ''private'' (the <code>MAP_PRIVATE</code> flag is set), the changes will neither be seen by other processes nor written to the file. |
||
⚫ | |||
If the mapping is ''private'' (the <code>MAP_PRIVATE</code> flag is set), the changes will neither be seen by other processes nor written to the file. |
|||
⚫ | 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]] (IPC). On modern [[operating system]]s, mmap(2) is typically preferred to the [[System V]] IPC [[Shared memory (interprocess communication)|Shared Memory]] facility.<ref name="Kerrisk 2010 p. 1116">{{cite book | last=Kerrisk | first=Michael | title=The Linux programming interface : a Linux and UNIX system programming handbook | publisher=No Starch Press | publication-place=San Francisco | year=2010 | isbn=978-1-59327-291-3 | oclc=728672600 | page=1116}}</ref> |
||
⚫ | |||
⚫ | 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). |
||
⚫ | mmap |
||
⚫ | The main difference between System V shared memory (shmem) and memory mapped I/O (mmap) is that |
||
== Example of usage under the C programming language == |
== Example of usage under the C programming language == |
||
<syntaxhighlight lang="c"> |
<syntaxhighlight lang="c" line> |
||
#include <sys/types.h> |
#include <sys/types.h> |
||
#include <sys/mman.h> |
#include <sys/mman.h> |
||
Line 34: | Line 34: | ||
#include <unistd.h> |
#include <unistd.h> |
||
/* |
/* 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) |
int main(void) |
||
{ |
{ |
||
Line 91: | Line 96: | ||
PID 22476: anonymous string 2, zero-backed string 2 |
PID 22476: anonymous string 2, zero-backed string 2 |
||
</nowiki></pre> |
</nowiki></pre> |
||
== 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.<ref>{{Cite web |title=Are You Sure You Want to Use MMAP in Your Database Management System? |url=https://db.cs.cmu.edu/mmap-cidr2022/ |access-date=2023-07-04 |website=db.cs.cmu.edu |language=en}}</ref> |
|||
== See also == |
== See also == |
||
Line 111: | Line 119: | ||
**[https://developer.apple.com/documentation/Darwin/Reference/ManPages/man2/mmap.2.html Mac OS X] |
**[https://developer.apple.com/documentation/Darwin/Reference/ManPages/man2/mmap.2.html Mac OS X] |
||
**[http://docs.oracle.com/cd/E23824_01/html/821-1463/mmap-2.html#scrolltoc Solaris] |
**[http://docs.oracle.com/cd/E23824_01/html/821-1463/mmap-2.html#scrolltoc Solaris] |
||
**[https://archive. |
**[https://archive.today/20070310090003/http://devrsrc1.external.hp.com/STKS/cgi-bin/man2html?debug=0&manpage=/usr/share/man/man2.Z/mmap.2 HP-UX] |
||
**[http://www.qnx.com/developers/docs/6.4.1/neutrino/lib_ref/m/mmap.html QNX] |
**[http://www.qnx.com/developers/docs/6.4.1/neutrino/lib_ref/m/mmap.html QNX] |
||
*Windows |
*Windows |
Latest revision as of 11:23, 29 September 2023
In computing, mmap(2)
is a POSIX-compliant Unix system call that maps files or devices into memory. It is a method of memory-mapped file I/O. It implements demand paging 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 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, macOS and the BSDs, 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
[edit]The original design of memory-mapped files came from the TOPS-20 operating system. mmap
and associated systems calls were designed as part of the Berkeley Software Distribution (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.[1] Sun Microsystems had implemented this very API, though, in their SunOS operating system. The BSD developers at University of California, Berkeley unsuccessfully requested Sun to donate its implementation; 4.3BSD-Reno was instead shipped with an implementation based on the virtual memory system of Mach.[2]
File-backed and anonymous
[edit]File-backed mapping maps an area of the process's virtual memory 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. The contents are initialized to zero.[3] In this respect an anonymous mapping is similar to malloc, and is used in some malloc implementations for certain allocations, particularly large ones. Anonymous mappings are not part of the POSIX standard, but are implemented in almost all operating systems by the MAP_ANONYMOUS
and MAP_ANON
flags.
Memory visibility
[edit]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 (IPC). On modern operating systems, mmap(2) is typically preferred to the System V IPC Shared Memory facility.[4]
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
[edit]#include <sys/types.h>
#include <sys/mman.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* 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)
{
const char str1[] = "string 1";
const char str2[] = "string 2";
pid_t parpid = getpid(), childpid;
int fd = -1;
char *anon, *zero;
if ((fd = open("/dev/zero", O_RDWR, 0)) == -1)
err(1, "open");
anon = (char*)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0);
zero = (char*)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
if (anon == MAP_FAILED || zero == MAP_FAILED)
errx(1, "either mmap");
strcpy(anon, str1);
strcpy(zero, str1);
printf("PID %d:\tanonymous %s, zero-backed %s\n", parpid, anon, zero);
switch ((childpid = fork())) {
case -1:
err(1, "fork");
/* NOTREACHED */
case 0:
childpid = getpid();
printf("PID %d:\tanonymous %s, zero-backed %s\n", childpid, anon, zero);
sleep(3);
printf("PID %d:\tanonymous %s, zero-backed %s\n", childpid, anon, zero);
munmap(anon, 4096);
munmap(zero, 4096);
close(fd);
return EXIT_SUCCESS;
}
sleep(2);
strcpy(anon, str2);
strcpy(zero, str2);
printf("PID %d:\tanonymous %s, zero-backed %s\n", parpid, anon, zero);
munmap(anon, 4096);
munmap(zero, 4096);
close(fd);
return EXIT_SUCCESS;
}
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
[edit]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.[5]
See also
[edit]- Virtual memory for when there is more address space than physical memory
- Paging for the implementation of virtual memory
- Page cache for a disk caching mechanism utilized by mmap
- Demand paging for a scheme implemented by mmap
References
[edit]- ^ William Joy; Eric Cooper; Robert Fabry; Samuel Leffler; Kirk McKusick; David Mosher (1983). 4.2BSD System Manual (PDF) (Report). Computer Systems Research Group, University of California, Berkeley.
- ^ McKusick, Marshall Kirk (1999). "Twenty Years of Berkeley Unix: From AT&T-Owned to Freely Redistributable". Open Sources: Voices from the Open Source Revolution. O'Reilly.
- ^ "mmap(2) - Linux manual page".
- ^ Kerrisk, Michael (2010). The Linux programming interface : a Linux and UNIX system programming handbook. San Francisco: No Starch Press. p. 1116. ISBN 978-1-59327-291-3. OCLC 728672600.
- ^ "Are You Sure You Want to Use MMAP in Your Database Management System?". db.cs.cmu.edu. Retrieved 2023-07-04.
Further reading
[edit]- Description from POSIX standard
- Differences:
- Windows
- MapViewOfFile win32 function is somewhat equivalent to mmap.
- More example source code:
- SharedHashFile, An open source, shared memory hash table implemented using mmap().