Virtual address space Virtual Address Space

Read "Computer System", encountered Virtual Address Space concept, that is, the virtual address space, previously seen, but know these are not the why, therefore we do some detailed conceptual analysis: the origin and development and the status quo, since what role, why should invent it.

Reference links:

Talking about the process address space and virtual storage space

Virtual Address Space background should start with the development of memory allocation mechanism, it is a derivative concept, to serve the memory allocation mechanism.

The following is described from three stages:

  1. The original memory allocation mechanism
  2. Segmented memory allocation
  3. Paged memory allocation mechanism

1. The original memory allocation

Original computer, runs a program, the program will load all memory and run directly on the memory.

At this stage, the memory address of the program is to access the actual physical memory address .

When the system is running multiple programs at the same time, how to allocate memory for these programs?

For example:

Total Memory Size 128M, simultaneously running two programs A and B, A need 10M, B need to 110M.

Raw memory allocation strategy:

First memory prior to 10M points A, then followed by the remaining sub-118M to 110M B. As shown below:

limitation

  1. Process address space not isolated

Because AB is the direct access to physical memory, the memory data exist to modify each other's risk, violates the principle of independence of the process.

  1. Address of the program running is uncertain.

When the remaining space in memory to meet the requirements of the program C, the operating system will be randomly allocated in the remaining space in a contiguous space for the size of the 20M using procedure C, as are assigned randomly, so the address of the program running is uncertain .

Note: This is with respect to the following want of Segmentation segmentation techniques, namely virtual memory space for the program, the start address is 0, rather than random, the programmer implements a memory mapping and transparent way right.

2. segmentation memory allocation

Segmented purpose: to solve the address space to isolate the problem.

Principle: Increase the middle layer (virtual address space).

Since direct access to physical address risk, do not let it direct access to a middleman to control that program do not have direct access to physical memory, accessible only to convey the demands by the middleman, and this way convey to comply with the rules of the system, avoid the problem of crosstalk memory, indirect played address space isolation.

Now, the program accesses the memory address is not the actual physical address, but the virtual address, and then by the system to map the virtual addresses to physical memory addresses, memory access is completed.

When a process is created, the system assigns 4G virtual address space for it.

Why is 4G?

Since the 32-bit system, a pointer length of 4 bytes, and a 4-byte pointer addressing capability is:

From
0000 0000 0000 0000 0000 0000 0000 0000
to
1111 1111 1111 1111 1111 1111 1111 1111
复制代码

16 is replaced by hexadecimal 0x00000000 ~ 0xFFFFFFFF, i.e. 0xFFFFFFFF 4G maximum size capacity.

What is memory addressing, what is the addressability?

Addressing: Addressing, to find, locate an address.

Addressability

Pointer length is 4 bytes, we know the content of the pointer memory address, the 4 bytes is stored inside the N possible address. N How much is it? This 4-byte size of 32 bits of memory address space How many possible can say then?

32 bit together can represent the possibility of the number 0101, the number of addresses can be represented. 32 kinds of potential energy possibilities represent up to 4G of 0101, namely 4G address.

This means that I can access a memory address pointer, this can only be confined to 4G different addresses, address ranges beyond that 4G is too much for me and place.

For me, that is outside the scope of my abilities only took my hand like a key four-room, four that I can only open the door of this room corresponding to this level the rest of the other four houses . The corresponding 32-bit system memory 8G, I can only use up to 4G of them. The remaining 4G For me this pointer, it did not have the key to open the door, the visit does not arise.

Here is my 4G pointer addressing capability.

By mapping mechanism, when a program accesses an address on the virtual address space, the equivalent of a visit to a real memory that a corresponding physical address.

What is the principle of this mechanism is mapped? Principle will analyze the segmentation techniques.

Segmentation segments (memory block technology)

Thinking: between the virtual address space and physical address space to do one mapping. System to ensure that different process address space is mapped onto the physical address space in different regions, the physical address space are separated from one another, so that the address space is realized between the isolation process.

example:

A、B 两个进程

A需要10M,对应的虚拟地址空间分布是: 0x00000000 到 0x00A00000 

B需要100M,对应的虚拟内存地址空间分布是:0x00000000 到 0x06400000。

按照 Segmentation 的映射方法,进程 A和B 在物理内存上映射区域分别为:

