Process (program) memory space layout

It can be inferred that you lack confidence in a victory over your rivals from the fact that you are irritable against them. -------- If the enemy makes you angry, you have not overcome their grasp.

Recently read a previous blog, I feel a little sense of self-directed and starred in, is about knowledge or too boring stiff, black will add more later I understand the idea of ​​memory processes. In addition, because we are dealing with programmers often and English , at the beginning of each blog will join English Chicken Soup modules. Learn together and progress together. There is also hope that the Friends yards exhibitions.

Title (program) is actually not accurate. In fact, running programs should be written.
What is a process? What is the procedure?

  • Operating System such definitions: running programs and its resources (CPU, memory, system resources, etc.) is called the occupation process . Standing on the programmer's perspective, C source file is called, but the machine does not recognize the source to the point of view of the programmer, then we need to use the compiler generates a binary executable program for the CPU may be identified and stored on a storage medium, then compiled executable program can only be called and not called process.
  • Once the program starts running, then running the program and the resources used is called a process. This concept is a process for the system, rather than the user, the user, he was faced with the concept of the program. Clearly, a program can be executed multiple times, which means that multiple processes can execute the same program.

1, the illustrated

In fact, writing this blog is to lay the foundation for multi-process programming behind.
Linux process memory management objects are virtual memory per process innate respective noninterference in each other's virtual memory space 0-4G, 0-3G user space implementation of the user's own code, 1GB of space is a high execution kernel space Linu x system call, where to store the entire kernel code and all of the kernel module, the user can see and are in contact with the virtual address, not the actual physical memory address.

  • Linux under a three-part process data in memory, is the "code segment", "stack segment" and "data segment."
    代码段: As the name suggests, it is to store the data of the program code, if there are several machines the same process to run a program more, then they can use the same code.
    堆栈段: Storage is a subroutine return address, local variables subroutine parameters and procedures and malloc () dynamic application memory address.
    数据段: Storage procedures global variables, static variables and constants memory space.
    Here Insert Picture Description

  • Note that the section area figures do not care, this is based on each person's name for the habit dependent.

2, detailed explanation

The basic idea of Linux memory management is only accessible in real address when it established a physical map of the address, distribution Linux C / C ++ language There are 3 ways.
(1) from the allocated static storage area . Is the memory allocation data segment, this memory in the program has been allocated a good compilation stage, there during the entire operation of the program, such as global variables, static variables.
(2) created on the stack . During a function, the function of the storage unit can create a local variable on the stack, the memory cells are automatically released at the end of the function execution. Stack memory allocation operation in the processor instruction set, the efficiency is high, but finite system stack memory allocation, such as large array will cause the stack space explode
mistakes.
(3) from the heap allocation , known as dynamic memory allocation. Program at run time to apply any number of memory using malloc or new, the programmer is responsible when using free or delete release memory. This area is called dynamic memory allocation memory allocation. Dynamic memory is determined by the lifetime of us, very flexible, but also many problems, such as the value of a pointer to a block of memory has changed and no other pointer to this memory, this memory can not access, memory leak .

