Linux内存page,Linux虚拟内存管理 - Page Table的作用
虛擬內(nèi)存的作用:
1.
擴展實際有限的物理內(nèi)存,當(dāng)然這種擴展是虛擬的,比如物理內(nèi)存512M,對于一個需要1G空間的進(jìn)程來說,照樣可以運行。這增加了操作系統(tǒng)是應(yīng)用范圍。
2.
使得進(jìn)程中的數(shù)據(jù)空間增大,增大到多少與硬件有關(guān),對于一個32位的芯片,進(jìn)程中的數(shù)據(jù)空間可以為4G[2^32],對于64位的芯片則支持2^64大小
的空間。這一點使得進(jìn)程自身可操作的空間大大增加。
通俗來講,虛擬內(nèi)存的管理的核心是解決如何在小的物理內(nèi)存中運行更大程序的問題。
在Linux中,解決這個問題的關(guān)鍵是一個叫做page
table[PT頁面轉(zhuǎn)換表]的結(jié)構(gòu)。Linux把物理內(nèi)存分為了固定統(tǒng)一大小的塊,稱為page[頁],一般為4KB,并且每個頁都有一個編號
[page frame
number]。這樣一個512M大小的內(nèi)存將包括128K個頁。這種方式稱為paging,使得操作系統(tǒng)對內(nèi)存的管理更方便。page
table的作用就是將進(jìn)程操作的地址[虛擬地址]轉(zhuǎn)換成物理地址。
其原理很簡單,如下:
用一個32位芯片的系統(tǒng)為例[64位同理],運行的每個進(jìn)程的可操作數(shù)據(jù)空間為2^32,即2^20個頁,設(shè)其物理內(nèi)存為512M,則物理頁有
2^17個,現(xiàn)在就說明如何將2^20個頁放入2^17個頁中運行。我們把進(jìn)程操作的地址分為兩部分,第一部分為地址的高20位,第二部分為后12位,這
樣很容易將第一部分理解為虛擬頁標(biāo)號,第二部分理解為在頁中的offset。那么現(xiàn)在我們只需將虛擬頁標(biāo)號對應(yīng)到物理頁號即可,這個對應(yīng)就是page
table的工作,在這個例子中page
table包括了2^20個記錄,每個記錄有兩部分組成:20位的虛擬標(biāo)號和17位的物理標(biāo)號,這樣CPU用進(jìn)程地址的第一部分作為索引找到對應(yīng)的17位
物理標(biāo)號,與地址的第二部分一起便組成一個29位的地址,這個地址就是要找的物理地址。因為物理頁少于虛擬頁,所以page
table中的有些記錄的后17位是空的或無效的。
利用這個方法,使得運行的進(jìn)程無需知道自己操作的地址是虛擬的,和運行在一個真實的大物理內(nèi)存中效果是一樣的。
可以看出,在進(jìn)程的運行過程中,page table必須一直保存在內(nèi)存中,在上面的例子中,我們把虛擬地址分了2層,page
table有2^20個記錄,需要1M左右的空間,為了節(jié)省空間我們可以將地址分為3層,第一層10位,需要1K左右的空間,第二層10位,需要1K左右
的空間,第三層12位,這樣在一段時間內(nèi)只需要2K的空間保存page
table。實際上,Alpha的芯片采用的就是這種3層的分法,Intel的芯片采用的2層的分法。
Figure: Three Level Page Tables
Linux assumes that there are three levels of page tables.
Each Page Table contains the page frame number of the next level of Page Table. The
Figure above shows how a virtual address can
be broken into a number of fields; each field providing an offset into a
particular Page Table.
To translate a virtual address into a physical one, the processor must take the
contents of each level field, convert it into an offset into the physical
page containing the Page Table and read the page frame number of the next level of Page
Table.
This is repeated three times until the page frame number of the physical page
containing the virtual address is found.
Now the final field in the virtual address, the byte offset, is used to
find the data inside the page.
Each platform that Linux runs on must provide translation macros that allow
the kernel to traverse the page tables for a particular process.
This way, the kernel does not need to know the format of the page table entries or
how they are arranged.
This is so successful that Linux uses the same page table manipulation code for
the Alpha processor, which has three levels of page tables, and for Intel x86 processors,
which have two levels of page tables.
總結(jié)
以上是生活随笔為你收集整理的Linux内存page,Linux虚拟内存管理 - Page Table的作用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux终端lex程序运行,lex的简
- 下一篇: linux安装无法添加挂载,linux-