进程 A:0x00100000 到 0x00B00000(分配一块 10M 大小的内存片段)

进程 B:0x00C00000 到 0x07000000(分配一块 100M 大小的内存片段)

复制代码

Accordingly, processes A and B are mapped to different memory areas, are not mutually overlapped with each other, to achieve isolation address.

From a programmer's point of view, the process address space at 0x00000000 A is distributed to 0x00A00000, which is programmer-oriented virtual address space, we only need to face the virtual address space can be programmed, behind the real physical address mapping to the system to handle, it is transparent to the programmer.

Technology limitations segment

Segmentation segment mapping ways to solve the original memory allocation memory space isolation and program memory address issues of uncertainty, but the efficiency is still relatively low.

Segmentation segment mapping method, the memory is switched every time the entire process, the particle size is too large, resulting in a lot of disk access operations, poor performance.

In fact, the program has run local characteristics, in a period of time, most programs just a small part of the data used in the current program only, most of the remaining data in a period of time will not be used (dog in the manger, pit waste precious memory bit).

Based on this, computer scientists thought a smaller memory size and segmentation mapping method - Paging (Paging) .

3. Paging Paging

Page: The address space is divided into a plurality of page, the size of each page is determined by the CPU. Currently PC are choosing to use a 4KB page size. Compared to the way the process is divided according to size memory size, pagination is much smaller.

The difference between the segmentation and paging

Fragment: each time the program is run always load the current program to all the data memory block.

Page: running, with a page where you allocate memory for which page, the page is useless to temporarily remain in the hard disk, rather than a one-time load to the main memory.

When used in the page, the paging process is as follows:

-> 1. the physical address space allocated to the page memory

-> 2. Create the page in the virtual address space

-> 3. Mapping: building bridges, the physical address and virtual address space docking

Loading process executable file

The following describes the loading process by an executable file, the specific method will be described paging mechanism.

An executable file, essentially a collection of compiled data and instructions, it will be divided into many page. It is in the implementation process, the unit is loaded into memory page.

When a program is executed, the system first process of creating a 4G virtual address space for the program.

Virtual address space is just an intermediate layer, essentially a data structure, to create a 4G virtual address space is actually not really create space, just create a mapping mechanism that required only the amount of data structure, this data structure is the project page ( page index) and page table (page array).

Once you've created the virtual address space needed for data in this layer structure, the newly created process starts reading the first page of the executable file.

On the first page of the executable file contains information such as the file header and segment table, the process according to the header information, the executable file in all segments, page-one mapping to the virtual address space in response.

At this point, the real data and instructions executable files (headers relative terms) has not been loaded into memory, the system according to the knowledge of the executable file header information, pre-built executable files and processes virtual address space of the page mapping relationship only.

In fact, ahead of the first built catalog and future correspondence between the actual content of it, like I am a teacher, a class of students advance to assign a key to a person, but to let you into the room. Only when I read your name, you can hold the key to enter your room.

When a virtual address to access the CPU used in the program, change the address and find no physical address associated, PUC page to the virtual address belongs as an empty page - Page Fault (CPU know that the system did not give the page allocation program memory), then the CPU will return control to the system.

Then assign the operating system for the program page in the physical address space of a page, then this page of physical memory and virtual address space mapped page set up, and finally the control back to CPU, CPU again from the wrong position on the page just happened Begin execution.

We can understand these as a paging communication mechanism - with the continued execution of the program, page fault will continue to occur, the operating system will assign corresponding physical memory pag for the process to meet the required process execution memory.

The core idea of ​​the method of pagination

When the program execution to the page N, to allocate a memory page for this page, then add to map virtual memory address space of the page. Through this process map, you can access to the memory of the corresponding page.

The logical address (Logical Address)

In C, the value of the variable itself can be read pointer (& operation), the value is a logical address, which is relative to the current process data segment address, and not the absolute physical address.

Intel only in real mode, only logical addresses and physical addresses are equal.

Programmers only need to deal with and logical addresses, segmentation and paging mechanism for you is completely transparent.

It can be understood as the logical address of the virtual address.

Reproduced in: https: //juejin.im/post/5d0ae862f265da1b897ad552

Guess you like

Origin blog.csdn.net/weixin_34337381/article/details/93183002