Transcript 記憶體

Chapter 8 記憶體管理策略
(Memory-Management Strategies)
1
CHAPTER 8
8.1
8.2
8.3
8.4
8.5
8.6
記憶體管理策略
背景
置換
連續記憶體配置
分頁
分頁表的結構
分段
2
1 背景(Background)


電腦系統中,記憶體是系統的重心。記憶體本身是一個大型的字組或
位元組陣列,各字組和位元組都各有其位址,CPU是根據程式計數器
的數值到記憶體的位址擷取指令。這些指令可能會造成對於特殊記憶
體位址的額外載入或儲存動作。
藉由管理記憶體的各種不同相關技巧講題開始討論,其中包含了基本
硬體、符號的記憶體位址與真實記憶體的連結,以及邏輯位址和實際
位址間區分的概觀。
3
1.1 基本硬體(Basic Hardware)


主記憶體和建立在處理器內的暫存器是CPU唯一可以直接存取的儲存體。
機器指令使用記憶體位址做為參數,但卻沒有使用磁碟位址做為參數。因
此,任何執行的指令,和任何被這些指令使用的資料必須放在這些直接存
取儲存裝置之內。如果資料不在記憶體內,則必須在CPU操作它們之前移
到記憶體之中。
每一Process都有其個別記憶體空間,非法使用者不得存取此區域記憶體。
每當在User Mode執行的程式企圖要存取其他使用者所分配之記憶體空間
時,監督程式將會發岀中斷(Interrupt)。
4
1.2 位址連結(Address Binding)

編譯時間(Compile time):在編譯
時間時,若已確知程式在記憶
體中的位置, 那麼絕對碼
(absolute code)便可產生。

載入時間(Load time):程式在編
譯時間若不能確定在記憶體中
的位置,那麼編 譯程式就必須
產生重定碼(relocatable code)。

執行時問(Execution time):如果
行程在執行時能夠被由原來的
記憶段落移動至另一段落位置
的話,那麼連結的動作就會被
延遲至執行時間才發生。
5
1.3 邏輯位址空間和實體位址空間
(Logical versus Physical Address Space)

CPU所產生的位址通常稱為邏輯位址(logical address, 也稱為virtual address),
而記憶體單元所看到的位址(也就是載入到記憶體的記憶體位址暫存器
(memory-address register)之數值)通常叫做實體位址(Physical address)。
 Logical and physical addresses are the
same in compile-time and load-time
address-binding schemes; logical (virtual)
and physical addresses differ in executiontime address-binding scheme.
硬體裝置MMU: Memory-Management Unit

The user program deals with logical
addresses; it never sees the real
physical addresses
6
1.4






動態載入(Dynamic Loading)
行程大小受限於實體記憶體大小。要得到較佳之記憶體
空間使用效率,可採行動態載入(dynamic loading)。
主程式儲存在主記憶體並執行,當需要呼叫其它程式時,
首先看看此程式是不是已經存在記憶體內,如果不是,
便呼叫重定位 鏈結載入程式(relocatable linking loading),
將所需要的程式載入主記憶體內,並更新行程位址表的
內容。然後控制就轉移給新的載入程式。
Routine is not loaded until it is called
Better memory-space utilization; unused routine is never loaded
Useful when large amounts of code are needed to handle
infrequently occurring cases
No special support from the operating system is required
implemented through program design
7
1.5 動態鏈結和共用程式庫(Dynamic Linking and Shared
Libraries)
 採用動態鏈結,在程式參用程式庫副程式處做一記號
(stub),該記號為一小段程式來指示如何去找尋適當的記
憶體常駐程式庫副程式,或是如何載入程式庫副程式(如
果不在記憶體中時)。當執行到記號處時,首先檢查所需
要的副程式是否已經存在記憶體中。如果該副程式不在
記憶體中,程式會將它載入到記憶體中。





Linking postponed until execution time
Small piece of code, stub, used to locate the appropriate memoryresident library routine
Operating system needed to check if routine is in processes’
memory address
Dynamic linking is particularly useful for libraries
System also known as shared libraries
8
2.置換(Swapping)

一個行程必須在記憶體被執行,然而一個行程可能會暫時的被置換
(swapped)出記憶體到備份儲存 (backing store),而後再回到記憶
體被繼續執行。
9
10
3.連續記憶體配置
(Contiguous Memory Allocation)
3.1 記憶體映對和保護(Memory Mapping and Protection)

