Linux内存page,Linux虚拟内存管理 - Page Table的作用
虛擬內存的作用:
1.
擴展實際有限的物理內存,當然這種擴展是虛擬的,比如物理內存512M,對于一個需要1G空間的進程來說,照樣可以運行。這增加了操作系統(tǒng)是應用范圍。
2.
使得進程中的數據空間增大,增大到多少與硬件有關,對于一個32位的芯片,進程中的數據空間可以為4G[2^32],對于64位的芯片則支持2^64大小
的空間。這一點使得進程自身可操作的空間大大增加。
通俗來講,虛擬內存的管理的核心是解決如何在小的物理內存中運行更大程序的問題。
在Linux中,解決這個問題的關鍵是一個叫做page
table[PT頁面轉換表]的結構。Linux把物理內存分為了固定統(tǒng)一大小的塊,稱為page[頁],一般為4KB,并且每個頁都有一個編號
[page frame
number]。這樣一個512M大小的內存將包括128K個頁。這種方式稱為paging,使得操作系統(tǒng)對內存的管理更方便。page
table的作用就是將進程操作的地址[虛擬地址]轉換成物理地址。
其原理很簡單,如下:
用一個32位芯片的系統(tǒng)為例[64位同理],運行的每個進程的可操作數據空間為2^32,即2^20個頁,設其物理內存為512M,則物理頁有
2^17個,現在就說明如何將2^20個頁放入2^17個頁中運行。我們把進程操作的地址分為兩部分,第一部分為地址的高20位,第二部分為后12位,這
樣很容易將第一部分理解為虛擬頁標號,第二部分理解為在頁中的offset。那么現在我們只需將虛擬頁標號對應到物理頁號即可,這個對應就是page
table的工作,在這個例子中page
table包括了2^20個記錄,每個記錄有兩部分組成:20位的虛擬標號和17位的物理標號,這樣CPU用進程地址的第一部分作為索引找到對應的17位
物理標號,與地址的第二部分一起便組成一個29位的地址,這個地址就是要找的物理地址。因為物理頁少于虛擬頁,所以page
table中的有些記錄的后17位是空的或無效的。
利用這個方法,使得運行的進程無需知道自己操作的地址是虛擬的,和運行在一個真實的大物理內存中效果是一樣的。
可以看出,在進程的運行過程中,page table必須一直保存在內存中,在上面的例子中,我們把虛擬地址分了2層,page
table有2^20個記錄,需要1M左右的空間,為了節(jié)省空間我們可以將地址分為3層,第一層10位,需要1K左右的空間,第二層10位,需要1K左右
的空間,第三層12位,這樣在一段時間內只需要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.
總結
以上是生活随笔為你收集整理的Linux内存page,Linux虚拟内存管理 - Page Table的作用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux终端lex程序运行,lex的简
- 下一篇: 吃醋的句子说说心情 暗示自己吃醋的短句2