In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "Linux memory layout", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Linux memory layout is what kind of" article.
Let's first take a look at the Linux memory layout:
In linux, each process is abstracted as a task_struct structure, called a process descriptor, which stores the process.
All kinds of information; such as open files, signals, memory, etc.; then an attribute of task_struct, mm_struct, manages all the virtual memory of the process, called memory descriptors. In the mm_struct structure, the beginning and end of each memory segment of the process is stored, as shown in the above figure; the physical memory used by the process, that is, the number of resident memory RSS pages, the number of virtual address space VSZ pages used by this memory, and the process virtual memory area collection and page table.
As can be seen from the above diagram, the process has code segments Text segment, data segments (initialized global, static variables), BSS segments (uninitialized global, static variables), heap, memory mapping area, and stack
Each virtual memory area (VMA) consists of a contiguous block of virtual addresses that are never overwritten. An vm_area_struct instance describes a memory area, including the start and end addresses of the memory area; the flags flag determines the access permissions and behavior of the memory; vm_file determines which file maps the memory, and if there is no file mapping, the memory is anonymous. Each memory segment mentioned in the above figure corresponds to a vm_area_struct structure. As shown in the following figure
The image above shows the memory layout of the / bin/gonzo process. The binaries of the program are mapped to code segments and data segments, which are read-only and execute-only and can not be changed; global and static uninitialized variables are mapped to BSS segments, for anonymous mapping, and the heap and stack are also anonymously mapped, because there is no corresponding file mapping; memory mapping area can map shared libraries, mapping files and anonymous mapping, so this memory segment can be file mapping or anonymous mapping. And different files are mapped to different vm_area_ structure sections.
These vm_area_struct collections are stored in an one-way linked list and a red-black tree in mm_struct; when you output a / proc/pid/maps file, you only need to traverse this linked list. The main purpose of a red-black tree is to quickly locate a block of memory, and the root of the red-black tree is stored in the mm_ RB domain.
As mentioned earlier, linear addresses need to be translated into physical addresses through a page table. The memory descriptor of each process also holds the process page table pointer pgd, and each virtual memory page corresponds to an item in the page table.
Virtual memory does not store any data, it just maps the address space to physical memory. Physical memory is allocated by the kernel partner system, and if a piece of physical memory is not mapped, it can be allocated to virtual memory by the partner system. The physical memory leaf box that has just been allocated may be anonymous, storing process data, or it may also cache, store data from files or block devices. A virtual memory vm_area_struct block is made up of contiguous virtual memory pages, while the physical memory mapped by these virtual memory blocks is not necessarily contiguous, as shown in the following figure:
As shown in the figure above, three pages are mapped to physical memory and two pages are not mapped, so the resident memory RSS is 12kb and the virtual memory size is 20kb. The Present flag for the page table item PTE that has three pages mapped to physical memory is set to 1, and the Present bit of the two virtual memory page table items that do not map physical memory is cleared. So accessing those two pieces of memory at this time will result in an abnormal missing page.
Vma is like a contract between the application and the kernel. When an application requests memory or file mapping, the kernel first responds to this request by allocating or updating virtual memory; however, the virtual memory is not mapped to real physical memory. Instead, physical memory is not really mapped until memory access results in a memory exception missing page. That is, when accessing unmapped virtual memory, a page fault exception occurs because the Present bit of the page table item is not set. Vma records and page table items play an important role in solving memory page gaps, freeing memory and memory swap out. The following figure shows the above situation:
1. At first, there is only 8kb memory in the heap, and it has been mapped to physical memory.
2. When the brk () function is called to extend the heap, the new page is not mapped to physical memory
3. When the processor needs to access an address and the address is in the virtual memory just allocated above, a page fault exception is generated.
4. At this time, the process requests a page of physical memory from the partner system, maps it to that piece of virtual memory, adds a page table entry, and sets the Present bit.
The above is about the content of this article "what is the memory layout of Linux?", I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.
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.