当前位置:网站首页>Technology that deeply understands the principle of MMAP and makes big manufacturers love it
Technology that deeply understands the principle of MMAP and makes big manufacturers love it
2022-07-29 06:01:00 【Code and thinking】
Like wechat MMKV Components 、 US mission Logan Components , And the log module of wechat xlog, Why do big factories prefer it ? What magic does he have ? I think the main reasons are as follows :
- Cross platform ,C++ To write , Can support multiple platforms
- Cross process , Through file sharing, multiple processes can share memory , Implement process communication
- High performance , Realize zero copy of user space and kernel space , Fast and memory saving
- High stability , Page break protector , Implemented by the operating system , Stability is conceivable
Function introduction
void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset);
- addr Represents the starting address of the mapped virtual memory ;
- length Represents the mapping length ;
- prot Describes the access rights of this new memory area ;
- flags Describes the type of the mapping ;
- fd Represents the file descriptor ;
- offset Represents the offset value in the file .
mmap The power of , It can be configured according to parameters , Used to create shared memory , So as to improve the file mapping area IO efficiency , Realization IO Zero copy , Next, we will talk about zero copy technology , In contrast , These functions are mainly determined by three parameters , Let's explain one by one
prot
Four situations are as follows :
- PROT_EXEC, Represents that the memory map has executable permissions , It can be regarded as a code segment , It usually stores CPU Executable machine code
- PROT_READ, Represents that the memory map is readable
- PROT_WRITE, Represents that the memory map is writable
- PROT_NONE, Represents that the memory map cannot be accessed
flags
The representative ones are as follows :
- MAP_SHARED, Create a shared mapping area
- MAP_PRIVATE, Create a private mapping area
- MAP_ANONYMOUS, Create an anonymous mapping area , This situation only needs to be passed in -1 that will do
- MAP_FIXED, When the operating system uses addr When memory mapping for the starting address , If it is found that the length or permission requirements cannot be met , Mapping will fail , If the MAP_FIXED, Then the system will find other suitable areas to map
fd
When parameters fd It's not equal to 0 when , Memory mapping will be associated with the file , If it is equal to 0, Will become anonymous mapping , here flags Must be MAP_ANONYMOUS
Application scenarios

