In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly talks about "what is mmap in LINUX". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is mmap in LINUX"?
Analysis of mmap in LINUX
I. basic principles and classification of mmap
In LINUX, we can use mmap to allocate and create a virtual memory address map in the process virtual address space.
It can be
1. File mapping
Initialize memory with the contents of the file
2. Anonymous mapping
Initialize memory space with all zeros (calloc is also available)
The following pictures are from the UNIX system programming manual
As for whether to share or not, it is divided into
1. Private mapping (MAP_PRIVATE)
Multi-process data sharing, modifications do not reflect the actual files on disk
Private write-time replication implementation
2. Shared mapping (MAP_SHARED)
Data is shared among multiple processes, and changes are reflected in the actual files on the disk.
So to sum up, there are four combinations.
1. Private file mapping
Multiple processes are initialized with the same physical memory page, but each process
Changes to memory files are not shared and are not reflected in physical files, such as
Our LINUX .so dynamic library files are mapped to each process virtual in this way
In the address space
2. Private anonymous mapping
Mmap creates a new mapping that is not shared by each process, which is used primarily for
Allocate memory (mmap is called when malloc allocates large memory).
3. Shared file mapping
Multiple processes share the same physical memory space through virtual memory technology.
Will be reflected in the actual physical file, and it is also a mechanism for interprocess communication (IPC)
4. Share anonymous mapping
This mechanism does not use write-time replication in fork, and the parent and child processes share it completely.
The same physical memory page, which also implements parent-child process communication (IPC).
The following is also a screenshot of the UNIX system programming manual
Under / proc/PID/maps, we can find a map created by the current process using mmap, such as:
379a000000-379a016000 r-xp 00000000 08:03 12320771 / lib64/libgcc_s-4.4.7-20120601.so.1
379a016000-379a215000-p 00016000 08:03 12320771 / lib64/libgcc_s-4.4.7-20120601.so.1
379a215000-379a216000 rw-p 00015000 08:03 12320771 / lib64/libgcc_s-4.4.7-20120601.so.1
379a400000-379a4e8000 r-xp 00000000 08:03 9700201 / usr/lib64/libstdc++.so.6.0.13
379a4e8000-379a6e8000-p 000e8000 08:03 9700201 / usr/lib64/libstdc++.so.6.0.13
379a6e8000-379a6ef000 Rmurmurp 000e8000 08:03 9700201 / usr/lib64/libstdc++.so.6.0.13
379a6ef000-379a6f1000 rw-p 000ef000 08:03 9700201 / usr/lib64/libstdc++.so.6.0.13
For explanation, you can refer to the UNIX system programming manual as described below
II. Mmap function prototype
Void * mmap (void * addr, size_t length, int prot, int flags,int fd, off_t offset)
There are a lot of parameters.
Addr: where to put the mapping (virtual address), which is usually passed to NULL and left to the kernel to decide.
Length: the size of the mapping (direct), and the minimum is an integral multiple of the system page (4K)
Port: bitmap mask
PROT_NONE cannot be accessed
PROT_READ readable
PROT_WRITE can be modified
PROT_EXEC executable
Illegally access or report SIGSEGV segment error signal
Flags: bitmap mask
MAP_ANONYMOUS: create an anonymous mapping
MAP_PRIVATE: private mapping
MAP_SHARED: shared mapping (note that it is not guaranteed to actually write to the physical disk (MSYNC))
MAP_FIXED:addr must be a page aligned address
Other signs are not explained.
Fd: the file descriptor of the mapping file
Offset: where to start the mapping in the file, which must be an integral multiple of the system page (4K)
Return value:
The starting address of the mapped virtual memory address is returned successfully, and MAP_FAILED is returned if failed.
Third, establish anonymous mapping
1. The pointer MAP_ANONYMOUS, and fd is specified as 0
2. Open the / dev/zero file and pass the file descriptor to mmap ()
Anonymous mapping allocates virtual memory space initialized with all zeros
IV. Other functions
Int msync (void * addr, size_t length, int flags)
Used to synchronize kener buffer data to disk
Int munmap (void * addr, size_t length)
Used to de-map
5. Program examples
Next, we use mmap to do private anonymous mapping to complete a small inter-thread synchronization problem program, using this piece
Memory area for inter-thread communication
Click (here) to collapse or open
# include
# include
# include
# include
# define uint unsigned int
# define MMSIZE (uint) (1
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.