Mmap

From Hackepedia
Revision as of 09:27, 28 March 2013 by Pbug (talk | contribs) (forking children)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

mmap is a system call to set up a memory mapping.

If an anonymous region of memory is memory mapped it's similar to malloc'ing memory. When the MAP_SHARED flag is given to mmap on an anonymous region then that region is shared across all childs of a process (result of forking). This is a simple shared memory setup. The apache webserver uses this concept to collect statistics on all children that it spawns (see fork).


If a file is mmap'ed that file can be treated like memory and access is fast, hence some programs (cp on FreeBSD if I recall correctly) use it. Pages written to a file mapped memory region should get msync'ed to reflect the changes, otherwise dirtied pages (that's a changed page) are written to the file upon munmap.


locking memory

mlock locks a region of mmap'ed memory either partially or fully depending on the length given. Locked memory does not get paged out to disk (swap) and mlocked memory should stay in physical memory until reclaimed.

flushing dirtied memory

Dirty pages are modified pages held in memory of perhaps and mmaped file. Use the msync system call to flush changes from memory to the file.

reclaiming an mmap'ed region

use the munmap call for this. Usually this is when changes in memory are committed to disk too.