COP4610: Operating Systems & Concurrent Programming up ↑

Unix Memory Management API

 

Features of the Unix Memory Mapping API

Memory Mapping

#include <sys/mman.h>
void *mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off);
*

The mmap() function allows a process to establish a mapping between a range of the address space of the process and and a portion of an open file or shared memory object.

The user specifies the portion of the file. Normally, the system gets to choose the starting address of the corresponding "window" in the virtual address space of the process. The system is responsible for using the virtual memory mechanism to implement the mapping.

While the mapping is in effect, the contents of that portion of the file are accessible via memory, and vice versa. Changes made to that portion of the file affect the corresponding region of virtual memory, and vice versa.

"The prot argument determines whether read, write, execute, or some combination of accesses are permitted to the data being mapped." [Unix man-page] It is a bitwise "or" of one or more of the following:

The flags argument specified other infomration about the mapping, including the flag MAP_SHARED, which specifies that changes to the mapped data are shared (what one usually would want). Flag values include the following:

Memory Mapping Examples

These two programs illustrate the use of the mmap function.

The first one just consists of two processes that share a region of memory, by mapping it to a file.

The second one is more complicated, and also uses the POSIX semaphore operations. It implements a solution to the Dining Philosphers problem for processes.

Memory Protection

#include <sys/mman.h>
int mprotect(void *addr, size_t len, int prot);
"The mprotect() function changes the access protections on the mappings specified by the range [addr, addr + len), rounding len up to the next multiple of the page size as returned by sysconf(3C), to be that specified by prot." [Unix man pages]

memory locking (mlock(), mlockall())

#include <sys/mman.h>
int mlockall(int flags);
int munlockall(void);
int mlock(const void * addr, size_t len);
int munlock(const void * addr, size_t len);
The mlock() function uses the mappings established for the address range [addr, addr + len) to identify pages to be locked in memory. [Unix man pages]
The mlockall() function locks in memory all pages mapped by an address space.

With mlocakall(), the process can specify whether the locking applies only to current pages, only to future pages, or both, using the flags MCL_CURRENT and MCL_FUTURE. [Unix man pages]

Memory Synchronization

#include <sys/mman.h>
int msync(void *addr, size_t len, int flags);
The msync() function writes all modified copies of pages over the range [addr, addr + len) to the underlying hardware, or invalidates any copies so that further references to the pages will be obtained by the system from their permanent storage locations. The permanent storage for a modified MAP_SHARED mapping is the file the page is mapped to; the permanent storage for a modified MAP_PRIVATE mapping is its swap area. [Unix man pages]

The flags control details of the behavior:

T. P. Baker. ($Id)