2. • Three memory problems:
– Not enough RAM
– Holes in our address
space
– Programs writing over
each other
• What is virtual memory?
– Indirection
– How does it solve the
problems
– Page Tables and
Translation
• Implementing Virtual
Memory
– Why do we store the
page tables?
Contents
• Paging
– Why we do paging ?
– Page Faults
– Demand Paging
• Page Replacement
and its Algorithms
– What is Page
Replacement?
– FIFO Replacement
Algo
– Optimal
Replacement Algo
– LRU Algorithm
4. • Architecture: 32-bit address space
• Programs: any byte in 32-bit address space
• What if you don’t have 4GB of memory?
#1: What if we don’t have enough memory?
32-bit program
address space
(4GB)
30-bit RAM
address space
(1GB)
Address 0x00000000
Address 0xFFFFFFFF
…to… ??
Crash if we try to access
more than 1GB
Q: How much memory can you access with 32-
bit address?
A. 230
bytes = 1GB
B. 232 bytes = 4GB
C. 232
words = 16GB
5. • How do programs share the memory?
• Where do we put them?
#2: Holes in our address space?
32-bit RAM
address space (4GB)
1. Program 1 and 2 fit
(They use 3GB of memory, leaving 1 GB free)
2. Quit Program 1
(Program 2 uses 2GB of memory, leaving 2GB free)
3. Can’t run Program 3
(Even though we have enough free space!)
Memory Fragmentation
6. • Each program can access any 32-bit memory address.
• What if multiple programs access the same address?
– MOV [1024], AH Write to address 1024 regardless of the program that’s running
• They can corrupt or crash each other: security and reliability.
#3: How do we keep programs secure?
32-bit RAM
address space (4GB)
1. Program 1 stores your bank
balance at address 1024
2. Program 2 stores your game
score at address 1024.
3. Profit? Loss? Not Good!
7. • If all programs have access to the same 32-bit memory space:
• How do we solve this?
– Key to the problem: “same memory space”
– Can we give each program its own virtual memory space?
– If so, we can:
• Separately map each program’s memory space to the RAM memory space
• (and even move it to disk if we run out of memory)
Problems with memory?
9. “Any problem in computer science can be solved by adding indirection”
Virtual Memory takes program addresses and maps them to RAM
addresses
Virtual Memory is a layer of indirection
Without Virtual Memory
Program Address = RAM Address
32-bit program
address space
(4GB)
30-bit RAM
address space
(1GB)
0
1
…
??
With Virtual Memory
Program Address Maps to RAM Address
32-bit program
address space
(4GB)
30-bit RAM
address space
(1GB)
0
1
…
Disk
Map
0
1
2
VM: Mapping gives us
flexibility in how we
use the RAM
10. Solving the problem: #1 not enough memory?
With Virtual Memory
Program Address Maps to RAM Address
32-bit program
address space
(4GB)
30-bit RAM
address space
(1GB)
Disk
Map
• Map some of the program’s address space to the disk
• When we need it, we bring it into memory
11. Solving the problem: #2 holes in the address space?
With Virtual Memory
Program Address Maps to RAM Address
Each program has its own
mapping.
Mappings let us put our
program data wherever we want
in the RAM
32-bit program
address space
(4GB)
Map
• How do we use the holes left when programs quit?
• We can map a program’s addresses to RAM addresses however we like
12. Solving the problem: #3 keeping programs secure?
Map
• Program 1’s and Program 2’s addresses map to different RAM addresses.
• Because each program has its own address space, they cannot access each
other’s data: security and reliability!
With Virtual Memory
Program Address Maps to RAM Address
1. Program 1 stores your bank
balance at address 1024
1A. VM maps it to RAM Address 1.
2. Program 2 stores your video
game score at address 1024
2B. VM maps it to RAM address 5.
3. Neither can touch the other’s
data.
13. How does VM work?
Vir tual and Phys ic al Addr es s es
14. How does VM work?
• Basic idea: separate memory spaces
– Virtual memory: what the program sees
• E.g. MOV [1024], AX accesses virtual address 1024
– Physical memory: the physical RAM in the computer
• E.g. if you have 2GB of RAM installed, you have physical addresses: 0 to 𝟐 𝟑𝟏
-1
• Virtual Addresses (VA)
– What the program uses
– In our example, this was full 32 bit address space: 0 to 𝟐 𝟑𝟏-1
• Physical Addresses (PA)
– What the hardware uses to talk to the RAM
– Address space determined by how much RAM is installed.
16. Page Tables
• The map from Virtual Address to Physical Address is the Page
Table
17. First in First out Algorithm
• Reference string: 7,0,1,2,0,3,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)
• How to track ages of pages?
– What the hardware uses to talk to the RAM
– Address space determined by how much RAM is installed.
• How to track ages of pages?
– Just use a FIFO queue
1 7 2 4 0 7
2 0 3 2 1 0
3 1 0 3 2 1
18. Optimal Algorithm
• Replace page that will not be used for longest period of time
• How do you know this?
– Can’t read the future
– Theoreticians refer to this as an “oracle”
– Not resizeable
• Used for measuring how well your algo performs?
19. 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
• 12 faults – better than FIFO but worse than OPT
• Generally good algo and frequently used