Memory 3
Memory 3
Background
Main Memory is divided into blocks (Pages / Frames)
Paged Memory
Blocks are Fixed Size called Pages
Segmented Memory
Blocks are Variable Size called Segments
Complete program will reside in Secondary storage / HDD
Entire program rarely used
Error code , exception handlers, large data structures
90-10 rule
“Programs spend 90% of their time in 10% of their code”
Code needs to be in Main Memory to execute
Entire program code not needed in Main Memory at same
time
Background (Cont.)
Solution: Think main memory as cache for disk
Processor
Start Instruction
Generate Address /
VM Address
Instruction ++
No Yes
Page in
Run Instruction
MM ?
Page
Fault
Flow – Diagram (Cont.)
Free Block No
Select a page for Replacement
in MM ?
Yes
Get “Disk Address” of Page
Update Page Table
from FMT
15 page faults
Can vary by reference string: consider 1,2,3,4,1,2,5,1,2,3,4,5
Consider 3 Frames
Consider 4 Frames
FIFO Illustrating Belady’s Anomaly
3 Frames: 9 Page Fault
4 Frames: 10 Page Fault
Adding more frames can cause more page faults!
Belady’s Anomaly (Only in FIFO)
Graph of Page Faults Versus The Number of Frames
Optimal Algorithm
“Replace page that will not be used for longest period of time”
Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
3 frames (3 pages can be in memory at a time per process)
Optimal Algorithm
Page Fault: 9
Pros:
Optimal Performance
Cons:
Can’t read the future
Used for measuring how well other algorithm performs
Least Recently Used (LRU) Algorithm
“Use past knowledge rather than future”
Replace page that has not been used in the most amount of time
Associate “time of last use” with each page
Reference string: 7,0,1,2,0,3,0,4,2,3,0,3,0,3,2,1,2,0,1,7,0,1
3 frames (3 pages can be in memory at a time per process)
Least Recently Used (LRU) Algorithm
Page Fault: 12 faults
better than FIFO but worse than Optimal
Generally good algorithm and frequently used
But how to implement?