Process Virtual Address Range

In short: is the process virtual address space continuous?

I need to know something about the virtual address allocated to the process by the kernel. Please correct me if I am wrong when I continue.

When creating a process, the kernel allocates virtual memory in the process and saves the starting and ending values โ€‹โ€‹of the virtual addresses of various segments of the process in mm_struct in task_struct .

Now say that the process has ended from the heap, and it needs to increase the size of the heap. .

If the range of virtual addresses is contiguous, is this a new allocated piece of the heap provided from outside the range that was originally allocated for this process? Or it is highlighted in such a way that the new fragment is next to the original. What to do if there is no space for this (since there is a segment with a memory display). How is it tracked? If the virtual address range is not in contact, how vm_struct track the various fragments of the address ranges for the heap (or any other segment)?

Can you clarify my concept?

+6
source share
3 answers

The virtual address space is not contiguous. See the output of cat /proc/<pid>/mem .

When the process starts, the kernel allocates several mappings for the dynamic linker and the process itself. Subsequently, the dynamic linker allocates more mappings through mmap() , and the process can select more mappings through mmap() and expand the heap through brk() . malloc() for dlmalloc and derivatives uses brk() for distributions less than the threshold, and mmap() for distributions greater than or equal to this threshold (about 128K IIRC).

In any case, when mmap() called, the kernel usually displays memory far from the heap, so there is usually enough space to expand the heap. If there is no virtual space left to expand the heap, brk() will fail.

+8
source

No, the virtual address space of a process is not necessarily contiguous. In the old days, a process received memory through brk , which actually caused the heap of the process to be an adjacent memory zone. Currently, memory allocation is done through mmap , which can process the page of the virtual memory of a process page by page.

If you're interested in learning about the kernel side, I recommend two links:

If you want to research on your system, you can see every mapping of the process memory in /proc/$pid/maps . See How to read from / proc / $ pid / mem on Linux? for more information.

+6
source

thank you .. after going through the indicated literature in accordance with my understanding,

virtual address space is not continuous throughout the process, nor even throughout the entire memory segment. and the different tiers of the virtual address ranges are managed in the kernel using the AVL vm_area_struct tree (virtual memory areas). thereby easily adding and removing pieces of virtual memory areas in the task_struct process. ref: virtual memory . but the virtual memory areas themselves are contiguous.

i.e. in fact, task_struct contains a pointer to mm_struct , which contains a pointer to the heads of the AVL trees (one tree for each memory area). tree nodes are nothing more than vm_area_struct , which have start and end pointers to mark the start and end of virtual memory areas.

Many thanks

+1
source

Source: https://habr.com/ru/post/887560/


All Articles