preface
- The memory mapping 在
Linux
Very important in operating systems because it involves efficient cross-process communication & file manipulation - Today, I’m going to detail a core concept in operating systems: memory mapping
directory
Definition 1.
Associate one virtual memory region in the process with one object on disk so that there is a mapping between the two
- The above mapping procedure = initializes the virtual memory region
- Once the virtual memory area is initialized, you are swapped around in swap space
- The mapped objects are called shared objects (normal files/anonymous files)
2. The role
If the preceding mapping relationship exists, it has the following characteristics
- The virtual memory area of multiple processes has been mapped to a shared object
- If one process writes data to the virtual area
- It is also visible to the process that also maps the shared object to its own virtual memory region
The schematic diagram is as follows
- Assume that the virtual memory regions of processes 1 and 2 are mapped to the same shared object.
- When process 1 writes to its virtual memory region, it is also mapped to the virtual memory region in process 2
3. Implementation process
- The implementation process of memory mapping is mainly through
Linux
System call functions under the system:Mmap ()
- This function creates a virtual memory area and maps shared objects
- Its function prototype, specific use & internal process is as follows
Void *mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset); */ mmap(NULL, MAP_SIZE, PROT_READ, */ mmap(NULL, MAP_SIZE, PROT_READ, */ mmap(NULL, MAP_SIZE, PROT_READ, *)) MAP_PRIVATE, fd, 0); /** * Internal principles * Step 1: Create a virtual memory area * Step 2: implement address mapping, that is, the virtual address space of the process ->> Shared object * Note: * a. At this point, the virtual address does not have any data associated with the file, only to establish a mapping relationship * b. When one of the processes writes to virtual memory, the data is truly visible */Copy the code
Characteristics of 4.
- Improves data read, write & transfer time performance
- The number of data copies is reduced
- Efficient interaction between user-space and kernel space (direct interaction through mapped regions)
- Memory reads and writes instead of I/O reads and writes
- Improve memory utilization: Through virtual memory & shared objects
5. Application scenarios
In Linux, according to the essential principles & characteristics of memory mapping, its application scenarios are as follows:
- Implement memory sharing: such as cross-process communication
- Improve data read/write efficiency: for example, file read/write operations
6. Examples
In the following sections, I’ll detail an example of memory mapping applied to cross-process communication & file operations
6.1 File Read/Write Operations
- The traditional
Linux
The process for operating system files is as follows
- A memory-mapped file read/write operation was used
As can be seen from the above: the use of memory mapping file read/write mode is more efficient, the best performance!
6.2 Cross-process Communication
- Traditional cross-process communication
- Cross-process communication using memory mapping
As you can see from the above, cross-process communication using memory mapping is the most efficient and performs best!
7. To summarize
-
This article provides a comprehensive overview of memory mapping in Linux
-
I will continue to cover the basics of programming development. If you are interested, please follow the development notes for Carson_Ho