当前位置:网站首页>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
边栏推荐
- Power BI Report Server 自定义身份验证
- Nifi changed UTC time to CST time
- 并发编程学习笔记 之 Lock锁及其实现类ReentrantLock、ReentrantReadWriteLock和StampedLock的基本用法
- 与张小姐的春夏秋冬(3)
- 全闪分布式,如何深度性能POC?
- 并发编程学习笔记 之 ReentrantLock实现原理的探究
- Show profiles of MySQL is used.
- Xsan is highly available - xdfs and San are integrated with new vitality
- Spring, summer, autumn and winter with Miss Zhang (2)
- 【语义分割】SETR_Rethinking Semantic Segmentation from a Sequence-to-Sequence Perspective with Transformer
猜你喜欢
Android studio login registration - source code (connect to MySQL database)
与张小姐的春夏秋冬(2)
手撕ORM 框架(泛型+注解+反射)
Windos下安装pyspider报错:Please specify --curl-dir=/path/to/built/libcurl解决办法
全闪分布式,如何深度性能POC?
Go|gin quickly use swagger
Training log III of "Shandong University mobile Internet development technology teaching website construction" project
微信内置浏览器禁止缓存的问题
PHP write a diaper to buy the lowest price in the whole network
Training log 6 of the project "construction of Shandong University mobile Internet development technology teaching website"
随机推荐
Study and research the way of programming
[DL] build convolutional neural network for regression prediction (detailed tutorial of data + code)
关于Flow的原理解析
Performance comparison | FASS iSCSI vs nvme/tcp
识变!应变!求变!
How to obtain openid of wechat applet in uni app project
nacos外置数据库的配置与使用
Detailed explanation of atomic operation class atomicinteger in learning notes of concurrent programming
【数据库】数据库课程设计一一疫苗接种数据库
Training log III of "Shandong University mobile Internet development technology teaching website construction" project
【ML】机器学习模型之PMML--概述
[DL] introduction and understanding of tensor
SQL repair duplicate data
Basic use of array -- traverse the circular array to find the maximum value, minimum value, maximum subscript and minimum subscript of the array
【目标检测】KL-Loss:Bounding Box Regression with Uncertainty for Accurate Object Detection
ASM插桩:学完ASM Tree api,再也不用怕hook了
Thinkphp6 pipeline mode pipeline use
全闪分布式,如何深度性能POC?
DCAT batch operation popup and parameter transfer
Realize the scheduled backup of MySQL database in Linux environment through simple script (mysqldump command backup)