13-Virtual Memory 1
13-Virtual Memory 1
* Slides adopted from Prof Kosar, Dantu, and Farshad Ghanei at UB, “Operating System
Concepts” book and supplementary material by A. Silberschatz, P.B. Galvin, and G. Gagne.
Wiley Publishers
The materials provided by the instructor in this course are for the use of the students enrolled in the course only.
Copyrighted course materials may not be further disseminated without instructor permission.
Summary
● Main Memory:
○ Implementation of Paging
○ Effective Access Time
○ Multi-level Paging
○ Example: Intel 32-bit Architecture
2
Today
● Virtual Memory
○ Demand Paging
○ Page Faults
○ Page Replacement
○ Page Replacement Algorithms
■ FIFO
■ Optimal Algorithm
■ LRU & LRU Approximations
■ Counting Algorithms
3
Virtual Memory
● Separation of user logical memory from physical memory
○ Only part of the program needs to be in the memory for execution.
○ Logical address space can therefore be much larger than physical address
space.
○ Allows address spaces to be shared by several processes.
○ Allows for more efficient process creation.
4
Virtual Memory
Page 0
Frame 0 Page 1
Frame 1 Page 2
Frame 2 .
.
.
.
.
.
Frame N-1
Page M-1
7
Demand Paging
● Bring a page into memory only when it is needed
○ Less I/O needed
○ Less memory needed
○ Faster response
○ More users
8
Frame X 1
Frame Y 0
.
.
.
Demand Paging - Valid/Invalid Bit
● With each page table entry, a valid/invalid bit is associated
○ 0: not-in-memory or invalid
○ 1: in-memory and legal
● Initially valid/invalid bit is set to 0 on all entries
● Example of a page table snapshot:
10
Demand Paging - Valid/Invalid Bit
11
Demand Paging - Page Fault
● If there is ever a reference to a page not in memory, first reference will trap to OS
(Page fault)
● OS looks at another table (in PCB) to decide:
○ Invalid reference (out of bounds or etc.) → Abort the process
○ Valid reference but not in memory → Swap-in
■ Get an empty frame
■ Swap (read) page into the new frame
■ Set the page table, and validation bit=1
■ Restart instruction
12
Demand Paging - Page Fault
13
Page Replacement
What happens if there is no free frame?
● Page replacement: Find some page that resides in memory, but is not really in use,
swap it out to free up space.
○ Algorithms (FIFO, LRU, …)
○ Performance: Want an algorithm which will result in minimum number of page
faults.
○ Same page may be brought into memory several times
14
Page Replacement
● Prevent over-allocation of memory by modifying page-fault service routine to include
page replacement
● Use modify (dirty) bit to reduce overhead of page transfers
Only modified pages are swapped out (written to disk)
● Page replacement completes separation between logical memory and physical
memory
Large virtual memory can be provided on a smaller physical memory
15
Page Replacement - Simplified
1. Find the location of the desired page on disk
2. Find a free frame:
a. If there is a free frame, use it
b. If there is no free frame, use a page replacement algorithm to select a victim
3. Read the desired page into the [newly] free frame. Update the page and frame tables
4. Restart the process
16
Page Replacement
17
Page Replacement Algorithms
● We want lowest page fault rate
● Evaluate algorithm by running it on a particular string of memory references
(reference string) and computing the number of page faults on that string.
● In our examples, the reference string is:
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
18
Page Replacement Algorithms - FIFO
● First-In, First-Out (FIFO)
● Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
● 3 frames (3 pages can be in memory at a time)
● The blue arrow represents the next victim
Referenced
1 2 3 4 1 2 5 1 2 3 4 5
Page
Frame-1 1 1 1 4 4 4 5 5 5 5 5 5
Frame-2 2 2 2 1 1 1 1 1 3 3 3
Frame-3 3 3 3 2 2 2 2 2 4 4
19
9 page faults
Page Replacement Algorithms - FIFO
● Expected Graph of Page Faults vs The Number of Frames
20
Page Replacement Algorithms - FIFO
● Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
● 4 frames (4 pages can be in memory at a time)
Referenced
1 2 3 4 1 2 5 1 2 3 4 5
Page
Frame-1 1 1 1 1 1 1 5 5 5 5 4 4
Frame-2 2 2 2 2 2 2 1 1 1 1 5
Frame-3 3 3 3 3 3 3 2 2 2 2
Frame-4 4 4 4 4 4 4 3 3 3
21
10 page faults → Belady’s Anomaly (more frames, more page faults)
Page Replacement Algorithms - FIFO
● FIFO is obvious, and simple to implement
○ When you page in something, put it on the tail of a list
○ Evict page at the head of the list
● Why might this be good?
○ Maybe the one brought in longest ago is not being used
● Why might this be bad?
○ Maybe the one brought a while ago is being used!
○ No information either way!
● In fact, FIFO’s performance is typically lousy
● FIFO also suffers from Belady’s anomaly
22
Page Replacement Algorithms - Optimal
● Replace page that will not be used for the longest time in future
● 4 frames available. Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
Referenced
1 2 3 4 1 2 5 1 2 3 4 5
Page
Frame-1 1 1 1 1 1 1 1 1 1 1 4 4
Frame-2 2 2 2 2 2 2 2 2 2 2 2
Frame-3 3 3 3 3 3 3 3 3 3 3
Frame-4 4 4 4 5 5 5 5 5 5
6 page faults
➢ How would you know in advance? 23
Page Replacement Algorithms - Optimal
● Belady’s optimal algorithm is provably optimal, with lowest fault rate
○ Evict the page that won’t be used for the longest time in future
○ It is impossible to predict the future!
● Why Belady’s optimal algorithm useful?
○ As a reference to compare other algorithms
● Is there a best practical algorithm?
○ No, it depends on the workload
● Is there a worst algorithm?
○ No, but random replacement is pretty bad!
■ However there are some situations where OSs use near-random algorithms
24
quite effectively
Summary
● Virtual Memory
○ Demand Paging
○ Page Faults
○ Page Replacement
○ Page Replacement Algorithms
■ FIFO
■ Optimal Algorithm
■ LRU & LRU Approximations
■ Counting Algorithms
25
Acknowledgements
● “Operating Systems Concepts” book and supplementary material by A. Silberschatz,
P. Galvin and G. Gagne
● “Operating Systems: Internals and Design Principles” book and supplementary
material by W. Stallings
● “Modern Operating Systems” book and supplementary material by A. Tanenbaum
● R. Doursat and M. Yuksel from University of Nevada, Reno
● T. Kosar and K. Dantu from University at Buffalo
26