|
Mapped Memory permits different processes to communicate via a shared file. Mapped Memory can be used for Interprocess Communication or as an easy way to access the contents of a file.
Mapped Memory forms an association between a file and a process’s memory. Linux splits the file into page-sized chunks and then copies them into virtual memory pages so that they can be made available in a process’s address space. Thus, the process can read the file’s contents by writing to memory. This permits fast access to files.
A Mapped Memory can also be thought of as allocating a buffer to hold a file’s entire contents, and then reading the file into buffer and writing the buffer back out to the file afterward.
Mapping an ordinary File
To map an ordinary file to a process’s memory, use the mmap(“Memory Mapped”) call. The first argument is the address at which you would like linux to map the file into your process’s address space; the value NULL allows Linux to choose an available start address. The second argument is the length of the map in bytes. The third argument specifies the protection on the mapped address range. The fourth argument is a flag value that specifies additional options. The fifth argument is a file descriptor opened to the file to be mapped. The last argument is the offset from the beginning of the file from which to start the map. You can map all or part of the file into memory by choosing the starting offset and length appropriately.
The flag value is a bitwise ‘OR’ of these constraints:
- MAP_FIXED
If you specify this flag, Linux uses the address you request to map the file rather than treating it as a hint. This address must be page_aligned.
- MAP_PRIVATE
Writes to the memory range should not be written back to the attached file, but to a private copy of the file. No other process sees these writes. This mode may not be used with MAP_SHARED.
- MAP_SHARED
Writes are immediately reflected in the underlying file rather than buffering writes. Use this mode when using mapped memory for Interprocess Communication. This mode may not be used with MAP_PRIVATE.
If the call succeeds, it returns a pointer to the beginning of the memory. On failure, it returns MAP_FAILED.
When memory Mapping’s usage is finished, release it by using munmap. Pass it the start address and length of the mapped memory region. Linux automatically unmaps mapped regions when a process terminates.
Shared Access to a file
Different processes can communicate using memory mapped regions associated with the same file. Specify the MAP_SHAERD flag so that any writes to these regions are immediately transferred to the underlying file and made visible to the other process. If this flag is not specified, Linux may buffer writes before transferring them to the file. For eg. to flush a shared file mapped at address mem_addr of length of length mem_length bytes, call this:
Msync(mem_addr,mem_length,MS_SYNC | MS_INVALIDATE);
Users of Mapped Memory regions must establish and follow a protocol to avoid race conditions.
Private mapping
Specifying MAP_PRIVATE to mmap creates a copy-on-write region. Any write to the region is reflected only in this process’s memory; other processes that map the same file won’t see the changes. Instead of writing directly to a page shared by all processes, the process writes to a private copy of this page. All subsequent reading and writing by the process use this page.
The mmap call can be used for purposes other than IPC. One common use is as a replacement for read and write. For eg. rather than explicitly reading a file’s contents into memory, a program might map the file into memory and scan it using memory reads. For some programs, this is more convenient and may also run faster then explicit file I/O operations.
|