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
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
- 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
- ^ 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
- ^ 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
- ^ "mmap2 - Linux manual page"
- Description from POSIX standard
- Mac OS X
- MapViewOfFile win32 function is somewhat equivalent to mmap
- More example source code:
- SharedHashFile, An open source, shared memory hash table implemented using mmap
|Data exchange among threads in computer programs|
m.maploco.com, m.mapquest.com, mapquest, mmap, mmap.org, mmapayout.com, mmapinc.org, mmaplayground.com, mmappsmobile.com, www.mmapinc.org
mmap Information about
mmap viewing the topic.
There are excerpts from wikipedia on this article and video
Random Posts (searchxengine.com)
Our site has a system which serves search engine function.
You can search all data in our system with above button which written "What did you look for? "
Welcome to our simple, stylish and fast search engine system.
We have prepared this method why you can reach most accurate and most up to date knowladge. The search engine that developed for you transmits you to the latest and exact information with its basic and quick system.
You can find nearly everything data which found from internet with this system.