Difference between revisions of "Mmap"

From Hackepedia
Jump to navigationJump to search
(more more)
(fix link)
Line 3: Line 3:
 
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.
 
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.
  
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]].
+
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]].
  
  

Revision as of 09:05, 28 March 2013

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.

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.