Chapter 9-Virtual Memory - Updated
Chapter 9-Virtual Memory - Updated
Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009
Chapter 9: Virtual Memory
Background
Demand Paging
Copy-on-Write
Page Replacement
Allocation of Frames
Thrashing
Memory-Mapped Files
Allocating Kernel Memory
Other Considerations
Operating-System Examples
Operating System Concepts – 8th Edition 9.2 Silberschatz, Galvin and Gagne ©2009
Objectives
To describe the benefits of a virtual memory system
Operating System Concepts – 8th Edition 9.3 Silberschatz, Galvin and Gagne ©2009
Background
Virtual memory – separation of user logical memory from
physical memory.
Operating System Concepts – 8th Edition 9.4 Silberschatz, Galvin and Gagne ©2009
Virtual Memory That is
Larger Than Physical Memory
Operating System Concepts – 8th Edition 9.5 Silberschatz, Galvin and Gagne ©2009
Demand Paging
Bring a page into memory only when it is needed
Less I/O needed
Less memory needed
Faster response
Higher Degree of Multi-programming
Lazy swapper – never swaps a page into memory unless page will be
needed
Swapper that deals with pages is a pager
Operating System Concepts – 8th Edition 9.6 Silberschatz, Galvin and Gagne ©2009
Valid-Invalid Bit
With each page table entry a valid–invalid bit is associated
(v in-memory, i not-in-memory)
Initially valid–invalid bit is set to i on all entries
Example of a page table snapshot:
i
i
page table
Operating System Concepts – 8th Edition 9.8 Silberschatz, Galvin and Gagne ©2009
Page Fault
Operating System Concepts – 8th Edition 9.9 Silberschatz, Galvin and Gagne ©2009
Steps in Handling a Page Fault
Operating System Concepts – 8th Edition 9.10 Silberschatz, Galvin and Gagne ©2009
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
Operating System Concepts – 8th Edition 9.11 Silberschatz, Galvin and Gagne ©2009
Demand Paging Example
Memory access time = 100 nanosec
Page fault service time = 25 millisec
Teffective = (1 - p) x 100 + p (25 milli)
= (1 - p) x 100 + p (25000000)
= 100 + 24999900 x p
If one access out of 1000 causes a page
fault, effective access time is 25
microseconds, a slowdown by a factor of
250.
Operating System Concepts – 8th Edition 9.12 Silberschatz, Galvin and Gagne ©2009
Demand Paging Example
If we want less than 10 percentage
degradation in effective memory access
time then we have the following inequality
110 > 100 + 25000000 x p
10 > 25000000 x p
p < 0.0000004
This means we can allow only one page
fault every 2,500,000.
Operating System Concepts – 8th Edition 9.13 Silberschatz, Galvin and Gagne ©2009
Demand Paging Example
Memory access time = 200 nanoseconds
Operating System Concepts – 8th Edition 9.14 Silberschatz, Galvin and Gagne ©2009
What happens if there is no free frame?
Page replacement – find some page in memory, but not really in use,
swap it out
algorithm
performance – want an algorithm which will result in minimum
number of page faults
Operating System Concepts – 8th Edition 9.15 Silberschatz, Galvin and Gagne ©2009
Page Replacement
Prevent over-allocation of memory by modifying page-fault service
routine to include page replacement
Operating System Concepts – 8th Edition 9.16 Silberschatz, Galvin and Gagne ©2009
Basic Page Replacement
3. Bring the desired page into the (newly) free frame; update the page
and frame tables
Operating System Concepts – 8th Edition 9.17 Silberschatz, Galvin and Gagne ©2009
Page Replacement
Operating System Concepts – 8th Edition 9.18 Silberschatz, Galvin and Gagne ©2009
Page Replacement Algorithms
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
Operating System Concepts – 8th Edition 9.19 Silberschatz, Galvin and Gagne ©2009
Graph of Page Faults Versus
The Number of Frames
Operating System Concepts – 8th Edition 9.20 Silberschatz, Galvin and Gagne ©2009
First-In-First-Out (FIFO) Algorithm
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 per process)
1 1 4 5
2 2 1 3 9 page faults
3 3 2 4
4 frames
1 1 5 4
2 2 1 5 10 page faults
3 3 2
4 4 3
Operating System Concepts – 8th Edition 9.21 Silberschatz, Galvin and Gagne ©2009
FIFO Page Replacement
Operating System Concepts – 8th Edition 9.22 Silberschatz, Galvin and Gagne ©2009
FIFO Illustrating Belady’s Anomaly
Operating System Concepts – 8th Edition 9.23 Silberschatz, Galvin and Gagne ©2009
Optimal Algorithm
Replace page that will not be used for longest period of time
4 frames example
1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 4
2 6 page faults
3
4 5
How do you know this?
Used for measuring how well your algorithm performs
Operating System Concepts – 8th Edition 9.24 Silberschatz, Galvin and Gagne ©2009
Optimal Page Replacement
Operating System Concepts – 8th Edition 9.25 Silberschatz, Galvin and Gagne ©2009
Least Recently Used (LRU) Algorithm
Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1 1 1 1 5
2 2 2 2 2
3 5 5 4 4
4 4 3 3 3
Counter implementation
Every page entry has a counter; every time page is referenced
through this entry, copy the clock into the counter
When a page needs to be changed, look at the counters to
determine which are to change
Operating System Concepts – 8th Edition 9.26 Silberschatz, Galvin and Gagne ©2009
LRU Page Replacement
Operating System Concepts – 8th Edition 9.27 Silberschatz, Galvin and Gagne ©2009
Use Of A Stack to Record The
Most Recent Page References
Operating System Concepts – 8th Edition 9.28 Silberschatz, Galvin and Gagne ©2009
Counting Algorithms
Keep a counter of the number of references that have been made to
each page
MFU Algorithm: based on the argument that the page with the
smallest count was probably just brought in and has yet to be used
Operating System Concepts – 8th Edition 9.29 Silberschatz, Galvin and Gagne ©2009
Allocation of Frames
Operating System Concepts – 8th Edition 9.30 Silberschatz, Galvin and Gagne ©2009
Fixed Allocation
Equal allocation – For example, if there are 100 frames and 5
processes, give each process 20 frames.
Operating System Concepts – 8th Edition 9.31 Silberschatz, Galvin and Gagne ©2009
Priority Allocation
Use a proportional allocation scheme using priorities rather than size
Operating System Concepts – 8th Edition 9.32 Silberschatz, Galvin and Gagne ©2009
Global vs. Local Allocation
Local replacement – each process selects from only its own set of
allocated frames
Operating System Concepts – 8th Edition 9.33 Silberschatz, Galvin and Gagne ©2009
Thrashing
If a process does not have “enough” pages, the page-fault rate is very
high. This leads to:
low CPU utilization
operating system thinks that it needs to increase the degree of
multiprogramming
another process added to the system
Operating System Concepts – 8th Edition 9.34 Silberschatz, Galvin and Gagne ©2009
Thrashing (Cont.)
Operating System Concepts – 8th Edition 9.35 Silberschatz, Galvin and Gagne ©2009
Demand Paging and Thrashing
Operating System Concepts – 8th Edition 9.36 Silberschatz, Galvin and Gagne ©2009
Page Replacement Algorithms
Operating System Concepts – 8th Edition 9.37 Silberschatz, Galvin and Gagne ©2009
1. The Principle of Optimality
In this strategy, that page is replaced that
will not be used again for the furthest time
into the future.
Although it is possible to demonstrate the
optimality of this strategy, it cannot be fully
applied since the future cannot be
predicted.
Operating System Concepts – 8th Edition 9.38 Silberschatz, Galvin and Gagne ©2009
2. Random Page Replacement
Operating System Concepts – 8th Edition 9.40 Silberschatz, Galvin and Gagne ©2009
Implementation: This strategy can also
be implemented with a simple FIFO
queue.
As each page arrives, it is placed at the
tail of the queue and pages are replaced
from the head of the queue.
-ve: The overhead of keeping the
time-stamp.
Belady’s Anomaly (Reading Homework
Assignment).
Operating System Concepts – 8th Edition 9.41 Silberschatz, Galvin and Gagne ©2009
Example
Operating System Concepts – 8th Edition 9.42 Silberschatz, Galvin and Gagne ©2009
FIFO
Memory page B E E R B A R E B E A R
1 B
2
3
Operating System Concepts – 8th Edition 9.43 Silberschatz, Galvin and Gagne ©2009
FIFO
Memory page B E E R B A R E B E A R
1 B
2 E
3
Operating System Concepts – 8th Edition 9.44 Silberschatz, Galvin and Gagne ©2009
FIFO
Memory page B E E R B A R E B E A R
1 B
2 E *
3
Operating System Concepts – 8th Edition 9.45 Silberschatz, Galvin and Gagne ©2009
FIFO
Memory page B E E R B A R E B E A R
1 B
2 E *
3 R
Operating System Concepts – 8th Edition 9.46 Silberschatz, Galvin and Gagne ©2009
FIFO
Memory page B E E R B A R E B E A R
1 B *
2 E *
3 R
Operating System Concepts – 8th Edition 9.47 Silberschatz, Galvin and Gagne ©2009
FIFO
Memory page B E E R B A R E B E A R
1 B *
2 E *
3 R
Operating System Concepts – 8th Edition 9.48 Silberschatz, Galvin and Gagne ©2009
FIFO
Memory page B E E R B A R E B E A R
1 B * A
2 E *
3 R
Operating System Concepts – 8th Edition 9.49 Silberschatz, Galvin and Gagne ©2009
FIFO
Memory page B E E R B A R E B E A R
1 B * A
2 E *
3 R *
Operating System Concepts – 8th Edition 9.50 Silberschatz, Galvin and Gagne ©2009
FIFO
Memory page B E E R B A R E B E A R
1 B * A
2 E * *
3 R *
Operating System Concepts – 8th Edition 9.51 Silberschatz, Galvin and Gagne ©2009
FIFO
Memory page B E E R B A R E B E A R
1 B * A
2 E * *
3 R *
Operating System Concepts – 8th Edition 9.52 Silberschatz, Galvin and Gagne ©2009
FIFO
Memory page B E E R B A R E B E A R
1 B * A
2 E * * B
3 R *
Operating System Concepts – 8th Edition 9.53 Silberschatz, Galvin and Gagne ©2009
FIFO
Memory page B E E R B A R E B E A R
1 B * A
2 E * * B
3 R *
Operating System Concepts – 8th Edition 9.54 Silberschatz, Galvin and Gagne ©2009
FIFO
Memory page B E E R B A R E B E A R
1 B * A
2 E * * B
3 R * E
Operating System Concepts – 8th Edition 9.55 Silberschatz, Galvin and Gagne ©2009
FIFO
Memory page B E E R B A R E B E A R
1 B * A *
2 E * * B
3 R * E
Operating System Concepts – 8th Edition 9.56 Silberschatz, Galvin and Gagne ©2009
FIFO
Memory page B E E R B A R E B E A R
1 B * A *
2 E * * B
3 R * E
Operating System Concepts – 8th Edition 9.57 Silberschatz, Galvin and Gagne ©2009
FIFO
Memory page B E E R B A R E B E A R
1 B * A * R
2 E * * B
3 R * E
Operating System Concepts – 8th Edition 9.58 Silberschatz, Galvin and Gagne ©2009
FIFO
7 page faults
Memory page B E E R B A R E B E A R
1 B * A * R
2 E * * B
3 R * E
Operating System Concepts – 8th Edition 9.59 Silberschatz, Galvin and Gagne ©2009
4. LRU (Least-Recently-
Used)
Memory page B E E R B A R E B E A R
1 B
2 E *
3 R
Operating System Concepts – 8th Edition 9.62 Silberschatz, Galvin and Gagne ©2009
LRU
Memory page B E E R B A R E B E A R
1 B *
2 E *
3 R
Operating System Concepts – 8th Edition 9.63 Silberschatz, Galvin and Gagne ©2009
LRU
Memory page B E E R B A R E B E A R
1 B *
2 E *
3 R
Operating System Concepts – 8th Edition 9.64 Silberschatz, Galvin and Gagne ©2009
LRU
Memory page B E E R B A R E B E A R
1 B *
2 E * A
3 R
Operating System Concepts – 8th Edition 9.65 Silberschatz, Galvin and Gagne ©2009
LRU
Memory page B E E R B A R E B E A R
1 B *
2 E * A
3 R *
Operating System Concepts – 8th Edition 9.66 Silberschatz, Galvin and Gagne ©2009
LRU
Memory page B E E R B A R E B E A R
1 B *
2 E * A
3 R *
Operating System Concepts – 8th Edition 9.67 Silberschatz, Galvin and Gagne ©2009
LRU
Memory page B E E R B A R E B E A R
1 B * E
2 E * A
3 R *
Operating System Concepts – 8th Edition 9.68 Silberschatz, Galvin and Gagne ©2009
LRU
Memory page B E E R B A R E B E A R
1 B * E
2 E * A
3 R *
Operating System Concepts – 8th Edition 9.69 Silberschatz, Galvin and Gagne ©2009
LRU
Memory page B E E R B A R E B E A R
1 B * E
2 E * A B
3 R *
Operating System Concepts – 8th Edition 9.70 Silberschatz, Galvin and Gagne ©2009
LRU
Memory page B E E R B A R E B E A R
1 B * E *
2 E * A B
3 R *
Operating System Concepts – 8th Edition 9.71 Silberschatz, Galvin and Gagne ©2009
LRU
Memory page B E E R B A R E B E A R
1 B * E *
2 E * A B
3 R *
Operating System Concepts – 8th Edition 9.72 Silberschatz, Galvin and Gagne ©2009
LRU
Memory page B E E R B A R E B E A R
1 B * E *
2 E * A B
3 R * A
Operating System Concepts – 8th Edition 9.73 Silberschatz, Galvin and Gagne ©2009
LRU
Memory page B E E R B A R E B E A R
1 B * E *
2 E * A B
3 R * A
Operating System Concepts – 8th Edition 9.74 Silberschatz, Galvin and Gagne ©2009
LRU
Memory page B E E R B A R E B E A R
1 B * E *
2 E * A B R
3 R * A
Operating System Concepts – 8th Edition 9.75 Silberschatz, Galvin and Gagne ©2009
LRU
8 page faults
Memory page B E E R B A R E B E A R
1 B * E *
2 E * A B R
3 R * A
Operating System Concepts – 8th Edition 9.76 Silberschatz, Galvin and Gagne ©2009
5. LFU (Least-Frequently-
Used)
Memory page B E E R B A R E B E A R
1 B
2
3
Operating System Concepts – 8th Edition 9.78 Silberschatz, Galvin and Gagne ©2009
LFU
Memory page B E E R B A R E B E A R
1 B
2 E
3
Operating System Concepts – 8th Edition 9.79 Silberschatz, Galvin and Gagne ©2009
LFU
Memory page B E E R B A R E B E A R
1 B
2 E 2
3
Operating System Concepts – 8th Edition 9.80 Silberschatz, Galvin and Gagne ©2009
LFU
Memory page B E E R B A R E B E A R
1 B
2 E 2
3 R
Operating System Concepts – 8th Edition 9.81 Silberschatz, Galvin and Gagne ©2009
LFU
Memory page B E E R B A R E B E A R
1 B 2
2 E 2
3 R
Operating System Concepts – 8th Edition 9.82 Silberschatz, Galvin and Gagne ©2009
LFU
Memory page B E E R B A R E B E A R
1 B 2
2 E 2
3 R A
Operating System Concepts – 8th Edition 9.83 Silberschatz, Galvin and Gagne ©2009
LFU
Memory page B E E R B A R E B E A R
1 B 2
2 E 2
3 R A R
Operating System Concepts – 8th Edition 9.84 Silberschatz, Galvin and Gagne ©2009
LFU
Memory page B E E R B A R E B E A R
1 B 2
2 E 2 3
3 R A R
Operating System Concepts – 8th Edition 9.85 Silberschatz, Galvin and Gagne ©2009
LFU
Memory page B E E R B A R E B E A R
1 B 2 3
2 E 2 3
3 R A R
Operating System Concepts – 8th Edition 9.86 Silberschatz, Galvin and Gagne ©2009
LFU
Memory page B E E R B A R E B E A R
1 B 2 3
2 E 2 3 4
3 R A R
Operating System Concepts – 8th Edition 9.87 Silberschatz, Galvin and Gagne ©2009
LFU
Memory page B E E R B A R E B E A R
1 B 2 3
2 E 2 3 4
3 R A R A
Operating System Concepts – 8th Edition 9.88 Silberschatz, Galvin and Gagne ©2009
LFU
Memory page B E E R B A R E B E A R
1 B 2 3
2 E 2 3 4
3 R A R A R
Operating System Concepts – 8th Edition 9.89 Silberschatz, Galvin and Gagne ©2009
LFU
7 page faults
Memory page B E E R B A R E B E A R
1 B 2 3
2 E 2 3 4
3 R A R A R
Operating System Concepts – 8th Edition 9.90 Silberschatz, Galvin and Gagne ©2009
6. NUR (Not-Used-Recently)
Pages not used recently are not likely to be used in the
near future and they may be replaced with incoming pages.
This is a scheme for approximating LRU with little
overhead.
Implementation: NUR is implemented with addition of two
hardware bits per page.
These are:
(1). Reference bit = 0 if the page has not been referenced
1 if the page has been referenced
(2). Dirty bit = 0 if the page has not been modified
1 if the page has been modified
Operating System Concepts – 8th Edition 9.91 Silberschatz, Galvin and Gagne ©2009
NUR
Initially, the reference bits of all pages are set to 0.
If a particular page is referenced, its reference bit is set to
1.
Initially, the dirty bits of all pages are also set to 0.
Whenever a page is modified, its modified bit is set to 1.
When a page is to be replaced, the OS first tries to find a
page which has not been referenced (otherwise, we have
no choice but to replace a referenced page).
If a page has not been referenced, we check whether it has
been modified or not.
If it has not been modified, we replace it on the basis that
there is less overhead involved than in replacing a modified
page that must be written back to secondary storage.
Otherwise, a modified page is replaced.
Operating System Concepts – 8th Edition 9.92 Silberschatz, Galvin and Gagne ©2009
The Principle of Locality
Operating System Concepts – 8th Edition 9.93 Silberschatz, Galvin and Gagne ©2009
Temporal Locality
Operating System Concepts – 8th Edition 9.94 Silberschatz, Galvin and Gagne ©2009
Spatial Locality
Operating System Concepts – 8th Edition 9.95 Silberschatz, Galvin and Gagne ©2009
Working Set Theory
Denning formulated the working set theory of program
behavior based upon observations of locality.
A working set is a collection of pages a process is
actively referencing.
The real working set of a process is the set of pages that
must be in main memory for a process to execute
efficiently.
Denning maintained that for a program to run efficiently, its
working set of pages must be maintained in the main
memory.
Else, excessive paging activity (Thrashing) might occur as
the program repeatedly requests pages from the secondary
storage.
Operating System Concepts – 8th Edition 9.96 Silberschatz, Galvin and Gagne ©2009
Locality In A Memory-Reference Pattern
Operating System Concepts – 8th Edition 9.97 Silberschatz, Galvin and Gagne ©2009
Working-Set Model
working-set window a fixed number of page references
if D > m Thrashing
Operating System Concepts – 8th Edition 9.98 Silberschatz, Galvin and Gagne ©2009
Working-set model
Operating System Concepts – 8th Edition 9.99 Silberschatz, Galvin and Gagne ©2009
Page-Fault Frequency Scheme
Operating System Concepts – 8th Edition 9.100 Silberschatz, Galvin and Gagne ©2009
Question
Operating System Concepts – 8th Edition 9.101 Silberschatz, Galvin and Gagne ©2009
Question
Operating System Concepts – 8th Edition 9.102 Silberschatz, Galvin and Gagne ©2009
P is a set of processes. R is a set of
resources. E is a set of request or
assignment edges. The
sets P, R, and E are as follows:
P = {P1, P2, P3}, R = {R1,R2,R3}
Operating System Concepts – 8th Edition 9.103 Silberschatz, Galvin and Gagne ©2009
E = {P1 → R1, P1 → R2, P2 → R2, P2 →
R3, P3 → R2, P3 → R3,R1 → P2,R2 →
P2,R3 → P1}.
R1 has one instance. R2 has two
instances. R3 has one instances.
(a) Draw the resource-allocation graph.
(b) Is there any deadlock in this situation?
Briefly Explain.
Operating System Concepts – 8th Edition 9.104 Silberschatz, Galvin and Gagne ©2009
Question
Operating System Concepts – 8th Edition 9.105 Silberschatz, Galvin and Gagne ©2009
End of Chapter 9
Operating System Concepts – 8th Edition Silberschatz, Galvin and Gagne ©2009