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 introduces you what is virtual mapping and mmap (), the content is very detailed, interested friends can refer to, hope to be helpful to you.
Virtual memory mapping
We know that the program is stored on disk to a static file; the process is the process of running the program at one time. When the process starts to run, the process's code and data, etc., must be loaded into the process user space into the appropriate area. These areas are called code segments, data segments, etc., and the loaded data and code are called executable images of the process. From the above description, we can find that the process does not load the program into physical memory at once, but only loads the program into the user space of the process, which is called virtual memory mapping.
In the process of becoming an executable file, a source program goes through four stages: preprocessing, compilation, assembly and linking. Therefore, in order for a process to run successfully, it is necessary to load not only the process image in its user space, but also the function libraries and linkers used by the process. Therefore, a process is divided into several memory areas from user space. Linux uses the mm_struct structure to describe a process to the user address space, and the vm_area_struct structure to describe an area of memory in the process address space. Therefore, a vm_area_struct structure may represent a process to a data segment, a linker to a code segment, and so on.
The virtual memory mapping of a process only maps the file on disk to the user address space of the process, and does not establish the mapping of virtual memory to physical memory. When an executable image is mapped to the process user space and execution begins, only a small portion of the virtual pages are loaded into physical memory. During the subsequent execution of the process, if you need to access the data that is not in physical memory, a page fault interrupt (which is actually an exception) is generated and the required pages are transferred to physical memory from the swap area or disk. This process is the virtual memory request page mechanism.
Process to virtual storage area
So for an arbitrary process, we can look at its address space to memory area by following the method below.
Let's first look at a simple test program:
# include
< stdio.h ># include
< stdlib.h >Int main () {int iTunes 1; char * str=NULL; printf ("hello,world!\ n"); str= (char *) malloc (sizeof (char) * 1119); sleep (1000); return 0;}
The malloc function is used in this program, so the str variable is stored in the heap. We can see the memory space partition of the process by printing the / proc/3530/maps file. Of which 3530 is the id of the process.
Edsionte@edsionte-desktop:~$ cat / proc/3530/maps 0014a000-00165000 r-xp 00000000 08:07 398276 / lib/ld-2.11.1.so 00165000-00166000 Rmuri p 0001a000 08:07 398276 / lib/ld-2.11.1.so 00166000-00167000 rw-p 0001b000 08:07 398276 / lib/ld-2.11.1.so 001d8000-0032b000 r-xp 00000000 08:07 421931 / lib/tls/i686/cmov/libc-2.11.1.so 0032b000- 0032c000-p 00153000 08:07 421931 / lib/tls/i686/cmov/libc-2.11.1.so 0032c000-0032e000 Rafael 0032c000 p 00153000 08:07 421931 / lib/tls/i686/cmov/libc-2.11.1.so 0032e000-0032f000 rw-p 00155000 08:07 421931 / lib/tls/i686/cmov/libc-2.11.1.so 0032f000-00332000 rw-p 00000000 00:00 0000441000-00442000 r-xp 00000000 00:00 0 [vdso] 08048000- 08049000 r-xp 00000000 08:09 326401 / home/edsionte/test 08049000-0804a000 r murf p 00000000 08:09 326401 / home/edsionte/test 0804a000-0804b000 rw-p 00001000 08:09 326401 / home/edsionte/test 08958000-08979000 rw-p 00000000 00:00 0 [heap] b78ce000-b78cf000 rw-p 00000000 00:00 0 b78dd000-b78e0000 rw-p 00000000 00:00 0 bfa6a000-bfa7f000 rw-p 00000000 00:00 0 [stack]
Each line of information is displayed in turn for the actual address of the memory area-termination address, access rights, offset, major equipment number: secondary device number, inode, file.
The above information not only contains the memory areas of the test executable object, but also displays the memory area information of the / lib/ld-2.11.1.so (dynamic linker) file and / lib/tls/i686/cmov/libc-2.11.1.so (C library) file, respectively.
We can roughly judge the type of a memory area from its access rights. The meaning of each attribute symbol is as follows: r copyright readbook w writerexlylyexecuterecoverssharedrecoverplyprivate. Therefore, rmurx generally represents the code snippet of the program, which can be read and executed. Rw- may represent data segment, BSS segment, stack segment, etc., which can be read or written. The stack segment can be distinguished from the file name of the line information; if the file name of a line of information is empty, it may be the BSS segment. In addition, the above test process shares the kernel dynamic library, so the file name appears as vdso (Virtual Dynamic Shared Object) at lines 00441000-00442000.
Mmap system call
A new to virtual storage area can be created in the process-to-user space through the mmap system call. The system call to the prototype is as follows:
# include void * mmap (void * addr, size_t length, int prot, int flags, int fd, off_t offset)
This function can map the open file to the process user space to a piece of memory, and after successful execution, the function returns the mapping area to the first address. After users get the first address of this virtual memory, they can access the file as if they were accessing memory.
The parameters of the system call are described as follows:
Addr: mapping to user address space to starting address
Length: mapping area in bytes to length
Prot: map area to access mode. Including PROT_EXEC (executable), PROT_READ (readable), PROT_WRITE (writable), PROT_NONE (file inaccessible). This access mode cannot exceed the mapped file to open mode. For example, if the open mode of the mapped file is read-only, then the access mode from here to here cannot be read-write.
Flags: this field is flexible, so that the flag has different functions, as shown below:
MAP_SHARED: create a mapping area that can be shared by child processes
MAP_PRIVATE: create a "realistic copy" mapping area
MAP_ANONYMOUS: create an anonymous to mapping area that is independent of the process
Fd: the file descriptor to map to the process user space, which must be the file to open
Offset: the starting mapping offset of the file
An example of mmap ()
In this program, first open the file test.c as read-only, and then map the test.c file to the current process to the user address space through the file return to the file descriptor and the mmap function. After a successful execution of the mmap function, buf is assigned to the first address of the mapped virtual area. Note that the mmap function returns a pointer of type Void, while buf is a pointer of type char. When the return value of mmap is assigned to the buf variable, void* is automatically converted to type char*.
*, just as we usually use a chartype pointer variable to print out the data in buf in turn.
# include
< stdio.h ># include
< sys/mman.h ># include
< fcntl.h >Int main () {int iForce FD; char * buf = NULL; fd = open (". / test.c", O_RDONLY); if (fd < 0) {printf ("open error\ n"); return-1;} buf = mmap (NULL, 12, PROT_READ, MAP_PRIVATE, fd, 0); for (I = 0) ("% c", buf [I]) } printf ("\ n"); return 0;} about what is virtual mapping and mmap () is shared here, I hope 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.