Layers Specific description
Stack area Stack memory to complete the program by the compiler at compile phase, the process of stack space at the top user-space process and growth is down, each call will be open each function in its own stack space stack space, function parameters, local variables the function returns the address, etc. are those of a first-order function pressed into the top of the stack in the stack, the function returns the function stack space disappears, the function returns the address of a local variable is illegal.
Heap Heap memory is allocated during the execution of the program, the process for storing operating variables are dynamically allocated, the size is not fixed, is positioned between the stack and non-stack initialization data segment, and the process is used to close the stack space . When the process calling malloc functions such as the allocation of memory, the stack frame memory is not the function of the newly allocated, but is dynamically added to the heap, then pile on expanding into high-address; when using the free time to free the memory and other functions, the released memory was kicked out from the heap, the heap will be reduced. Because dynamically allocated memory is not in the function stack frame, so even if the function returns this memory is not going away.
Uninitialized data segment (segment BSS) It used to store uninitialized of global variables and static variables static . And before the program begins execution, that is, before the main (), the kernel data in this segment will be initialized to zero or null pointer . Static variable resources released automatically by the system after the end of the program. Static allocation
Initialized data segment (.data) in some places will be directly called into segments Also a static memory allocation, to protect initialized the global variables and static variables static , read-only memory area belongs.
Literals segment (.rodata) Figure fancy
Text segment This is part of the executable machine instructions executed by the CPU. Often the text segment is read-only to prevent accidents due to modify the program of its own execution.
  • The following points should be noted

  • The program is not running essentially by the BSS segments, data segments, text composed of three segments.

  • BSS segment does not take the size of the executable file, which is to get the memory by the linker.

  • Bss segment content (data not initialized) is not stored in a file on disk. The reason is that the kernel before the program starts running they are set to 0.

  • Need to be stored in the program file, only the text segment and initialization data segment.

  • BSS segment size obtained from the executable file, then the linker to obtain this size memory block, the data segment immediately behind. When this program into the memory address space cleared.

  • Stack Area: stack area from the high to the low address bits of the address bits growth, is a contiguous area of ​​memory allocated stack area size is generally small.

  • Heap: for dynamic memory allocation, release and applications assigned by the programmer. It was a stack grows from lower to higher addresses bits, using a chain storage structure. Frequent malloc / free memory space caused by discontinuity, fragmentation. When the application heap space library functions is plenty of space available according to certain search algorithm. So heap of efficiency to be much lower than the stack.

3, code verification

C Use the following code verification process memory space above is correct.

#include <stdio.h>
#include <stdlib.h>


int   global_bss_var;     // 未初始化的全局变量,存放在数据段的BSS区,其值默认为0;
int   global_data_var = 5;  // 初始化了的全局变量,存放在数据段的DATA区,其值为初始化值20;


int main(int argc, char **argv) //argv里存放的是命令行参数,他存放在命令行参数区
{ 
    static int   local_bss_var;     // 未初始化的静态变量,存放在数据段的BSS区,其值默认为0;  
    


    static int   local_data_var = 10;  // 初始化了的静态变量,存放在数据段的DATA区,其值为初始化值10;
    


    char        *str="Hello";
    // str是初始化了的局部变量,存放在栈(STACK)中,其值是"Hello"这个字符串常量存放 在DATA段里RODATA区中的地址    
   

    char        *ptr;   
    // ptr是未初始化了的局部变量,存放在栈(STACK)中;其值为随机值,这时候的ptr称 为“野指针(为初始化的指针)”
   

    ptr = malloc(100);
    // malloc()会从堆(HEAP)中分配100个字节的内存空间,并将该内存空间的首地址返回给ptr存 放;
      

    printf("[cmd args]:  argv address: %p\n", argv);     
    printf("--------\n");
     
    printf("[ Stack]:     str address: %p\n", &str);   

    printf("[ Stack]:     ptr address: %p\n", &ptr); 
    printf("--------\n");
    
    printf("[ Heap ]:  malloc address: %p\n", ptr);      
    printf("--------\n");
    
    printf("[  bss ]:  local_bss_var address: %p value: %d\n", &local_bss_var, local_bss_var);      

    printf("[  bss ]:  global_bss_var address: %p value: %d\n", &global_bss_var, global_bss_var);
    

    printf("[ data ]:  local_data_var address: %p value: %d\n", &local_data_var, local_data_var);
   
    printf("[ data ]:  global_data_var address: %p value: %d\n", &global_data_var, global_data_var);       
    
    printf("[rodata]: \"%s\" address: %p \n", str, str);   
    printf("--------\n");
    
    printf("[ text ]:  main() address: %p\n", main); 
    printf("--------\n");
    
    return 0;

}

Here Insert Picture Description

Published 29 original articles · won praise 65 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_46027505/article/details/105076010