[Turn] Linux Virtual Memory

https://www.cnblogs.com/dyllove98/archive/2013/06/12/3132940.html

The first layer appreciated

1. Each process has its own separate 4G memory space, memory space of each process has a similar structure

2. When a new process is created, it will establish its own memory space, this process of data, code, etc. copied from disk to its own process space, what data where, by the process control task_struct record in the table, task_struct recording in a list, recorded in the memory space allocation, which addresses the data, no data which addresses, which read, which can be written, this list can be recorded by

3. Each process has allocated memory space are mapped to the corresponding disk space


 

problem:

Obviously not so much computer memory (if the n process would require n * 4G) Memory

Establish a process to copy the program files on the disk to make the process corresponding to the memory for multiple processes corresponding to a program in this case, memory is wasted!

The second layer appreciated

1. 4G memory space per process virtual memory space only, each time an address access memory space, we need to address is translated into actual physical memory address

2. All processes share the same physical memory, each process only needs to present his own virtual memory space is mapped and stored on the physical memory.

3. The process is to know the data on which memory address in physical memory, which is not, where else in physical memory, page table need to record

Each table entry of the page table 4. The two parts, the first part of this page is recorded in the physical memory, the physical memory address of a second portion of the recording page (if any)

5. When the process accesses a virtual address, see page table if the corresponding data is not found in physical memory, the missing page abnormalities

6. Data page fault exception handling process, the process is to require copies from disk into physical memory, if the memory is full, there is no empty place, then find a cover page, of course, if the page had been covered modified, this page requires written back to disk

 

to sum up:

advantage:

1.既然每个进程的内存空间都是一致而且固定的,所以链接器在链接可执行文件时,可以设定内存地址,而不用去管这些数据最终实际的内存地址,这是有独立内存空间的好处

2.当不同的进程使用同样的代码时,比如库文件中的代码,物理内存中可以只存储一份这样的代码,不同的进程只需要把自己的虚拟内存映射过去就可以了,节省内存

3.在程序需要分配连续的内存空间的时候,只需要在虚拟内存空间分配连续空间,而不需要实际物理内存的连续空间,可以利用碎片。

 

另外,事实上,在每个进程创建加载时,内核只是为进程“创建”了虚拟内存的布局,具体就是初始化进程控制表中内存相关的链表,实际上并不立即就把虚拟内存对应位置的程序数据和代码(比如.text .data段)拷贝到物理内存中,只是建立好虚拟内存和磁盘文件之间的映射就好(叫做存储器映射),等到运行到对应的程序时,才会通过缺页异常,来拷贝数据。还有进程运行过程中,要动态分配内存,比如malloc时,也只是分配了虚拟内存,即为这块虚拟内存对应的页表项做相应设置,当进程真正访问到此数据时,才引发缺页异常。

 

补充理解:

虚拟存储器涉及三个概念: 虚拟存储空间,磁盘空间,内存空间


可以认为虚拟空间都被映射到了磁盘空间中,(事实上也是按需要映射到磁盘空间上,通过mmap),并且由页表记录映射位置,当访问到某个地址的时候,通过页表中的有效位,可以得知此数据是否在内存中,如果不是,则通过缺页异常,将磁盘对应的数据拷贝到内存中,如果没有空闲内存,则选择牺牲页面,替换其他页面。

 

mmap是用来建立从虚拟空间到磁盘空间的映射的,可以将一个虚拟空间地址映射到一个磁盘文件上,当不设置这个地址时,则由系统自动设置,函数返回对应的内存地址(虚拟地址),当访问这个地址的时候,就需要把磁盘上的内容拷贝到内存了,然后就可以读或者写,最后通过manmap可以将内存上的数据换回到磁盘,也就是解除虚拟空间和内存空间的映射,这也是一种读写磁盘文件的方法,也是一种进程共享数据的方法 共享内存

Guess you like

Origin www.cnblogs.com/qxxnxxFight/p/11127740.html