当前位置:网站首页>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
边栏推荐
- Laravel swagger add access password
- Nifi changed UTC time to CST time
- Centos7 silently installs Oracle
- "Shandong University mobile Internet development technology teaching website construction" project training log V
- File permissions of day02 operation
- ASM插桩:学完ASM Tree api,再也不用怕hook了
- DCAT batch operation popup and parameter transfer
- Markdown syntax
- Interesting talk about performance optimization thread pool: is the more threads open, the better?
- C # judge whether the user accesses by mobile phone or computer
猜你喜欢
![[pycharm] pycharm remote connection server](/img/b2/a4e1c095343f9e635ff3dad1e3c507.png)
[pycharm] pycharm remote connection server

通过简单的脚本在Linux环境实现Mysql数据库的定时备份(Mysqldump命令备份)

北京宝德&TaoCloud共建信创之路

Bare metal cloud FASS high performance elastic block storage solution

Thinkphp6 output QR code image format to solve the conflict with debug

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

D3.JS 纵向关系图(加箭头,连接线文字描述)

Interesting talk about performance optimization thread pool: is the more threads open, the better?

Windos下安装pyspider报错:Please specify --curl-dir=/path/to/built/libcurl解决办法

深入理解MMAP原理,让大厂都爱不释手的技术
随机推荐
并发编程学习笔记 之 原子操作类AtomicReference、AtomicStampedReference详解
[go] use of defer
mysql在查询字符串类型的时候带单引号和不带的区别和原因
Win10+opencv3.2+vs2015 configuration
Some opportunities for young people in rural brand building
Centos7 silently installs Oracle
重庆大道云行作为软件产业代表受邀参加渝中区重点项目签约仪式
Show profiles of MySQL is used.
30 knowledge points that must be mastered in quantitative development [what is level-2 data]
与张小姐的春夏秋冬(4)
Flink connector Oracle CDC 实时同步数据到MySQL(Oracle12c)
Nifi changed UTC time to CST time
与张小姐的春夏秋冬(3)
Simple optimization of interesting apps for deep learning (suitable for novices)
Flutter 绘制技巧探索:一起画箭头(技巧拓展)
Use of file upload (2) -- upload to Alibaba cloud OSS file server
Spring, summer, autumn and winter with Miss Zhang (4)
与张小姐的春夏秋冬(5)
Research on the implementation principle of reentrantlock in concurrent programming learning notes
day02 作业之文件权限