The difference between Linux program storage structure and process structure heap and stack

Source: Internet
Author: User

Abstract: This article mainly describes the Linux system, program storage structure ( code area, data section and bbs area ) and the basic structure of the process ( code area, data section, BBS area, Heap and Stack), and the difference between heaps and stacks.
Linux program storage structure and process structure1.Linux Program Storage structureunder the Linux system, the program is a normal executable file, Figure 1 is a Linux under the ELF format executable file basic situation.
 
Figure 1 Basic information for an elf-formatted executable file
As can be seen, this elf format executable file is stored without being transferred into the memory, divided into code area (text), data area (database) and 3 parts for the initialization area (BSS). The basic description of each paragraph is as follows:
(1) code area (text Segment). Also known as the Body segment. A machine instruction that holds the CPU executable, usually the code area is shareable (that is, additional execution code can access it) the code area is usually read-only, making it read-only because it prevents the program from accidentally modifying its instructions. Therefore, the constant data allocates space in the code snippet at compile time. Example 1 shows .
The code area directives include opcode and operand (or object's address reference), and if it is a specific numeric value, it will be included directly in the code. If it is local data, it will allocate space at runtime and then reference the address of the data, and if it is the BBS area and data area, the address of the data will also be referenced in the code.
(2) Global initialization data area/static data area, or abbreviated data segment . The zone contains the global variables that are explicitly initialized in the program, Static variables that have been initialized (including global static variables and local static variables). However, variables declared by const and string constants request space in the code snippet.
(3) Uninitialized data area, also known as BBS area . An uninitialized global variable and an uninitialized static variable are deposited. The data in the BBS area is initialized by the kernel to 0 or a null pointer (NULL) before the program begins execution.
Example 1 shows that constant data allocates space in the code snippet at compile time.
#include <stdio.h>int main () {        char *buf =null;        printf ("%s\n", buf);        return 0;}

Check the size of each segment after compiling:


Add a character constant and const data constant in your code:
#include <stdio.h>const int I=10;int main () {        char *buf = NULL;        printf ("%s\n", buf);        return 0;}

Review after recompilation: The data for the code snippet has been increased by 4 bytes of Const I.


2.LINUX Process Structure    under Linux system, if an elf format executable is loaded into memory, Will evolve into one or more processes. The process is the basic snap-in for Linux transactions, and all processes have their own independent environment and resources. The environment of a process is determined and composed by the current system state and its parent process information.
   The code area, initialization data area, uninitialized data area, contextual information, and mounted signals, and so on, that a running process is requesting in memory space.
(1) code area . The code snippet for the executable file is loaded, and its location loaded into memory is done by the loader.
(2) global initialization data area/static data area . The executable data segment is loaded, Positions can also be separated after the code snippet. The program has requested space for the data segment at the beginning of the run, and is released when the program exits, so the lifetime of the data stored in the data segment is the entire program run process.
(3) uninitialized data area . Loading the executable BBS segment, The position can be separated or close to the data segment. The program at the beginning of the application for the part of the space, the program is released, the data stored in this part of the life cycle for the entire program running process.
(4) . The release is automatically assigned by the compiler. The automatic variables and the information required to save each function call are stored in this section. Each time the function is called, its return address and the caller's environment information are stored on the stack. The recently called function then allocates storage space on the stack for its automatic and temporary variables. Using stacks in this way, you can call C functions recursively. The return function uses a new stack frame each time it calls itself, so the set of variables in one function call instance does not affect the variables in another function call instance.
(5) , typically for dynamic storage allocation in the heap. The programmer is usually assigned and released, and if the programmer does not release, the program ends up being retracted by the system. The heap is located between the non-initialized data segment and the stack.
    Figure 2 shows a comparison of the ELF format executable storage structure and the basic structure of the Linux process.

Figure 2 Executable file and process store layout
3. Why do you divide so many districts? (1) The code snippet and the data segment are separated, the runtime is easy to load separately, the Harvard architecture processor will achieve better pipeline processing efficiency .
(2) The code is executed sequentially, the processor's PC pointer is read in turn, and the code can be shared by multiple programs, the data throughout the run process may be used many times, if the combination of code and data will result in wasted space .
(3) Temporary data and code that needs to be reused are placed in the stack at run time, and the lifecycle is short, making it easier to improve resource utilization .
(4) The heap area can be assigned and released by programmers so that the user is free to allocate and improve the flexibility of the program .
4. Heap and Stack differencesA stack is a space that is allocated by the compiler when the program runs, and is maintained by the operating system. The heap is a block of memory allocated by the malloc () function, and the management of the memory is controlled manually by the programmer, and the C language uses the free () function to complete. The main difference is the following:
(1) different management methods
The program is automatically managed by the operating system at run time, without manual control by the programmer, and the application and release of heap space is controlled by the programmer, which is prone to memory leaks.
(2) different space sizes
Stack is to the low address extension, is a contiguous area of memory. That is, the top of the stack address and the maximum capacity of the stack is pre-defined, when the requested space exceeds the remaining space on the stack, there will be a stack overflow error. The heap is extended to high addresses and is a discontinuous memory area. Because the system uses a linked list to store free memory addresses, and the traversal direction of the list is by low address .
(3) different fragments of production
For the heap, the frequent malloc/free will inevitably cause the memory space discontinuity to cause the massive fragment, causes the program efficiency to reduce. For the stack, it must be a contiguous physical memory space.
(4) Different growth modes
On the X86 platform, the growth direction of the heap is upward, that is, the direction of increasing the memory address, and the growth direction of the stack is downward, that is, the direction of reducing the memory address.
(5) different distribution methods
The heap is a program that is dynamically allocated by the malloc () function and freed by the free () function, and the allocation and release of the stack is done by the operating system, and the dynamic allocation of the stack is done by the Alloca () function, but the dynamic allocation of the stack is different from the heap, which is allocated and freed by the compiler without manual completion
(6) Different allocation efficiency
The stack is provided by the system, and the operating system supports the stack at the bottom: assigning the address of a dedicated register storage stack, Stack out stacks are specialized instruction executions. The heap is provided by the C function library, and its mechanism is complex, for example, in order to allocate a piece of memory, the operating system will need to re-organize the memory, search for the memory space, so that there is a chance to get enough memory and then return. Obviously, the heap is much less efficient than the stack

Author: Personal ability is limited, just study reference ... If the reader finds an error in the text, please ask.
-- ----------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------not to build a plateau in the sand, calm down, To precipitate-----------------------------------------slowly------------------------------------------------------------ ----------------------------------------------------------------------------------------------

The difference between Linux program storage structure and process structure heap and stack

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.