简体   繁体   中英

Virtual address range of a process

In short: is the virtual address space of a process contiguous?

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

On process creation, the kernel allocated virtual memory to the process and stores the starts and ends of the virtual addresses of the different segments of the process in the mm_struct in the task_struct .

Now say the a process has run out of the heap and needs to increase the heap size.calls brk() .

If the virtual address range is contiguous, is the newly allocated chunk of heap provided from outside the range that was allocated originally for this process? Or is it allocated in a way that the new chunk is adjacent to the original one. What if there is no space for that (because the memory mapped segment is lying there). how is it kept track of? If the virtual address range is not contiguous, how does the vm_struct keep track of the different chunks of the address ranges for the heap (or any other segment)?

Can you please clear my concept on that?

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

When starting a process, the kernel allocates several mappings for the dynamic linker and for the process itself. Afterwards, the dynamic linker allocates more mappings via mmap() , and the process can allocate more mappings via mmap() and extend the heap via brk() . malloc() on dlmalloc and derivatives uses brk() for allocations shorter than a threshold and mmap() for allocations larger than or equal to that threshold (around 128K IIRC).

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

No, the virtual address space of a process is not necessarily contiguous. In the old days, a process obtained memory through brk , which indeed forced the process heap to be a contiguous zone of memory. Nowadays memory allocation is done through mmap , which can manipulate the process's virtual memory page by page.

If you're curious about the kernel side of things, I recommend two references:

If you'd like to explore around on your system, you can see each process's memory mapping in /proc/$pid/maps . See How do I read from /proc/$pid/mem under Linux? for more information.

thanks.. after going through the said literatures as per my understanding,

the virtual address space is not contiguous throughout the process, as well as not even throughout a given memory segment. and the different chunks of virtual address ranges are managed in the kernel using an AVL tree of vm_area_struct ( virtual memory areas ). thereby easily adding and deleting chunks of virtual memory areas to the task_struct of the process. ref: Virtual Memory . but the virtual memory areas in itself are contiguous.

ie in effect the task_struct contains a pointer to mm_struct which contains a pointer to heads of AVL trees (one tree for every memory region). the nodes of the tree are nothing but vm_area_struct s which has start and end pointers to mark the start and end of the virtual memory areas

thanks a lot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM