Mon . 19 Aug 2019
TR | RU | UK | KK | BE |


In computing, mmap2 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 naturally implements demand paging, because file contents are not read from disk initially and 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 munmap2 the pointers to it Protection information can be managed using mprotect2, and special treatment can be enforced using madvise2

In Linux, Mac OS X and the BSDs, mmap can create several types of mappings


  • 1 History
  • 2 File-backed and anonymous
  • 3 Memory visibility
  • 4 Example of usage under the C programming language
  • 5 See also
  • 6 References and further reading


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 42BSD System Manual, even though it was neither implemented in that release, nor in 43BSD1 Sun Microsystems had implemented this very API, though, in their SunOS operating system The BSD developers at UC Berkeley requested Sun to donate its implementation, but these talks never led to any transfer of code; 43BSD-Reno was shipped instead with an implementation based on the virtual memory system of Mach2

File-backed and anonymousedit

File-backed mapping maps an area of the process's virtual memory to files; ie 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 zero3 In this respect an anonymous mapping is similar to malloc, and is used in some malloc3 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 visibilityedit

If the mapping is shared the MAP_SHARED flag is set, then it is preserved across a fork2 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 media is only guaranteed to be written after it is msync2'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

mmap2ing 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 mmap2 is sometimes used for Interprocess Communication IPC On modern operating systems mmap2 is typically preferred to the System V IPC Shared Memory facilitycitation needed

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

Example of usage under the C programming languageedit

#include <sys/typesh> #include <sys/mmanh> #include <errh> #include <fcntlh> #include <stdioh> #include <stdlibh> #include <stringh> #include <unistdh> / Does not work on OS X, as you can't mmap over /dev/zero / int mainvoid sleep2; strcpyanon, str2; strcpyzero, str2; printf"PID %d:\tanonymous %s, zero-backed %s\n", parpid, anon, zero; munmapanon, 4096; munmapzero, 4096; closefd; 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

See alsoedit

  • 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 and further readingedit

  1. ^ William Joy; Eric Cooper; Robert Fabry; Samuel Leffler; Kirk McKusick; David Mosher 1983 42BSD System Manual Report Computer Systems Research Group, University of California, Berkeley 
  2. ^ 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 
  3. ^ "mmap2 - Linux manual page" 
  • Description from POSIX standard
  • Differences:
    • FreeBSD
    • illumos
    • Linux
    • Mac OS X
    • Solaris
    • HP-UX
    • QNX
  • Windows
    • MapViewOfFile win32 function is somewhat equivalent to mmap
  • More example source code:
    • SharedHashFile, An open source, shared memory hash table implemented using mmap,, mapquest, mmap,,,,,,

mmap Information about


  • user icon

    mmap beatiful post thanks!


mmap viewing the topic.
mmap what, mmap who, mmap explanation

There are excerpts from wikipedia on this article and video

Random Posts

Amorphous metal

Amorphous metal

An amorphous metal also known as metallic glass or glassy metal is a solid metallic material, usuall...
Arthur Lake (bishop)

Arthur Lake (bishop)

Arthur Lake September 1569 – 4 May 1626 was Bishop of Bath and Wells and a translator of the King Ja...
John Hawkins (author)

John Hawkins (author)

Sir John Hawkins 29 March 1719 – 21 May 1789 was an English author and friend of Dr Samuel Johnson a...
McDonnell Douglas MD-12

McDonnell Douglas MD-12

The McDonnell Douglas MD-12 was an aircraft design study undertaken by the McDonnell Douglas company...

Random Posts (

Nikolskoe (Gorshechensky district)

Nikolskoe (Gorshechensky district)

Nikolskoye - a village in the Gorshechensky district of the Kursk region of Russia The administrativ


Chertanka is a toponym in Russia: The Chertanka tributary of the Coma is a river in the Krasnoyarsk
Thirteenth tale

Thirteenth tale

novel Author: Diane Setterfield Original language: English Date of writing: 2006 Date of firs
Bolshakovo (Chelyabinsk region)

Bolshakovo (Chelyabinsk region)

Bolshakovo is a village in the Chebarkulsky district of the Chelyabinsk region of Russia. It is a pa