当前位置:网站首页>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
边栏推荐
- Semaphore (semaphore) for learning notes of concurrent programming
- 赓续新征程,共驭智存储
- 个人学习网站
- [CV] what are the specific numbers of convolution kernels (filters) 3*3, 5*5, 7*7 and 11*11?
- Ribbon学习笔记一
- Xsan is highly available - xdfs and San are integrated with new vitality
- ssm整合
- Some opportunities for young people in rural brand building
- 性能优化之趣谈线程池:线程开的越多就越好吗?
- A preliminary study on fastjason's autotype
猜你喜欢

Research and implementation of flash loan DAPP

My ideal job, the absolute freedom of coder farmers is the most important - the pursuit of entrepreneurship in the future

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

Spring, summer, autumn and winter with Miss Zhang (3)

在uni-app项目中,如何实现微信小程序openid的获取

Thinkphp6 pipeline mode pipeline use

Ribbon学习笔记二

Use of file upload (2) -- upload to Alibaba cloud OSS file server

Gluster cluster management analysis

Process management of day02 operation
随机推荐
Flink connector Oracle CDC 实时同步数据到MySQL(Oracle19c)
How to PR an open source composer project
【语义分割】语义分割综述
30 knowledge points that must be mastered in quantitative development [what is individual data]?
Reporting Services- Web Service
[clustmaps] visitor statistics
【目标检测】Generalized Focal Loss V1
datax安装
[ml] PMML of machine learning model -- Overview
【pycharm】pycharm远程连接服务器
并发编程学习笔记 之 Lock锁及其实现类ReentrantLock、ReentrantReadWriteLock和StampedLock的基本用法
关于Flow的原理解析
【目标检测】KL-Loss:Bounding Box Regression with Uncertainty for Accurate Object Detection
MySql统计函数COUNT详解
Lock lock of concurrent programming learning notes and its implementation basic usage of reentrantlock, reentrantreadwritelock and stampedlock
深入理解MMAP原理,让大厂都爱不释手的技术
asyncawait和promise的区别
【数据库】数据库课程设计一一疫苗接种数据库
Study and research the way of programming
微信小程序源码获取(附工具的下载)