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

Source: Internet
Author: User

absrtact: This article mainly describes the Linux system. Program storage structure ( code area, data segment and BBS area ) and the basic structure of the process ( code area, data segment, 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 can run files in the basic situation.
 
Figure 1 Basic information about executable files in elf format
It can be seen that this elf format can run when the file is stored, is not transferred into memory, is divided into the Code area (text), the data area (3 parts) and the initialization area (BSS). The basic description of each paragraph is as follows:
(1) code area (text segment). Also known as the body segment. Store the machine instructions that the CPU can run. Usually the code area is shareable (that is, additional running code can access it) and the code area is generally read-only. The reason for it to be read only is to prevent the program from accidentally altering its instructions. Therefore, constant data allocates space in the code snippet at compile time. Example 1 illustrates this.
The code area directives include opcode and operand (or object's address reference). The hypothesis is a detailed value. will be included directly in the code. The assumption is that the local data will allocate space at execution time in the stack area. The address of the data is then referenced. Assume that the BBS area and the data area are the same as the address in the code that will reference the data.
(2) Global initialization of data area/static data area. or abbreviated data segment . This area includes the global variables that are understood to be 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 null pointer (NULL) before the program starts running.
Example 1. Description 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:

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmdawnjewmg==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "height=" width= "592" >
Add a character constant and const data constants to your code:

#include <stdio.h>const int I=10;int main () {        char *buf = NULL;        printf ("%s\n", buf);        return 0;}

Once again compiled view: The data for the code snippet has been added with a 4-byte const I.


2.LINUX Process Structure    under Linux system, assume that an elf format executable file is loaded into memory for execution. Will evolve into one or more processes. Processes are 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.
   A code area, initialization data area, uninitialized data area, contextual information, and mounted signals, and so on, that an executing process is requesting in memory space.
(1) code area . Loads a code snippet that can run a file. The position loaded into memory is completed by the loader.
(2) global initialization data area/static data area . The executable file data segment is loaded. Positions can be separated after the code snippet. The program has requested space for the data segment at the beginning of execution, and is released only when the program exits, so. The life cycle of the data stored in the data segment is the entire program execution process.
(3) uninitialized data area . The executable BBS segment is loaded, and the location can be separated and close to the data segment. The program applied for space at the beginning of the execution. The lifetime of the data stored in that part is released only when the program exits, and the entire program executes the process.
(4) . Released by the compiler itself. The active variable and the information that is required for each function call are stored in this section. Each time the function is called, the return address and the caller's environment information are stored in the stack. The recently called function then allocates storage space for its own active and transient variables on the stack. The stack is used in this way. Can call the C function recursively. The recursive function uses a new stack frame each time it calls itself, so a set of variables in a function call instance does not affect a variable in the instance of a function call.
(5) , which is typically used for dynamic storage allocation in the heap. Generally by the program ape distribution and release, if the program ape does not release, the program end is retrieved by the system. The heap is located between the non-initialized data segment and the stack.
    Figure 2 sees a comparison diagram of the ELF format that can run the file storage structure and the basic structure of the Linux process.

Figure 2 Running a file with the process store layout
3. Why do you divide so many districts? (1) separate the code snippet from the data segment. Execution is easy to load separately, and the processor at Harvard Architecture will get 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 execution process may be used multiple times. It is assumed that mixing code and data together will result in wasted space .
(3) Temporary data and code that needs to be reused are put in the stack at execution time, and the life cycle is short, which makes it easier to improve resource utilization .
(4) The heap area can be allocated and released by the program ape so that the user is free to distribute. increase the flexibility of the program .
4. Heap and Stack differencesA stack is a space that is allocated by the compiler when the program executes. Maintained by the operating system. The heap is a block of memory allocated by the malloc () function, the memory is managed manually by the program ape, and the C language uses the free () function to complete. The main differences are the following:
(1) different management methods
The program at execution time stack by the operating system itself actively managed, without the program Ape manual control. While the application of heap space, release work by the program Ape control, easy to generate memory leaks.
(2) different space sizes
The stack is to the low address extension, is a contiguous area of memory. That is, the stack top address and stack maximum capacity is pre-defined, when the requested space exceeds the remaining space on the stack, there will be 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 spare memory addresses. and the traversal direction of the linked list is by the low address high address.
(3) different fragments of production
For heaps. Frequent malloc/free are bound to cause a large amount of fragmentation due to the discontinuous memory space, thus reducing the efficiency of the program. For Stacks. Must be a contiguous physical memory space.
(4) Different growth modes
On the X86 platform, the growth direction of the heap is upward. The direction that is added toward the memory address. The growth direction of the stack is downward, that is, toward the memory address reduction direction.
(5) different distribution methods
The heap is in the program that is dynamically requested by the malloc () function and freed by the free () function. Stack allocation and release is completed by the operating system, the dynamic allocation of the stack of Alloca () function is complete, but the dynamic allocation of the stack and the heap is different, it is allocated and released by the compiler, without manual completion.
(6) Different allocation efficiency
The stack is provided by the system. The operating system will support the stack at the bottom: assigning the address of a dedicated register stack, and the stack out is a dedicated instruction run. The heap is provided by the C function library, and its mechanism is very complex. For example, to allocate a piece of memory, the operating system will need to defragment the memory again. The search organizes the memory space so that there is a chance of having enough memory and then returning. Apparently. The heap is much less efficient than the stack.

Author: Personal Ability is limited, is only the study of the test ... If the reader finds an error in the text, please ask.
-- ----------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------not to build a plateau in the sand. Calm down to the heart. To precipitate-----------------------------------------slowly------------------------------------------------------------ ----------------------------------------------------------------------------------------------

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

Related Article

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.