Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 848
suppie Avatar asked May 08 '11 15:05

suppie


People also ask

What is the virtual address space of a process?

The virtual addresses used by different processes are isolated from each other. The code in one process cannot alter the physical memory that is being used by another process or the operating system. The range of virtual addresses that is available to a process is called the virtual address space for the process.

What is the range of the virtual address space in Linux?

For a 32-bit process, the virtual address space is usually the 2-gigabyte range 0x00000000 through 0x7FFFFFFF. For a 64-bit process, the virtual address space is the 8-terabyte range 0x000'00000000 through 0x7FF'FFFFFFFF. A range of virtual addresses is sometimes called a range of virtual memory.

What is the advantage of a virtual address in a processor?

As part of the read or write operation, the processor translates the virtual address to a physical address. Accessing memory through a virtual address has these advantages: A program can use a contiguous range of virtual addresses to access a large memory buffer that is not contiguous in physical memory.

What is the range of the virtual address of a pointer?

The range of virtual addresses usually starts at a low address and can extend to the highest address allowed by the computer's instruction set architecture and supported by the operating system 's pointer size implementation, which can be 4 bytes for 32-bit or 8 bytes for 64-bit OS versions.


1 Answers

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.

like image 54
ninjalj Avatar answered Oct 29 '22 05:10

ninjalj