Virtual Memory
description
Transcript of Virtual Memory
![Page 1: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/1.jpg)
Virtual Memory
![Page 2: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/2.jpg)
Names, Virtual Addresses & Physical Addresses
SourceProgram
AbsoluteModule
Name SpacePi’s Virtual
Address Space
ExecutableImage
PhysicalAddress Space
t: Virtual Address Space Physical Address Space
![Page 3: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/3.jpg)
Locality• Address space is logically
partitioned– Text, data, stack– Initialization, main, error handle
• Different parts have different reference patterns:
Address Space for pi
Initialization code (used once)Code for 1Code for 2Code for 3Code for error 1Code for error 2Code for error 3Data & stack
30%
20%
35%
15%
<1%
<1%
Execution time
![Page 4: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/4.jpg)
Virtual Memory
Virtual Address Space for piVirtual Address Space for pj
Virtual Address Space for pk
Secondary Memory
• Complete virtual address space is stored in secondary memory
Primary Memory
0
n-1
Physical Address Space
• Fragments of the virtual address space are dynamically loaded into primary memory at any given time
• Each address space is fragmented
![Page 5: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/5.jpg)
Virtual Memory• Every process has code and data locality
– Code tends to execute in a few fragments at one time
– Tend to reference same set of data structures• Dynamically load/unload currently-used
address space fragments as the process executes
• Uses dynamic address relocation/binding– Generalization of base-limit registers– Physical address corresponding to a compile-
time address is not bound until run time
![Page 6: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/6.jpg)
Virtual Memory (cont)• Since binding changes with time, use a
dynamic virtual address map, t
VirtualAddressSpace
t
![Page 7: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/7.jpg)
Address Formation• Translation system creates an address space,
but its address are virtual instead of physical• A virtual address, x:
– Is mapped to physical address y = t(x) if x is loaded at physical address y
– Is mapped to if x is not loaded
• The map, t, changes as the process executes -- it is “time varying”
• t: Virtual Address Physical Address {}
![Page 8: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/8.jpg)
Size of Blocks of Memory
• Virtual memory system transfers “blocks” of the address space to/from primary memory
• Fixed size blocks: System-defined pages are moved back and forth between primary and secondary memory
• Variable size blocks: Programmer-defined segments – corresponding to logical fragments – are the unit of movement
• Paging is the commercially dominant form of virtual memory today
![Page 9: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/9.jpg)
Paging• A page is a fixed size, 2h, block of virtual
addresses• A page frame is a fixed size, 2h, block of
physical memory (the same size as a page)• When a virtual address, x, in page i is
referenced by the CPU– If page i is loaded at page frame j, the virtual
address is relocated to page frame j– If page is not loaded, the OS interrupts the
process and loads the page into a page frame
![Page 10: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/10.jpg)
Addresses• Suppose there are G= 2g2h=2g+h virtual
addresses and H=2j+h physical addresses assigned to a process– Each page/page frame is 2h addresses– There are 2g pages in the virtual address space– 2j page frames are allocated to the process– Rather than map individual addresses
• t maps the 2g pages to the 2j page frames• That is, page_framej = t(pagei)
• Address k in pagei corresponds to address k in page_framej
![Page 11: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/11.jpg)
Page-Based Address Translation• Let N = {d0, d1, … dn-1} be the pages
• Let M = {b0, b1, …, bm-1} be page frames• Virtual address, i, satisfies 0i<G= 2g+h • Physical address, k = U2h+V (0V<G= 2h )
– U is page frame number– V is the line number within the page– t:[0:G-1] <U, V> {}– Since every page is size c=2h
• page number = U = i/c• line number = V = i mod c
![Page 12: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/12.jpg)
Address Translation (cont)
Page # Line #Virtual Address
g bits h bits
tMissing Page
Frame # Line #j bits h bits
Physical Address
MAR
CPU Memory
“page table”
![Page 13: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/13.jpg)
Demand Paging Algorithm1. Page fault occurs2. Process with missing page is interrupted3. Memory manager locates the missing page4. Page frame is unloaded (replacement policy)5. Page is loaded in the vacated page frame6. Page table is updated7. Process is restarted
![Page 14: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/14.jpg)
Modeling Page Behavior• Let = r1, r2, r3, …, ri, … be a page reference
stream– ri is the ith page # referenced by the process– The subscript is the virtual time for the process
• Given a page frame allocation of m, the memory state at time t, St(m), is set of pages loaded– St(m) = St-1(m) Xt - Yt
• Xt is the set of fetched pages at time t
• Yt is the set of replaced pages at time t
![Page 15: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/15.jpg)
More on Demand Paging• If rt was loaded at time t-1, St(m) = St-1(m)
• If rt was not loaded at time t-1 and there were empty page frames– St(m) = St-1(m) {rt}
• If rt was not loaded at time t-1 and there were no empty page frames– St(m) = St-1(m) {rt} - {y}
• The alternative is prefetch paging
![Page 16: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/16.jpg)
Static Allocation, Demand Paging
• Number of page frames is static over the life of the process
• Fetch policy is demand• Since St(m) = St-1(m) {rt} - {y}, the
replacement policy must choose y -- which uniquely identifies the paging policy
![Page 17: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/17.jpg)
Random Replacement• Replaced page, y, is chosen from the m
loaded page frames with probability 1/m
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 7012
213
213
210
310
310
312
012
032
032
062
462
452
752
203
20
2
• No knowledge of not perform well• Easy to implement
13 page faults
![Page 18: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/18.jpg)
Belady’s Optimal Algorithm• Replace page with maximal forward
distance: yt = max xeS t-1(m)FWDt(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 21 0 02 3
![Page 19: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/19.jpg)
Belady’s Optimal Algorithm• Replace page with maximal forward
distance: yt = max xeS t-1(m)FWDt(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 21 0 02 3
FWD4(2) = 1 FWD4(0) = 2FWD4(3) = 3
![Page 20: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/20.jpg)
Belady’s Optimal Algorithm• Replace page with maximal forward
distance: yt = max xeS t-1(m)FWDt(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 21 0 0 02 3 1
FWD4(2) = 1 FWD4(0) = 2FWD4(3) = 3
![Page 21: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/21.jpg)
Belady’s Optimal Algorithm• Replace page with maximal forward
distance: yt = max xeS t-1(m)FWDt(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 21 0 0 0 0 02 3 1 1 1
![Page 22: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/22.jpg)
Belady’s Optimal Algorithm• Replace page with maximal forward
distance: yt = max xeS t-1(m)FWDt(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 2 21 0 0 0 0 0 32 3 1 1 1 1
FWD7(2) = 2 FWD7(0) = 3FWD7(1) = 1
![Page 23: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/23.jpg)
Belady’s Optimal Algorithm• Replace page with maximal forward
distance: yt = max xeS t-1(m)FWDt(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 2 2 2 2 01 0 0 0 0 0 3 3 3 32 3 1 1 1 1 1 1 1
FWD10(2) = FWD10(3) = 2FWD10(1) = 3
![Page 24: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/24.jpg)
Belady’s Optimal Algorithm• Replace page with maximal forward
distance: yt = max xeS t-1(m)FWDt(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 2 2 2 2 0 0 01 0 0 0 0 0 3 3 3 3 3 32 3 1 1 1 1 1 1 1 1 1
FWD13(0) = FWD13(3) = FWD13(1) =
![Page 25: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/25.jpg)
Belady’s Optimal Algorithm• Replace page with maximal forward
distance: yt = max xeS t-1(m)FWDt(x)
Let page reference stream, = 2031203120316457
• Perfect knowledge of perfect performance• Impossible to implement
10 page faults
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 2 2 2 2 0 0 0 0 4 4 41 0 0 0 0 0 3 3 3 3 3 3 6 6 6 72 3 1 1 1 1 1 1 1 1 1 1 1 5 5
![Page 26: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/26.jpg)
Least Recently Used (LRU)• Replace page with maximal forward
distance: yt = max xeS t-1(m)BKWDt(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 21 0 02 3
BKWD4(2) = 3BKWD4(0) = 2BKWD4(3) = 1
![Page 27: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/27.jpg)
Least Recently Used (LRU)• Replace page with maximal forward
distance: yt = max xeS t-1(m)BKWDt(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 11 0 0 02 3 3
BKWD4(2) = 3BKWD4(0) = 2BKWD4(3) = 1
![Page 28: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/28.jpg)
Least Recently Used (LRU)• Replace page with maximal forward
distance: yt = max xeS t-1(m)BKWDt(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 1 11 0 0 0 22 3 3 3
BKWD5(1) = 1BKWD5(0) = 3BKWD5(3) = 2
![Page 29: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/29.jpg)
Least Recently Used (LRU)• Replace page with maximal forward
distance: yt = max xeS t-1(m)BKWDt(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 1 1 11 0 0 0 2 22 3 3 3 0
BKWD6(1) = 2BKWD6(2) = 1BKWD6(3) = 3
![Page 30: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/30.jpg)
Least Recently Used (LRU)• Replace page with maximal forward
distance: yt = max xeS t-1(m)BKWDt(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 1 1 1 3 3 3 0 0 0 6 6 6 71 0 0 0 2 2 2 1 1 1 3 3 3 4 4 42 3 3 3 0 0 0 2 2 2 1 1 1 5 5
![Page 31: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/31.jpg)
Least Recently Used (LRU)• Replace page with maximal forward
distance: yt = max xeS t-1(m)BKWDt(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 2 2 3 2 2 2 2 6 6 6 61 0 0 0 0 0 0 0 0 0 0 0 0 4 4 42 3 3 3 3 3 3 3 3 3 3 3 3 5 53 1 1 1 1 1 1 1 1 1 1 1 1 7
• Backward distance is a good predictor of forward distance -- locality
![Page 32: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/32.jpg)
Least Frequently Used (LFU)• Replace page with minimum use:
yt = min xeS t-1(m)FREQ(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 21 0 02 3
FREQ4(2) = 1FREQ4(0) = 1FREQ4(3) = 1
![Page 33: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/33.jpg)
Least Frequently Used (LFU)• Replace page with minimum use:
yt = min xeS t-1(m)FREQ(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 21 0 0 12 3 3
FREQ4(2) = 1FREQ4(0) = 1FREQ4(3) = 1
![Page 34: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/34.jpg)
Least Frequently Used (LFU)• Replace page with minimum use:
yt = min xeS t-1(m)FREQ(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 21 0 0 1 1 12 3 3 3 0
FREQ6(2) = 2FREQ6(1) = 1FREQ6(3) = 1
![Page 35: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/35.jpg)
Least Frequently Used (LFU)• Replace page with minimum use:
yt = min xeS t-1(m)FREQ(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 2 2 21 0 0 1 1 12 3 3 3 0
FREQ7(2) = ?FREQ7(1) = ?FREQ7(0) = ?
![Page 36: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/36.jpg)
First In First Out (FIFO)• Replace page that has been in memory the
longest: yt = max xeS t-1(m)AGE(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 11 0 0 02 3 3
![Page 37: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/37.jpg)
First In First Out (FIFO)• Replace page that has been in memory the
longest: yt = max xeS t-1(m)AGE(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 21 0 02 3
AGE4(2) = 3AGE4(0) = 2AGE4(3) = 1
![Page 38: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/38.jpg)
First In First Out (FIFO)• Replace page that has been in memory the
longest: yt = max xeS t-1(m)AGE(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 11 0 0 02 3 3
AGE4(2) = 3AGE4(0) = 2AGE4(3) = 1
![Page 39: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/39.jpg)
First In First Out (FIFO)• Replace page that has been in memory the
longest: yt = max xeS t-1(m)AGE(x)
Let page reference stream, = 2031203120316457
Frame 2 0 3 1 2 0 3 1 2 0 3 1 6 4 5 70 2 2 2 11 0 0 02 3 3
AGE5(1) = ?AGE5(0) = ?AGE5(3) = ?
![Page 40: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/40.jpg)
Belady’s Anomaly
• FIFO with m = 3 has 9 faults• FIFO with m = 4 has 10 faults
Let page reference stream, = 012301401234
Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 3 3 3 4 4 4 4 4 41 1 1 1 0 0 0 0 0 2 2 22 2 2 2 1 1 1 1 1 3 3
Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 0 0 0 4 4 4 4 3 31 1 1 1 1 1 1 0 0 0 0 42 2 2 2 2 2 2 1 1 1 13 3 3 3 3 3 3 2 2 2
![Page 41: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/41.jpg)
Stack Algorithms• Some algorithms are well-behaved• Inclusion Property: Pages loaded at time t
with m is also loaded at time t with m+1 Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 31 1 1 12 2 2
Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 01 1 1 12 2 23 3
LRU
![Page 42: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/42.jpg)
Stack Algorithms• Some algorithms are well-behaved• Inclusion Property: Pages loaded at time t
with m is also loaded at time t with m+1 Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 3 31 1 1 1 12 2 2 0
Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 0 01 1 1 1 12 2 2 23 3 3
LRU
![Page 43: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/43.jpg)
Stack Algorithms• Some algorithms are well-behaved• Inclusion Property: Pages loaded at time t
with m is also loaded at time t with m+1 Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 3 3 31 1 1 1 0 02 2 2 2 1
Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 0 0 01 1 1 1 1 12 2 2 2 23 3 3 3
LRU
![Page 44: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/44.jpg)
Stack Algorithms• Some algorithms are well-behaved• Inclusion Property: Pages loaded at time t
with m is also loaded at time t with m+1 Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 3 3 3 41 1 1 1 0 0 02 2 2 2 1 1
Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 0 0 0 01 1 1 1 1 1 12 2 2 2 2 43 3 3 3 3
LRU
![Page 45: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/45.jpg)
Stack Algorithms• Some algorithms are well-behaved• Inclusion Property: Pages loaded at time t
with m is also loaded at time t with m+1 Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 3 3 3 4 4 4 2 2 21 1 1 1 0 0 0 0 0 0 3 32 2 2 2 1 1 1 1 1 1 4
Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 0 0 0 0 0 0 0 0 41 1 1 1 1 1 1 1 1 1 1 12 2 2 2 2 4 4 4 4 3 33 3 3 3 3 3 3 2 2 2
LRU
![Page 46: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/46.jpg)
Stack Algorithms• Some algorithms are well-behaved• Inclusion Property: Pages loaded at time t
with m is also loaded at time t with m+1
FIFO Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 3 3 3 4 4 4 4 4 41 1 1 1 0 0 0 0 0 2 2 22 2 2 2 1 1 1 1 1 3 3
Frame 0 1 2 3 0 1 4 0 1 2 3 40 0 0 0 0 0 0 4 4 4 4 3 31 1 1 1 1 1 1 0 0 0 0 42 2 2 2 2 2 2 1 1 1 13 3 3 3 3 3 3 2 2 2
![Page 47: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/47.jpg)
Implementation• LRU has become preferred algorithm• Difficult to implement
– Must record when each page was referenced– Difficult to do in hardware
• Approximate LRU with a reference bit– Periodically reset– Set for a page when it is referenced
• Dirty bit
![Page 48: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/48.jpg)
Dynamic Paging Algorithms• The amount of physical memory -- the
number of page frames -- varies as the process executes
• How much memory should be allocated?– Fault rate must be “tolerable”– Will change according to the phase of process
• Need to define a placement & replacement policy
• Contemporary models based on working set
![Page 49: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/49.jpg)
Working Set• Intuitively, the working set is the set of pages
in the process’s locality– Somewhat imprecise– Time varying– Given k processes in memory, let mi(t) be # of
pages frames allocated to pi at time t• mi(0) = 0i=1
k mi(t) |primary memory|
• Also have St(mi(t)) = St(mi(t-1)) Xt - Yt
• Or, more simply S(mi(t)) = S(mi(t-1)) Xt - Yt
![Page 50: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/50.jpg)
Placed/Replaced Pages• S(mi(t)) = S(mi(t-1)) Xt - Yt
• For the missing page– Allocate a new page frame– Xt = {rt} in the new page frame
• How should Yt be defined?• Consider a parameter, , called the window size
– Determine BKWDt(y) for every yS(mi(t-1))
– if BKWDt(y) , unload y and deallocate frame
– if BKWDt(y) < do not disturb y
![Page 51: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/51.jpg)
Working Set Principle• Process pi should only be loaded and active
if it can be allocated enough page frames to hold its entire working set
• The size of the working set is estimated using – Unfortunately, a “good” value of depends on
the size of the locality– Empirically this works with a fixed
![Page 52: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/52.jpg)
Example ( = 3)
Frame 0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 70 0
# 1
![Page 53: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/53.jpg)
Example ( = 4)
Frame 0 1 2 3 0 1 2 3 0 1 2 3 4 5 6 70 0
# 1
![Page 54: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/54.jpg)
Implementing the Working Set• Global LRU will behave similarly to a working
set algorithm– Page fault
• Add a page frame to one process• Take away a page frame from another process
– Use LRU implementation idea• Reference bit for every page frame• Cleared periodically, set with each reference• Change allocation of some page frame with a clear
reference bit– Clock algorithms use this technique by searching
for cleared ref bits in a circular fashion
![Page 55: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/55.jpg)
Segmentation• Unit of memory movement is:
– Variably sized– Defined by the programmer
• Two component addresses, <Seg#, offset>• Address translation is more complex than
paging
• t: segments x offsets Physical Address {}• t(i, j) = k
![Page 56: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/56.jpg)
Segment Address Translation• t: segments x offsets physical address {}• t(i, j) = k• : segments segment addresses• t((segName), j) = k• : offset names offset addresses• t((segName), (offsetName)) = k• Read implementation in Section 12.5.2
![Page 57: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/57.jpg)
Address Translation
<segmentName, offsetName>
segment # offset
t
Missing segment
Limit Base P
Limit
Relocation
+
?
To Memory Address Register
![Page 58: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/58.jpg)
Implementation
• Segmentation requires special hardware– Segment descriptor support– Segment base registers (segment, code, stack)– Translation hardware
• Some of translation can be static– No dynamic offset name binding– Limited protection
![Page 59: Virtual Memory](https://reader035.fdocuments.us/reader035/viewer/2022062816/56815172550346895dbfa811/html5/thumbnails/59.jpg)
Multics
• Old, but still state-of-the-art segmentation• Uses linkage segments to support sharing• Uses dynamic offset name binding• Requires sophisticated memory
management unit• See pp 368-371