Linux process address space and virtual memory

Source: Internet
Author: User

http://blog.csdn.net/xu3737284/article/details/12710217

The address space of a process in a Linux operating system on a 32-bit machine is 4G, where 0-3g is the user space and 3g-4g is the kernel space. The address space of the process exists in virtual memory. Virtual memory cannot be disabled.

Process address space

The process address space is divided into kernel space and user space

Because each process can enter the kernel through system calls, the Linux kernel is shared by all processes within the system. Thus, from a specific process perspective, each process can have a virtual space of 4G bytes.

A. Body segment. This is the part of the machine instruction that is executed by the CPU. Normally, body segments are shareable, so even frequently executed programs (such as text-editing programs, C-compilers, shells, and so on) require only one copy in memory, and the body segment is often read-only to prevent the program from modifying its own instructions due to an accident.

B. Initialize the data segment. This segment is often referred to as a data segment, which contains variables that need to be assigned an initial value in the program. For example, a description outside any function in a C program:

int maxcount = 99; (global variable)

C. Non-initialized data segments. This segment is often referred to as a BSS segment, which is derived from an operation of the earlier assembler, which means "block started by symbol", which initializes this segment to 0 before the program begins execution. Description outside the function:

Long sum[1000];

Causes this variable to be stored in a non-initialized data segment.

D. Stacks. The automatic variables and the information you need to save each time the function is called are stored in this section. Each time the function is called, its return address, and the caller's environment information (for example, some machine registers) are stored in the stack. The newly called function then allocates storage space for its automatic and temporary variables on the stack. By using the stack in this way, the C function can be called recursively.

E. Heap. Dynamic storage allocations are typically performed in the heap. Due to the historically established conventions, the heap is located between the top of the non-initialized data segment and the bottom of the stack.

Since we see the stack space is growing, the heap space is growing from the bottom, they will meet it? Generally not, because they are spaced very large

kernel State and user state
When a task (process) executes a system call and is executed in the kernel code, we say that the process is in the kernel run state (or simply the kernel state). At this point the processor is executed in the highest privileged (level 0) kernel code. When the process is in the kernel state, the kernel code that executes will cause the kernel stack of the current process. Each process has its own kernel stack. When the process executes the user's own code, it is said to be in the user's running state (user state). That is, the processor is running in the least privileged (level 3) user code. When the user program is being executed and the program is interrupted abruptly, the user program can also be symbolically referred to as the kernel state in the process. Because the interrupt handler will use the kernel stack of the current process. This is somewhat similar to the state of a process that is in a kernel state.

Kernel space is the kernel code and data, while the process of user space is stored in the user program code and data. Whether it's kernel space or user space.

In the Linux operating system, each process is described by a TASK_STRUCT structure, each process's address space is described by a mm_struct, each section of the C language space is represented by vm_area_struct, their relationship is as follows:

When running a program, the operating system needs to create a process, what does the process and program do?

When a program is executed, the contents of the program must be placed into the virtual address space of the process, as is the case with the shared library of executable programs. An executable program is not actually read into physical memory, but only in virtual memory that is linked to the process.

When an executable program maps to a process virtual address space, a set of VM_AREA_STRUCT data structures is generated. Each VM_AREA_STRUCT data structure represents part of an executable impression, an executable code, or initialized data, and uninitialized data.

The Linux operating system maps and reads executable files through Sys_exec, with the following steps:

1. Create a set of vm_area_struct

2. Delimit a virtual user space and save its starting end address (set in the Elf section) to Vm_start and Vm_end.

3. Save the disk file handle in Vm_file

4. Save the corresponding segment's offset value in the disk file (set in Elf section) in Vm_pgoff;

5. Save the disk operation function of the disk file in Vm_ops

Note: There is no corresponding page catalog table entry to create the page table, and there is no Set page table entry.

Suppose now that there is a command in the program to read something between the vm_start--vm_end above

For example: mov [0x08000011],%eax, then the following sequence will be executed:

1.CPU based on CR3 (CURRENT->PGD) to find 0x08000011 address corresponding pgd[i], because the pgd[i] content remains initialized state is 0, resulting in a CPU exception.

2.do_page_fault is called, in this function, for Pgd[i], allocates a page table in memory and makes the table entry point to it, as shown in:

Note: Here I is 0x08000011 high 10 bits, J for its middle 10 bits, at this time the PT table entries are all 0 (Pte[j] also 0);

3. Assign a real physical memory page for PTE[J], and invoke Vm_ops to filemap_nopage the disk file according to Vm_file, Vm_pgoff, and vm_ in Vm_area_struct The contents of the Pgoff offset are read into the physical page, as shown in:

①. Allocate physical memory pages;

Ii. Read content from a disk file to a physical memory page

From the above we can know that during process creation, the program content is mapped to the virtual memory space of the process, in order to allow a large program to run in a limited amount of physical memory space, we can put the beginning of this program to load into the physical memory space to run, because the operating system handles the virtual address of the process, If the physical address is found to be in a virtual-to-Physical address conversion project, a page fault (nopage) will occur at this time, and then the operating system will load data that is not yet loaded into memory on the disk into physical memory, and the corresponding process pages table is updated. Perhaps you ask, if the physical memory is full at this time, how will the operating system handle it?

Let's look at how the Linux operating system is handled:

If a process wants to load a virtual page into physical memory without available free physical pages, the operating system must retire other pages in physical memory to make room for the page.

In the Linux operating system, the physical page is described as follows:

struct MEM_MAP

{

1. This page uses a count, which counts more than 1 when the page is shared by many processes.

2.age describes the age of this page and is used to determine if it is a good candidate for elimination or exchange.

3.map_nr describing the page frame number of the physical page

}

If a page that is retired from physical memory comes from an image or data file and has not been written yet, the page does not have to be saved and it can be discarded. If a process needs the page, it can retrieve the memory from the image or data file.

However, if the page has been modified, the operating system must retain the contents of the page for later access. This kind of page is called "Dirty (Dirty) page", when it is removed from memory, it will be saved in a special file called swap file.

Relative to the speed of the processor and physical memory, access to the swap file takes a long time, and the operating system must spend it on writing the page to disk and retrieving the memory when it is reused.

If the algorithm used to determine which page is being eliminated or swapped is not efficient enough, a situation called "jitter" may occur. In this case, the page is always written to disk and read back, and the operating system is too busy to do the real work.

Linux uses the "least recently Used (Least recently used, LRU)" page scheduling technique to choose which page to use fairly to remove from the system. Each page in this design system has an "age", and the age varies with the page being accessed. The more pages are accessed, the younger they are, and the less they are accessed, the older they become. The old page is the best candidate page for swapping.

Linux process address space and virtual memory

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.