encyclopegia.org

Mmap

* Marketing Metric Audit Protocol (MMAP) * Memory map * mmap, a UNIX system call for mapping files to memory * Marked Markovian Arrival Process[src]

In computing, mmap(2) is a POSIX-compliant Unix system call that maps files or devices into memory.[src] It is a method of memory-mapped file I/O.[src] It implements demand paging because file contents are not immediately read from disk and initially use no physical RAM at all.[src] The actual reads from disk are performed after a specific location is accessed, in a lazy manner.[src] After the mapping is no longer needed, the pointers must be unmapped with munmap(2) .[src] Protection information—for example, marking mapped regions as executable—can be managed using mprotect(2) , and special treatment can be enforced using madvise(2) .[src] In Linux, macOS and the BSDs, mmap can create several types of mappings.[src] 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.[src] ==History== The original design of memory-mapped files came from the TOPS-20 operating system.[src] mmap and associated system calls were designed as part of the Berkeley Software Distribution (BSD) version of Unix.[src] Their API was already described in the 4.2BSD System Manual, even though it was neither implemented in that release, nor in 4.3BSD.[src] Sun Microsystems had implemented this API, though, in the 4.0 release of their SunOS operating system.[src] 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.[src] 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.[src] N.B.[src] MAP_ANONYMOUS or MAP_ANON are supported by most UNIX versions, removing the original purpose of /dev/zero.[src] / /* 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(); pid_t childpid; int fd = -1; if ((fd = open("/dev/zero", O_RDWR, 0)) == -1) { err(1, "open"); } char* anon = (char*)mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_ANON|MAP_SHARED, -1, 0); char* 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"); // not reached 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 DragonFly BSD FreeBSD NetBSD OpenBSD illumos Mac OS X Solaris QNX Windows MapViewOfFile win32 function is somewhat equivalent to mmap.[src] More example source code: SharedHashFile, An open source, shared memory hash table implemented using mmap().[src]

19 sentences · 19 cited inline · 0 references stored

References

No reference rows published for this article.

Cite this article

MLA

"Mmap." encyclopegia.org, 2021, https://encyclopegia.org/wiki/mmap. Accessed 26 November 2026.

Chicago

"Mmap." encyclopegia.org. Accessed November 26, 2026. https://encyclopegia.org/wiki/mmap.

APA

encyclopegia.org. (2021). Mmap. Retrieved November 26, 2026, from https://encyclopegia.org/wiki/mmap

Accessed November 26, 2026 · CC BY-SA 4.0 · citation page · JSON

CC BY-SA 4.0 · share-alike · https://en.wikipedia.org/w/index.php?oldid=1024570745 · 0.5 · snapshot enwiki-001365927699

Cite · JSON · Search

Related services and records

Services

  • Estate — measured countries, lanes and facts
  • Intel — live corroborated events
  • Legal — statutes, cases and citations
  • Encyclopedia — articles with inline sources
  • Space — launches, satellites and orbits
  • Search — federated search and reports
  • Pass — passport account and SSO
  • Corpora — read-only corpus origins
  • e01 — self-hosted corpora and tools

Text and metadata only; each target carries its own sources. Missing data is published as no-data, never estimated.