English 中文(简体)
过程的虚拟处理范围
原标题:Virtual address range of a process

简言之,一个过程的虚拟空间是否毗连?

我需要了解一些关于花旗为这一进程分配的虚拟地址。 如果在我工作时我错了,请更正。

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.

i.e. 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_structs which has start and end pointers to mark the start and end of the virtual memory areas

thanks a lot





相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签