Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8 記憶體管理策略 (Memory-Management Strategies)

Similar presentations


Presentation on theme: "Chapter 8 記憶體管理策略 (Memory-Management Strategies)"— Presentation transcript:

1 Chapter 8 記憶體管理策略 (Memory-Management Strategies)

2 8.1 背景 8.2 置換 8.3 連續記憶體配置 8.4 分頁 8.5 分頁表的結構 8.6 分段
CHAPTER 8 記憶體管理策略 8.1 背景 8.2 置換 8.3 連續記憶體配置 8.4 分頁 8.5 分頁表的結構 8.6 分段

3 1 背景(Background) 電腦系統中,記憶體是系統的重心。記憶體本身是一個大型的字組或位元組陣列,各字組和位元組都各有其位址,CPU是根據程式計數器的數值到記憶體的位址擷取指令。這些指令可能會造成對於特殊記憶體位址的額外載入或儲存動作。 藉由管理記憶體的各種不同相關技巧講題開始討論,其中包含了基本硬體、符號的記憶體位址與真實記憶體的連結,以及邏輯位址和實際位址間區分的概觀。

4 1.1 基本硬體(Basic Hardware) 主記憶體和建立在處理器內的暫存器是CPU唯一可以直接存取的儲存體。機器指令使用記憶體位址做為參數,但卻沒有使用磁碟位址做為參數。因此,任何執行的指令,和任何被這些指令使用的資料必須放在這些直接存取儲存裝置之內。如果資料不在記憶體內,則必須在CPU操作它們之前移到記憶體之中。 每一Process都有其個別記憶體空間,非法使用者不得存取此區域記憶體。每當在User Mode執行的程式企圖要存取其他使用者所分配之記憶體空間時,監督程式將會發岀中斷(Interrupt)。

5 1.2 位址連結(Address Binding)
編譯時間(Compile time):在編譯時間時,若已確知程式在記憶體中的位置, 那麼絕對碼(absolute code)便可產生。 載入時間(Load time):程式在編譯時間若不能確定在記憶體中的位置,那麼編 譯程式就必須產生重定碼(relocatable code)。 執行時問(Execution time):如果行程在執行時能夠被由原來的記憶段落移動至另一段落位置的話,那麼連結的動作就會被延遲至執行時間才發生。

6 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 execution-time address-binding scheme. 硬體裝置MMU: Memory-Management Unit The user program deals with logical addresses; it never sees the real physical addresses

7 1.4 動態載入(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

8 1.5 動態鏈結和共用程式庫(Dynamic Linking and Shared Libraries)
採用動態鏈結,在程式參用程式庫副程式處做一記號 (stub),該記號為一小段程式來指示如何去找尋適當的記憶體常駐程式庫副程式,或是如何載入程式庫副程式(如果不在記憶體中時)。當執行到記號處時,首先檢查所需要的副程式是否已經存在記憶體中。如果該副程式不在記憶體中,程式會將它載入到記憶體中。 Linking postponed until execution time Small piece of code, stub, used to locate the appropriate memory-resident 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

9 2.置換(Swapping) 一個行程必須在記憶體被執行,然而一個行程可能會暫時的被置換 (swapped)出記憶體到備份儲存 (backing store),而後再回到記憶體被繼續執行。

10

11 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

12

13 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 process 5 process 8 process 2 process 9 process 10 13

14 3.2 記憶體配置(Memory Allocation)
最先配合(First fit):配置第一個夠大的區間。搜尋的起始位置可為第一個可用的區間 或是前一個最先配合搜尋動作的結束位置,而找到夠大的區間後,立即停止搜尋的動作。 最佳配合(Best fit):在所有容量夠大的區間中,將最小的一個區間分配給行程。如果 區間串列並未依照容量的大小順序排序,必須搜尋整個串列才能找到符合這項策略的區間。最佳配合法會找出最小的剩餘區間。 最差配合(Worst fit):將最大的區間配合給行程。如果區間串列並未依照容量的大小順序排列,必須搜尋整個串列。最差配合法會找出最大的剩餘區間,而這些區間的用處可能要比最佳配合法所找到的剩餘區間來得大。 How to satisfy a request of size n from a list of free holes First-fit and best-fit better than worst-fit in terms of speed and storage utilization

15 3.3 斷裂(Fragmentation) 記憶體配置的最先配合和最佳配合這二種策略都會遭遇到記憶體外部片斷(External Fragmentation)的問題。由於行程的載入和移出,可用的記憶空間將被劃分為許多小區間。當有足夠的總記憶體得以滿足要求,而這些區間卻是非連續的時候,此時外部斷裂的現象便發生了,儲存體被分成了許許多多的小區間。 外部斷裂是否會造成嚴重的問題,完全視記憶空間的總容量大小和行程的平均大小而定。例如,最先配合的統計分析顯示縱使具有最佳條件,給予N配置區間由於斷裂現象也將遺失其它的0.5N,那麼三分之一的記憶體可能沒有利用到,這就是著名的百分之五十規則 (50-percent rule)。

16 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)

