Saturday, February 2, 2008

mmap
The mmap () system call allows the programmer to associate a region of the process virtual address space with a file. It is an extremely general purpose utility:
  • It allows the mapped memory to have protection attributes (readable, writable, execable).
  • It allows the process to have a private copy (on-write) version of the file; changes are private to the process and disappear when the process exits. Alternatively, it allows the process to share the mapping with other processes; writing the memory area is equivalent to writing the file.
  • The memory mapped region need not correspond to an actual file (i.e. anonymous); by creating an anonymous mmap in a parent and forking, the children can share memory.
Mmap has several desirable properties, including
  • The namespace for mmap corresponds to the filesystem, adhering to the "everything is a file" Unix ideal.
  • Access permissions correspond to file permissions.
  • The actual relationship between the virtual address space and physical memory consumed by the mmap is controlled by the OS; in particular, memory resources are automatically freed when all processes using an mmap either munmap or exit.
These properties stand in contrast to system V shared memory.

Associated with mmap are the system calls msync () and madvise (). msync instructs the OS to write all modified pages to disk, either synchronous (don't return until call is complete) or asynchronously (return after sync has been scheduled); the OS will also optionally asynchronously sync dirty pages to disk if the proper flag is passed to mmap. madvise provides hints to the kernel as to how the program will access the mmap, in order to optimize.