Chapter 26. Memory-Mapped Files

Chapter 25, "Shared Memory," showed how shared memory could be used to share information between processes. A more modern feature of the UNIX kernel permits memory to be mapped to a regular file or a character device. This technique allows unrelated processes to share information as well, but adds a number of new features to the programmer's repertoire.

All executable files under modern UNIX kernels are mapped to virtual memory pages. These pages of memory are marked as being executable only within the process memory (on many platforms, this often implies that they are readable as well). In this manner, only those memory pages needed are actually paged into memory upon demand. For large programs, this is more efficient than loading the entire program into memory at startup.

Memory mapping simply extends this idea to application data files. Figure 26.1 shows how a memory-mapped file might be accessed from within a process's memory space.

The figure shows that the mapping may be larger than the actual file itself. This is often true because the virtual memory management performed by the UNIX kernel must use a fixed page size. Thus, Figure 26.1 shows that there is an extra region above the file's mapping. These extra bytes will be zeroed when the mapping is established.

When your application examines memory within the mapped region, pages of data are retrieved from the file as necessary to make the memory cells available. Likewise, if memory cells are modified, the changes are written back out to the file (depending upon options selected) at a time determined by the kernel. There are methods to control this behavior and its timing.

Figure 26.1. A file is mapped to process memory.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.229.253