Linux Process Memory Distribution __linux

Source: Internet
Author: User
Tags constant
Process Memory Distribution

has been in the analysis stack, the role of the stack this thing is also introduced almost, but where the stack has not been clear, as well as the heap, code, global variables where they are, this involves the process of memory distribution. process memory distribution for Linux 0.01

Memory distribution with the replacement of the operating system, more and more scientific and reasonable, and more complex, so we first understand the early operating system of the typical Linux 0.01 process memory distribution:

A process in Linux 0.01 is fixed with 64MB of linear memory space (the maximum memory footprint of a single program in the ACM Competition is limited to 64MB, there's got to be something fishy. O (∩_∩) o~, each process is placed in a single page catalog table, a page catalog table can manage 4G of linear space, so The linux0.01 has a maximum of 64 processes. The memory distribution for each process is as follows:

. Text contains the machine code sequence. Rodata is a source string, such as read-only content. The data store is the initialized global variable. BSS is an introduction, the existence of uninitialized global variable heap, stack is not to introduce it. The "note" static variable is not initialized with a default assignment of 0 or a space. Uninitialized variables and initialized to 0 are assigned to the. BSS segment.

. text. Rodata. BSS is resident memory, which means that the process runs from start to process zombie They are squatting there, so accessing them is a constant address , and the stack is a constant Gar (function call), minus frame (function return), Local variables within a frame can only be accessed relative to the current ESP (pointing to the top of the stack) or EBP (pointing to the current frame).

The stack is placed at a high address for a reason: the call function (Gar) is minus ESP, function return (frame) is added ESP, call in the front, so the stack is to the low address extension, put in a high address is more appropriate . process Memory distribution of modern operating systems

After understanding the memory distribution of Linux 0.01, see what happens to the memory distribution of modern operating systems:

  first of all, the Linux 0.01 process 64MB memory limit is too outdated, now the program has the potential to use 2GB, 3GB memory space (each process a page table of contents), of course, the machine has a bad words can not, my computer is only 2GB memory, want to use 3GB Memory is out of the expectation. But it is not 4GB memory can be used 4GB (32-bit), because the operating system also has to occupy a pit. Modern Linux 0xc0000000 above 1GB space is operating system-specific, and Linux 0.01, the 1th 64MB is the operating system pit, so the other processes completely occupy their 64MB, do not have to be polite with the operating system.

  second , Linux 0.01 has no threads, but modern Linux has multiple threads (Linux threads are actually a lightweight process), sharing global variables, heaps, open files among multiple threads of a process ... But stacks are not shared : each layer of function frames in the stack represents an execution clue, and a thread is an execution clue, so each thread is exclusive to one stack, which must be in the memory space of the owning process.

Based on the above two points, the memory distribution of the process becomes the following:

Moreover, if the dynamically loaded dynamic link library is also taken into account, the above map will be more "broken".

  If our program does not use multithreading, it is generally simple to think that its memory distribution model is the type of Linux 0.01 .

========================

A Linux process is divided into several parts (growing from a low address in a process's address space to a high address):
1.text paragraph, is to store code, readable executable can not be written, also known as the body section, code Snippets.
2.data segment, with initialized global variables and initialized static variables (whether local static or global static variables)
3.BSS segment, holding global uninitialized and uninitialized static variables (also no local or global static variable)

The above 3 parts is determined, that is, different procedures, the above 3 parts of the size are different, due to the program, if not initialized global variables defined more, then the BSS area will be larger, the other is small point.

4.heap, that is heap, heap in the process space is from low address to high address growth, you in the program through dynamic application to get the memory space (c in general for malloc/free,c++ in general for New/delete), is dynamically allocated in the heap.
5.stack, stacks, programs in each function of the local variables are stored in the stack, the stack is from the high address to the low address growth. At first, there was a large space between the heap and the stack, then, as the program runs, the heap continues to grow to a high address, the stack continues to grow to high address, so that the heap and stack space between the total has a maximum limit, beyond this maximum limit, there will be stacks and stack overlap, there will be errors, so generally speaking, The process under Linux has its maximum space.
6. To the top of a process address space, the command line arguments and environment variables are stored.

The entire process address space layout is shown in the following illustration:

The program runs, the system for the process address space in the TEXT/DATA/BSS segment map, by the system of the page fault exception handler on the disk on the program file in the real code, data writing process. In addition, all the variables in the BSS area will be zeroed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.