In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail how to analyze the principle and use of Mmap. The content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.
I. traditional reading and writing documents
In general, modifying the contents of a file requires the following three steps:
Read the contents of the file into memory.
Modify the contents of memory.
Write the data in memory to the file.
The process is shown in figure 1:
If you use code to implement the above process, the code is as follows:
Read (fd, buf, 1024); / / read the contents of the file to buf. / / modify the contents of buf write (fd, buf, 1024); / / write the contents of buf to the file
As you can see from figure 1, page cache is the middle layer when reading and writing files, and the kernel uses page caching to associate with the data blocks of the file. So when the application reads and writes files, the actual operation is page caching.
Use mmap to read and write files
From the traditional process of reading and writing files, we can find one thing that can be optimized: if the page cache can be read and written directly in user space, then the process of copying page cached data to the user space buffer can be avoided.
So, is there any such technology that can achieve the above approach? The answer is yes, it is mmap.
Using mmap system call, the virtual memory address of user space can be mapped (bound) to the file, and reading and writing to the mapped virtual memory address is just like reading and writing to the file. The principle is shown in figure 2:
As we mentioned earlier, both reading and writing files need to be cached by pages, so mmap maps the page cache of the file, not the file itself on disk. Because mmap maps the page cache of a file, it involves the question of synchronization, that is, when the page cache synchronizes data to disk.
The Linux kernel does not actively synchronize the mmap-mapped page cache to disk, but requires the user to actively trigger it. There are four times to synchronize mmap-mapped memory to disk:
Call the msync function for active data synchronization (active).
When the munmap function is called to de-map the file (active).
When the process exits (passive).
When the system shuts down (passive).
III. How to use mmap
Let's show you how to use the mmap,mmap function. The prototype is as follows:
Void * mmap (void * addr, size_t length, int prot, int flags, int fd, off_t offset)
Here is the function of each parameter of the mmap function:
Addr: specify the mapped virtual memory address, which can be set to NULL, so that the Linux kernel automatically selects the appropriate virtual memory address.
Length: length of the mapping.
Prot: the protection mode of mapping memory. Available values are as follows:
PROT_EXEC: can be executed.
PROT_READ: can be read.
PROT_WRITE: can be written.
PROT_NONE: not accessible.
Flags: specify the type of mapping. Commonly available values are as follows:
MAP_FIXED: maps using the specified starting virtual memory address.
MAP_SHARED: share the mapping space (shared memory) with all other processes mapped to this file.
MAP_PRIVATE: create a private mapping space for Copy on Write.
MAP_LOCKED: locks the page in the mapping area to prevent the page from being swapped out of memory.
...
Fd: the file handle for mapping.
Offset: file offset (where does the mapping start in the file).
After introducing the prototype of the mmap function, we now show you how to use mmap with a simple example:
Int fd = open (filepath, O_RDWR, 0644); / / Open the file void * addr = mmap (NULL, 8192, PROT_WRITE, MAP_SHARED, fd, 4096); / / Map a pair of files
In the above example, we first open the file in a read-write manner through the open function, and then map the file through the mmap function as follows:
The addr parameter is set to NULL, which means that the operating system automatically selects the appropriate virtual memory address for mapping.
The length parameter set to 8192 indicates that the mapped area is the size of two memory pages (the size of one memory page is 4 KB).
Setting the prot parameter to PROT_WRITE indicates that the mapped memory area is readable and writable.
The flags parameter is set to MAP_SHARED to indicate the shared mapping area.
The fd parameter sets the handle to the open file.
The offset parameter set to 4096 indicates that the mapping starts at 4096 of the file.
The mmap function returns the mapped memory address, which allows us to read and write to the file. We show the structure of the above example in the kernel in figure 3:
On how to analyze the principle and use of Mmap to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.