One mmap There are so many functions , From applying for memory allocation to loading dynamic libraries , Then to interprocess communication , It's really omnipotent , Strong enough to make people fall to the ground . Here are four situations , Take a parent-child process communication that I am most concerned about as an example , Implement a simple parent-child process communication logic , After all, the purpose of our study is to apply , How can theory alone be called a qualified blog ?
The parent and child processes share memory
#include <iostream>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/mman.h>
int main() {
pid_t c_pid = fork();
char* shm = (char*)mmap(nullptr, 4096, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
if (c_pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
} else if (c_pid > 0) {
printf("parent process pid: %d\n", getpid());
sprintf(shm, "%s", "hello, my child");
printf("parent process got a message: %s\n", shm);
wait(nullptr);
} else {
printf("child process pid: %d\n", getpid());
sprintf(shm, "%s", "hello, father.");
printf("child process got a message: %s\n", shm);
exit(EXIT_SUCCESS);
}
return EXIT_SUCCESS;
}
After running, print as follows
parent process pid: 87799
parent process got a message: hello, my child
child process pid: 87800
child process got a message: hello, father.
Process finished with exit code 0
use mmap Created an anonymous shared memory area ,fd Pass in **-1 and MAP_ANONYMOUS Configure to implement anonymous mapping , Use MAP_SHARED** Create a shared area , Use fork Function create subprocess , In this way, sub process communication is realized , adopt sprintf Write the formatted data to the shared memory .
Cross process communication is achieved through a few lines of code , It's so simple , Such a powerful thing , Is there any support behind it ? With questions, let's go on to find out .
MMAP The patron saint behind
Speaking of MMAP The patron saint of , Home page learn about the next memory page : In page virtual memory , The virtual storage space and physical main storage space will be divided into pages of fixed size , Memory allocation for threads is also in pages . such as : The size of the page is 4K, that 4GB Storage space is needed 4GB/4KB=1M Bar record , That is to say 100 More than a 4KB Page of , In memory pages , When the user reads and writes files , The kernel will request a memory page and file to read and write , Pictured :

At this time, if there is no data in the memory page , An interrupt mechanism will occur , It is called page break , This interruption is MMAP The patron saint of , Why do you say that ? We know mmap After function call , The process virtual address space is only established at the time of allocation , There is no physical memory allocated for virtual memory , When accessing these virtual memory without mapping relationship ,CPU The loading instruction found that the code segment is missing , It triggers the page missing interrupt , After interruption , The kernel checks the region of the virtual address , Memory mapping found , You can calculate the file offset through the virtual memory address , Locate the page of the file corresponding to the page missing in memory , Boot disk by kernel IO, Load the corresponding page from disk into memory . Final protection mmap It can go on smoothly , Selfless dedication . Understand the missing page interrupt , Let's talk more mmap Memory allocation principle in four scenarios
Four scene allocation principles

The above is a simple principle summary , There is no detailed expansion , If you are interested, you can check the information yourself .
summary
This sharing , It mainly introduces mmap Four application scenarios , The communication between parent and child processes is verified by an example , And go deep mmap Find its protector , And deeply understand mmap In four scenarios , How is the operating system organized and allocated , Through the understanding of these , After you mmap The practical application has a better theoretical basis , According to different needs , Different performance requirements , Choose the most appropriate implementation .
author :i The headmaster
link :https://juejin.cn/post/7119116943256190990
边栏推荐
- 【bug】XLRDError: Excel xlsx file; not supported
- C# 判断用户是手机访问还是电脑访问
- Ribbon学习笔记二
- Detailed explanation of tool classes countdownlatch and cyclicbarrier of concurrent programming learning notes
- [go] use of defer
- 深入理解MMAP原理,让大厂都爱不释手的技术
- Use of file upload (2) -- upload to Alibaba cloud OSS file server
- Ribbon learning notes 1
- 【目标检测】Generalized Focal Loss V1
- Performance comparison | FASS iSCSI vs nvme/tcp
猜你喜欢

Android Studio 实现登录注册-源代码 (连接MySql数据库)

mysql 的show profiles 使用。

【网络设计】ConvNeXt:A ConvNet for the 2020s

PHP write a diaper to buy the lowest price in the whole network

并发编程学习笔记 之 工具类Semaphore(信号量)

Research on the implementation principle of reentrantlock in concurrent programming learning notes

Go|gin quickly use swagger

【目标检测】KL-Loss:Bounding Box Regression with Uncertainty for Accurate Object Detection

Tear the ORM framework by hand (generic + annotation + reflection)

并发编程学习笔记 之 ReentrantLock实现原理的探究
随机推荐
【目标检测】KL-Loss:Bounding Box Regression with Uncertainty for Accurate Object Detection
Flink connector Oracle CDC synchronizes data to MySQL in real time (oracle19c)
Basic use of array -- traverse the circular array to find the maximum value, minimum value, maximum subscript and minimum subscript of the array
The difference between asyncawait and promise
NIFI 改UTC时间为CST时间
Super simple integration of HMS ml kit to realize parent control
有价值的博客、面经收集(持续更新)
Novice introduction: download from PHP environment to thinkphp6 framework by hand
Flink, the mainstream real-time stream processing computing framework, is the first experience.
深入理解MMAP原理,让大厂都爱不释手的技术
【go】defer的使用
D3.JS 纵向关系图(加箭头,连接线文字描述)
【语义分割】SETR_Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformer
datax安装
Spring, summer, autumn and winter with Miss Zhang (5)
Research and implementation of flash loan DAPP
个人学习网站
与张小姐的春夏秋冬(1)
How to make interesting apps for deep learning with zero code (suitable for novices)
FFmpeg创作GIF表情包教程来了!赶紧说声多谢乌蝇哥?