Main memory usually into two partitions:



Resident operating system, usually held in low memory with
interrupt vector
User processes then held in high memory
Relocation registers used to protect user processes from each
other, and from changing operating-system code and data



Base register contains value of smallest physical address
Limit register contains range of logical addresses – each logical
address must be less than the limit register
MMU maps logical address dynamically
11
12

Multiple-partition allocation



Hole – block of available memory; holes of various size
are scattered throughout memory
When a process arrives, it is allocated memory from a hole
large enough to accommodate it
Operating system maintains information about:
a) allocated partitions b) free partitions (hole)
OS
OS
OS
OS
process 5
process 5
process 5
process 5
process 9
process 9
process 8
process 2
process 10
process 2
process 2
process 2
13
3.2 記憶體配置(Memory Allocation)
How to satisfy a request of size n from a list of free holes

最先配合(First fit):配置第一個夠大的區間。搜尋的起始
位置可為第一個可用的區間 或是前一個最先配合搜尋動作
的結束位置,而找到夠大的區間後,立即停止搜尋的動作。

最佳配合(Best fit):在所有容量夠大的區間中,將最小的
一個區間分配給行程。如果 區間串列並未依照容量的大小
順序排序,必須搜尋整個串列才能找到符合這項策略的區間。
最佳配合法會找出最小的剩餘區間。

最差配合(Worst fit):將最大的區間配合給行程。如果區間
串列並未依照容量的大小順序排列,必須搜尋整個串列。最
差配合法會找出最大的剩餘區間,而這些區間的用處可能要
比最佳配合法所找到的剩餘區間來得大。
First-fit and best-fit better than worst-fit in terms of
speed and storage utilization
14
3.3 斷裂(Fragmentation)

記憶體配置的最先配合和最佳配合這二種策略都會遭遇到記
憶體外部片斷(External Fragmentation)的問題。由於行程的載
入和移出,可用的記憶空間將被劃分為許多小區間。當有足
夠的總記憶體得以滿足要求,而這些區間卻是非連續的時候,
此時外部斷裂的現象便發生了,儲存體被分成了許許多多的
小區間。

外部斷裂是否會造成嚴重的問題,完全視記憶空間的總容量
大小和行程的平均大小而定。例如,最先配合的統計分析顯
示縱使具有最佳條件,給予N配置區間由於斷裂現象也將遺
失其它的0.5N,那麼三分之一的記憶體可能沒有利用到,
這就是著名的百分之五十規則 (50-percent rule)。
15




External Fragmentation – total memory space exists to
satisfy a request, but it is not contiguous.
Internal Fragmentation – allocated memory may be slightly
larger than requested memory; this size difference is memory
internal to a partition, but not being used (發生在Paging)
Reduce external fragmentation by compaction (聚集)
 Shuffle memory contents to place all free memory together
in one large block
 Compaction is possible only if relocation is dynamic, and is
done at execution time
另一解決external fragmentation方法是允許Process的Logical
Address Space不連續:分頁(Paging)及分段(Segmentation)
16
4.分頁(Paging)
4.1 基本方法(Basic Method)







Logical address space of a process can be noncontiguous.
Divide physical memory into fixed-sized blocks called
frames (size is power of 2, between 512 and 8,192 bytes)
Divide logical memory into blocks of same size called
pages
Keep track of all free frames
To run a program of size n pages, need to find n free
frames and load program
Set up a page table to translate logical to physical
addresses
Internal fragmentation
17
18
Address Translation Scheme

Address generated by CPU is divided into:


Page number (p) – used as an index into a page table which
contains base address of each page in physical memory
Page offset (d) – combined with base address to define the
physical memory address that is sent to the memory unit
page number

page offset
p
d
m-n
n
For given logical address space 2m and page size 2n
19
0
0
0
1
2
3
p=1
Page: 1
p=2
2
d=3
d=2
3
1
d=2
2
3
4
5
6
m=4
n=2
d=3
7
20
21
Free-Frame List
New Process
22
4.2 硬體的支援(Hardware Support)
Implementation of Page Table





