Process address space distribution and executable file distribution

Source: Internet
Author: User

Process Address Space

When the operating system manages memory, each process has a separate process address space, the address of the process address space is the virtual address, and for a 32-bit operating system, the virtual address space is 2^32=4GB. When the process executes, the memory address that is seen and used is a virtual address, and the operating system uses the MMU part to convert the virtual address used by the process to a physical address.

The process address space is divided into separate sections: 
(1) since some code in the system kernel is common to all processes, there is a dedicated area in the process address space of all processes that holds the common kernel code and data. The content within the zone is the same, and the virtual memory is mapped to the same physical memory area.  
(2) when the process executes, it needs to maintain process-related data structures, such as page tables, task and mm structures, kernel stacks, etc., which are process independent, Each process may be different. These data structures are in a dedicated region in the process virtual address space.   the
(3) process needs to use the stack when it makes a function call, so there is a dedicated virtual memory area in the process address space to maintain the user stack.   the
(4) process needs to use the heap when it allocates dynamic memory, so there is a dedicated virtual memory area in the process address space to maintain the heap.  
(5) Uninitialized data in the process in the. BSS segment   The data initialized in the
(6) process is in the. Data segment  
(7) The process code is in the. Text segment  
(8) When the process executes, the shared library may be called, and there is a memory map area of the shared library in the process address space, which is process-independent, because each process may invoke a different shared library.  
    linux The address space distribution of processes in the system as shown in, where the user space is 0-3GB in a 32-bit system , 3-4GB for kernel space:

content layout in elf files

The ELF (executable linkable Format) file is an executable file under Linux. The structure of the elf file is as follows:

Where the ELF header holds the beginning address and length of each segment and other information, each segment holds different attribute content. Where the variables in the C program are stored in the Elf file as shown in:


The size of the. BSS segment can be obtained from the information in the ELF header, but this is just a "size" data that tells the program how much memory is used by the uninitialized global and static variables, and there is no space in the file, only if the executable file is loaded and run. is allocated memory (and is located after the data segment memory block) and is initialized to 0.

When the executable file is loaded into memory, the individual segments are loaded into the virtual storage area as shown in the following scenario:

Memory Paging

The operating system uses a paging mechanism when managing memory, and the paging mechanism avoids frequent memory call-outs compared to non-fragmentation and paging, and utilizes memory resources more fully than fragments. General memory page size is the 4KB,32 bit system when the process through the virtual memory address to access data or code execution, the 32-bit virtual address of the 0-11 bit is the offset within the page; 31-12 bits is used to flag virtual pages.
If the 31-12-bit total can represent 2^20 pages, so if you use a page table, there will be 2^20 page table entries, each page table item 4 B, a total of 4MB, so the kernel space for each process requires 4MB of space to store the page directory. And if the multi-level page table mechanism, 31-22 bits for the page Directory table index, the page directory table is the address of the page table, the total need 2^10x4b = 4KB, each page table of Contents table entry point to a page table, the size of each page is 2^10x4 = 4KB, so that only in memory of the 8KB page directory table + one page table to convert the virtual address to a physical address (
When the process is actually executing, the locality principle makes most of the memory accesses likely be in the memory area of 4kbx4k = 4MB that can be indexed by a page table)
In the actual virtual address to physical address conversion, need to use MMU,MMU is a hardware circuit, contains two parts, one is a segmented part, a logical address is converted to a linear address, a paging part, the linear address is converted to a physical address.


Program Startup

With the above process address space distribution and executable file distribution information, the process initiation process can be described. When the program starts, the operating system creates a new process to execute the program, which is divided into three main steps:
(1) The operating system allocates a separate process address space, mainly in the memory of the kernel area to create a new description of the process of the structure (task_struct in Linux), the structure contains information about the process, such as process running state, process registers, process open resources, and the memory management structure of the process (Mm_struct in Linux, the memory management structure of the process describes the layout of the virtual address space of the process). At the same time, create a table of page catalogs for the process.

(2) Read the executable file header to establish the mapping between each segment in the executable file and the segments in the process virtual address space. When the program is running, you need to load the contents of the executable into memory to execute, such as when the process accesses a global variable, the global variable is not loaded into memory, you need to know where the global variable corresponds to the executable file. So we need to know where the virtual address in the process corresponds to the location in the executable file .

(3) Point the PC pointer to the code entry of the process and start execution
A page break occurs continuously during execution, and the contents of the actual executable file are loaded into physical memory when a page break occurs, and then the mappings between the virtual memory pages and the physical memory pages are established.

system-to-process management

The operating system kernel area stores the structure information of each process, and Linux contains information about the process, such as process status, register, kernel stack, state word, and memory allocation mm_struct. Task_struct,task_struct. For a single process, it uses and updates the information in the task_struct when it runs, such as using mm_struct for accessing memory ...
When the process switches, the system saves the information of the original process to its corresponding task_struct, then selects another process, loads the information in the task_struct into the register of the machine, and then the new process follows its task_struct to guide the operation ....

Fork Process

In Linux, fork is a system call that replicates the current process to get the child processes of the current process. Fork, the system creates a new process structure in the kernel task_struct, because most of the information of the parent-child process is the same at the beginning, most of the information in the structure is copied from the task_struct of the original parent process.
At this point, the child process's virtual memory page and the parent process's virtual memory page use the same physical page. When a child process occurs or the parent process writes to memory, the system then assigns the physical page to the corresponding virtual page of the child process, the copy-on-write mechanism.

Process address space distribution and executable file distribution

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.