OS Lecture-13 (Virtual Memory and Page Replacement Algorithms)
OS Lecture-13 (Virtual Memory and Page Replacement Algorithms)
Virtual Memory
and
Page Replacement Algorithms
Virtual Memory
• Virtual memory – separation of user logical memory from physical
memory.
– A section of a hard disk that's set up to emulate the computer's RAM.
– Only part of the program needs to be in memory for execution.
– Logical address space can therefore be much larger than physical
address space.
– Need to allow pages to be swapped in and out.
Virtual Memory (cont.)
Virtual Address Space
Advantages of Virtual Memory
• Multitasking.
• When this bit is set to "valid“, this value indicates that the associated page
is both legal and in memory.
running 2
process reference
trap
1
i
6
restart
instruction
page table
free frame
5 4
reset bring in
page table missing page
disk
physical
memory
What happens if there is no free frame?
• Page replacement: Are all those pages in memory being
referenced? Choose one to swap back out to disk and make room
to load a new page.
– Algorithm: How you choose a victim.
• Side effect: The same page may be brought in and out of memory
several times.
Performance of Demand Paging
• Page Fault Rate 0 p 1.0
– if p = 0 no page faults
– if p = 1, every reference is a fault
3. Read the desired page into the (newly) free frame. Update the page and
frame tables.
• Reference string: 1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 1 4 4 4 5 5 5 5 5 5
# of page
2 2 2 2 1 1 1 1 1 3 3 3
faults = 9
3 3 3 3 2 2 2 2 2 4 4
F F F F F F F N N F F N
1 1 1 1 1 1 1 5 5 5 5 4 4
2 2 2 2 2 2 2 1 1 1 1 5
# of page
faults = 10 3 3 3 3 3 3 3 2 2 2 2
4 4 4 4 4 4 4 3 3 3
F F F F N N F F F F F F
First-In-First-Out (FIFO) Algorithm (cont.)
• FIFO algorithm suffers from Belady’s anomaly:
allocated frames page-fault rate
Optimal (OPT) Algorithm
• Replace page that will not be used for longest period of time.
• Reference string: 1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 1 1 1 1 1 1 1 1 4 4
2 2 2 2 2 2 2 2 2 2 2 2
# of page
3 3 3 3 3 3 3 3 3 3 3
faults = 6
4 4 4 4 5 5 5 5 5 5
F F F F N N F N N N F N
• OPT algorithm guarantees the lowest possible page-fault rate for a fixed number of
pages.
• But it is difficult to implement, because it requires future knowledge of the
reference string.
• As a result, OPT is used mainly for comparison purposes.
Least Recently Used (LRU) Algorithm
• LRU associates with each page the time of that page’s last use.
• When a page must be replaced, LRU chooses the page that has not been
used for the longest period of time.
• We can think of this strategy as OPT looking backward in time, rather than
forward.
• Reference string: 1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 1 1 1 1 1 1 1 1 1 5
2 2 2 2 2 2 2 2 2 2 2 2
# of page
3 3 3 3 3 5 5 5 5 4 4
faults = 8
4 4 4 4 4 4 4 3 3 3
F F F F N N F N N F F F
Implementation of the LRU Policy
• Each page could be tagged (in the page table entry) with the time at
each memory reference.
• The LRU page is the one with the smallest time value (needs to be
searched at each page fault).
a b
Most recently
used page goes
on the top 2 7
1 2
0 1
Least recently 7 0
used page is at
the top 4 4
Stack at a Stack at b
– Replace the one which is 0 (if one exists) – we do not know the
real order of use, however.
LRU Approximation Algorithm-2
• Second Chance (Clock):
─ The set of frames candidate for replacement is considered as a
circular buffer.
─ When a page is replaced, a pointer is set to point to the next
frame in buffer.
─ A reference bit for each frame is set to 1 whenever:
• a page is first loaded into the frame.
• the corresponding page is referenced.
4 5 5 5 6 7 8 6 7 8 6 7 8 4 1 5 2 4 4 1
1 04 4
0 0
4 0
4 4
0
4
0
8
0
8
0
8
0
8
1
8
1
8
1
8
1
8
0
8
0
8
0
2
0
2
0
2
0
2
0
2 5 5 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4
0 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 1 1 1
3 6 6 6 6 6 6 6 6 6 6 1 1 1 1 1 1
0 0 0 1 1 1 1 1 1 0 0 0 0 0 0 1
4 7 7 7 7 7 7 7 7 7 7 5 5 5 5 5
0 0 0 1 1 1 1 1 0 0 0 0 0 0 0
F F N N F F F N N N N N N F F F F N N N
# of page faults = 9
Counting Algorithms
• Keep a counter of the number of references that have been made
to each page.
• Based on the heuristic that a page not referenced often is not likely
to be referenced in the future.
Reference string: 7 0 1 2 0 3 0 4 2 3 0 3 2
1 1
7 1
7 1
7 1
2 1
2 1
2 1
2 1
4 1
4 1
3 1
3 2
3 2
3
# of page 2 1
0 1
0 1
0 2
0 2
0 3
0 3
0 3
0 3
0 4
0 4
0 4
0
faults = 8
3 11 11 11 13 13 13 12 12 12 12 22
F F F F N F N F F F N N N
Least Frequently Used (LFU) Algorithm
• Replaces page that is least intensively referenced
• Based on the heuristic that a page not referenced often is not likely to be
referenced in the future
• Could easily select wrong page for replacement
• A page that was referenced heavily in the past may never be referenced again,
but will stay in memory while newer, active pages are replaced
• Reference string: 1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 1 1 1 1 1 1 1 1 1 5
2 2 2 2 2 2 2 2 2 2 2 2
# of page
3 3 3 3 3 5 5 5 5 4 4
faults = 8
4 4 4 4 4 4 4 3 3 3
F F F F N N F N N F F F
Question
• Given page reference string:
1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6
Compare the number of page faults for FIFO, LRU, LFU, Second Chance
and Optimal page replacement algorithm assuming that 4 frames are
available in memory.
Allocation of Frames
Allocation of Frames
• Each process needs a minimum number of pages.
– Priority allocation
Fixed Allocation
• Equal allocation – e.g., if 100 frames and 5 processes, give each 20
pages.
si size of process pi m 64
si 10
S si
s2 127
m total number of frames
10
si a1 64 5
ai allocation for pi m 137
S 127
a2 64 59
137
Priority Allocation
• Use a proportional allocation scheme using priorities rather
than size.
• The processor spends most of its time swapping pages rather than
executing user instructions.
Thrashing (cont.)
Demand Paging and Thrashing
• Why does demand paging work?
• Locality model
– Process migrates from one locality to another
– Localities may overlap
• E.g.
for (……) {
computations;
}
…..
for (….. ) {
computations;
}
• Why does thrashing occur?
Σ size of locality > total memory size
Solutions of Thrashing
• Local replacement
– One process cannot steal frames from other processes
…2 6 1 5 7 7 7 7 5 1 6 2 3 4 1 2 3 4 4 4 3 4 3 4 4 4 1 3 2 3 4
t1 t2
• How can the system use this number to give optimum memory to the
process?
Working-Set Model (cont.)
• ∆ ≡ working-set window ≡ fixed number of page references
Example: 10,000 instructions
…2 6 1 5 7 7 7 7 5 1 6 2 3 4 1 2 3 4 4 4 3 4 3 4 4 4 1 3 2 3 4
∆ ∆
t1 t2
WS(t1) = {1, 2, 5, 6, 7} WS(t2) = {3, 4}
• Compute working set size for each process in the system WSSi.
0, 9, 0, 1, 8 1, 8, 7, 8, 7 1, 2, 8, 2, 7 8, 2, 3, 8, 3,
What is the working set W(t, ∆), with t equal to the time between the 15th
and 16th references, and ∆ equal to 6 references?
Solution:
• Disadvantages:
– requires a DMA controller to carry out the operation, which increases
the cost of the system
– cache coherence problems