Page table若太大不能放在硬體暫存器,需放在main memory
Page-table base register (PTBR分頁表基底暫存器) points to
the page table
Page-table length register (PTLR分頁表長度暫存器)
indicates size of the page table
分頁表儲放在記憶體,其存取資料或指令須要二次記憶體存取。
One for the page table and one for the data/instruction.
The two memory access problem can be solved by the use of
a special fast-lookup hardware cache called associative
memory or translation look-aside buffers (TLBs轉譯旁觀緩
衝器)
23
Associative Memory

Associative memory – parallel search
Page #
Frame #
Address translation (p, d)


If p is in associative register, get frame # out
Otherwise get frame # from page table in memory
24
Paging Hardware With TLB
記憶體
25
有效存取時間(Effective Access Time)





Associative Lookup =  time unit
Assume memory cycle time is 1 microsecond
Hit ratio – percentage of times that a page number is found in
the associative registers; ratio related to number of associative
registers
Hit ratio = 
Effective Access Time (EAT)
EAT = (1 + )  + (2 + )(1 – )
=2+–
Ex. Hit Ratio =80%, =20 ns, memory access=100 ns,
EAT = (100+20) 0.8 + (200 + 20) (1-0.8) = 140 ns
Hit Ratio =98%, =20 ns, memory access=100 ns,
EAT = (100+20) 0.98 + (200 + 20) (1-0.98) = 122 ns
26
4.3 保護(Protection)

Memory protection
implemented by associating
protection bit with each frame

Valid-invalid bit attached to
each entry in the page table:


“valid” indicates that the
associated page is in the
process’ logical address
space, and is thus a legal
page
“invalid” indicates that the
page is not in the process’
logical address space
27
4.4 共用分頁(Shared Pages)

Shared code



One copy of read-only
(reentrant) code shared among
processes (i.e., text editors,
compilers, window systems).
Shared code must appear in
same location in the logical
address space of all processes
Private code and data


Each process keeps a separate
copy of the code and data
The pages for the private code
and data can appear anywhere
in the logical address space
Ex. 有40位同時使用相同編輯軟體(150K)個人檔案(50K),總共需要(150+50)*40=8M
若將編輯軟體共用,則只需要150+(50*40)=2.15M
28
5.分頁表的結構(Structure of the Page Table)
5.1 階層式的分頁(Hierarchical Paging)
Logical Address為32 bits,其Page Table需要
220 (假設每一Page為4K=212)
若以二層式Paging表示,其Page Table只需要
211 (假設每一Page為4K=212)
29
30
5.2 雜湊分頁表(Hash Page Table)
處理位址空間大於32位元的一種常見方法是使用雜湊分頁表(Hash Page
Table)。其中雜湊值即是虛擬分頁值。雜湊表中每一項包括了雜
湊到相同位置之單元的鏈結串列。
每一個單元由三個欄位所組成:
1. 虛擬分頁的數值
2. 對映分頁們的數值
3. 指向鏈結串列下一單元的指標。
31
演算法依照下列的方式進行:
虛擬位址的虛擬分頁數值被雜湊(hashed)到雜湊表中。虛擬分頁數值
和鏈結串列中第一個單元的欄位(1)做比較。如果兩者相同,相關分
頁欄位 (欄位(2))就被用來形成所需要的實體位址。如果不吻合,鏈
結串列中接下來的單元會被搜尋以找出符合的虛擬分頁數值。
32
5.3 反轉分頁表(Inverted Page Table)

對於實體記憶體中的每一頁(或叫做欄)都有一個進入點。每一項中
包含了在此實體位址頁所對映的虛擬位址,以及擁有該頁的行程。
因此在電腦系統中只有一份分頁表,而此表對於實體位址中的每一
頁都只有一個進入點。
33
6.分段(Segmentation)
Segmentation
6.1 基本方法(Basic Method)
 試想你將會把一個正在寫的
程式看成如何?你會認為它
是由一個主程式和許多次常
式、函數或模式所組成。其
中也有許多不同的資料結構:
物件、陣列、堆疊、變數等
等。這些模式或資料單元都
由其名稱而定。常說的
「堆疊」、「函數程式
庫」、「主程式」,而並
不考慮這些單元在記憶體中
佔用那些位址。
34
Logical View of Segmentation
1
4
1
2
3
4
2
3
user space
physical memory space
35
6.2 硬體(Hardware)
 分段表 (Segment Table)中的每一項都有一個分段基底值 (segment
base)和分段界限值 (segment limit)。分段基底值包含了分段在主記
憶體中的實際開始位址,而分段界限值則設定了分段的長度。
36
37