17 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

19 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 For given logical address space 2m and page size 2n page number page offset p d m - n n 19

20 1 2 3 1 2 3 4 5 6 7 p=1 1 2 3 Page: d=3 d=2 p=2 d=2 d=3 m=4 n=2

21

22 Free-Frame List New Process

23 Implementation of Page Table
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

24 Associative Memory Associative memory – parallel search
Address translation (p, d) If p is in associative register, get frame # out Otherwise get frame # from page table in memory Page # Frame # 24

25 Paging Hardware With TLB
記憶體 25

26 有效存取時間(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) ( ) (1-0.8) = 140 ns Hit Ratio =98%, =20 ns, memory access=100 ns, EAT = (100+20) ( ) (1-0.98) = 122 ns 26

27 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

28 4.4 共用分頁(Shared Pages) Shared code Private code and data
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

29 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) Logical Address為32 bits,其Page Table需要 220 (假設每一Page為4K=212)

30

31 5.2 雜湊分頁表(Hash Page Table)
每一個單元由三個欄位所組成: 1. 虛擬分頁的數值 2. 對映分頁們的數值 3. 指向鏈結串列下一單元的指標。

32 演算法依照下列的方式進行: 虛擬位址的虛擬分頁數值被雜湊(hashed)到雜湊表中。虛擬分頁數值和鏈結串列中第一個單元的欄位(1)做比較。如果兩者相同,相關分頁欄位 (欄位(2))就被用來形成所需要的實體位址。如果不吻合,鏈結串列中接下來的單元會被搜尋以找出符合的虛擬分頁數值。

33 5.3 反轉分頁表(Inverted Page Table)
對於實體記憶體中的每一頁(或叫做欄)都有一個進入點。每一項中包含了在此實體位址頁所對映的虛擬位址,以及擁有該頁的行程。因此在電腦系統中只有一份分頁表,而此表對於實體位址中的每一頁都只有一個進入點。

34 6.分段(Segmentation) 6.1 基本方法(Basic Method) Segmentation
試想你將會把一個正在寫的程式看成如何?你會認為它是由一個主程式和許多次常式、函數或模式所組成。其中也有許多不同的資料結構:物件、陣列、堆疊、變數等等。這些模式或資料單元都由其名稱而定。常說的 「堆疊」、「函數程式庫」、「主程式」,而並不考慮這些單元在記憶體中佔用那些位址。

35 Logical View of Segmentation
1 4 2 3 1 2 3 4 user space physical memory space 35

36 6.2 硬體(Hardware) 分段表 (Segment Table)中的每一項都有一個分段基底值 (segment base)和分段界限值 (segment limit)。分段基底值包含了分段在主記憶體中的實際開始位址,而分段界限值則設定了分段的長度。

37


Download ppt "Chapter 8 記憶體管理策略 (Memory-Management Strategies)"

Similar presentations


Ads by Google