- mmap
-
In computing,
mmap
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 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,
mmap
can create several types of mappings.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
malloc
, and is used in somemalloc
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 ismmap
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 systemsmmap
is typically preferred to the System V IPC Shared Memory 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 memory for a general context of possessing more addresses than physical memory
- Swapping or Paging for implementation of virtual memory used in contemporary systems
- Page cache, a disk caching mechanism utilized by mmap
- Demand paging a scheme implemented by mmap
- Swapping or Paging for implementation of virtual memory used in contemporary systems
References and further reading
- Digital Equipment Corporation. "Shared Memory". Guide to Realtime Programming. http://www.nacs.uci.edu/dcslib/digital_unix/digital-v40d/APS33DTE/DOCU_004.HTM. Retrieved 2009-09-11.[dead link]
- Brian "Beej" Hall. "Memory Mapped Files". Beej's Guide to Unix Interprocess Communication. http://beej.us/guide/bgipc/output/html/multipage/mmap.html. Retrieved 2011-07-15.
- Description from POSIX standard
- - Linux man page
- - man page on die.net
- A Performance Comparison of "read" and "mmap" in the Solaris 8 OS
- Windows
- MapViewOfFile win32 function is somewhat equivalent to mmap.
Inter-process communication in computing Methods File · Memory-mapped file · Message passing · Message queue and mailbox · Named pipe · Anonymous pipe · Pipe · Semaphore · Shared memory · Signal · Sockets : Internet and Unix domainSelected protocols
and standardsLibraries
and frameworksChannel · D-BusCategories:- Inter-process communication
- C POSIX library
- Computer programming stubs
- Virtual memory for a general context of possessing more addresses than physical memory
Wikimedia Foundation. 2010.