Transcript ch10

Chapter 10: Virtual Memory
 Background
 Demand Paging
 Page Replacement
 Allocation of Frames
 Thrashing
 Operating System Example
Operating System Concepts
10.1
Silberschatz, Galvin and Gagne 2002
Virtual memory
 separation of logical memory from physical memory
 only part of the program must be in 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
implemented via:
 demand paging
 demand segmentation
Operating System Concepts
10.2
Silberschatz, Galvin and Gagne 2002
Demand Paging
 Bring a page into memory only when it is needed
 Less I/O needed
 Less memory needed
 Faster response
 More users
 Page is needed  reference to it
 invalid reference  abort
 not-in-memory  bring to memory
Operating System Concepts
10.4
Silberschatz, Galvin and Gagne 2002
Valid-Invalid Bit
 each page table entry has valid–invalid bit:
1  in-memory, 0  not-in-memory
 initially valid–invalid but is set to 0 on all entries
Frame #
valid-invalid bit
 Example:
1
1
1
1
0

0
0
page table
 if valid–invalid bit is 0  page fault
Operating System Concepts
10.6
Silberschatz, Galvin and Gagne 2002
Page Table When Some Pages Are Not in Main Memory
Operating System Concepts
10.7
Silberschatz, Galvin and Gagne 2002
Page Fault Procedure
1) first page reference will trap to OS  page fault
2) OS looks at another table to decide:
1) invalid reference  abort.
2) just not in memory
3) get empty frame
4) swap page into frame
5) reset tables, validation bit = 1
6) restart instruction
1) caution: is it restartable ?
Operating System Concepts
10.8
Silberschatz, Galvin and Gagne 2002
Steps in Handling a Page Fault
Operating System Concepts
10.9
Silberschatz, Galvin and Gagne 2002
What happens if there is no free frame?
 Page replacement
 find some page in memory, that is not in use
 Algorithm performance
 want an algorithm which will result in minimum number of
page faults
 Same pages may be paged in/out repeatedly
Operating System Concepts
10.10
Silberschatz, Galvin and Gagne 2002
Basic Page Replacement
1. Find the location of the desired page on disk
2. Find a free frame:
1. If there is a free frame, use it
2. If there is no free frame, use a page replacement
algorithm to select a victim frame
3. Read the desired page into the (newly) free frame.
Update the page and frame tables
4. Restart the process
Operating System Concepts
10.16
Silberschatz, Galvin and Gagne 2002
Page Replacement
Operating System Concepts
10.17
Silberschatz, Galvin and Gagne 2002
Page Replacement Algorithms
 Want lowest page-fault rate
 Evaluate algorithm:
 Consider series of memory references
(reference string)
 Compute the number of page faults
 Example: reference string
1, 2, 3, 4, 1, 2, 3, 1, 2, 3, 4, 2
Page number
Operating System Concepts
10.18
Silberschatz, Galvin and Gagne 2002
Graph of Page Faults Versus The Number of Frames
Operating System Concepts
10.19
Silberschatz, Galvin and Gagne 2002
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
3
3
2
4
1
1
5
4
2
2
1
5
3
3
2
4
4
3
9 page faults
 4 frames
Operating System Concepts
10.20
10 page faults
Silberschatz, Galvin and Gagne 2002
Optimal Algorithm
 Replace page that will not be used for longest 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 ? (predict future !)
 Used to measure quality of an algorithm
Operating System Concepts
10.22
Silberschatz, Galvin and Gagne 2002
Least Recently Used (LRU) Algorithm
 Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5
1
5
2
3
5
4
3
4
 Counter implementation
 Every page entry has a counter
 On page access, copy the clock into the counter
 When a page needs to be changed, compare counters
 Replace page with smallest counter
Operating System Concepts
10.23
Silberschatz, Galvin and Gagne 2002
Allocation of Frames
 Each process needs minimum number of pages
 Example:
IBM 370 – 6 pages for SS MOVE instruction:
 instruction is 6 bytes, might span 2 pages
 2 pages to handle from address
 2 pages to handle to address
 Two major allocation schemes:
 fixed allocation
 priority allocation
Operating System Concepts
10.28
Silberschatz, Galvin and Gagne 2002
Fixed Allocation
 Equal allocation
e.g., if 100 frames and 5 processes, give each 20
pages
 Proportional allocation,
allocate according to the size of process
si  size of process pi
m  64
si  10
S   si
s2  127
m  total number of frames
ai  allocation for pi
10
 64  5
137
127
a2 
 64  59
137
a1 
si
 m
S
Operating System Concepts
10.29
Silberschatz, Galvin and Gagne 2002
Priority Allocation
 Use a proportional allocation scheme using
priorities rather than size
 If process Pi generates a page fault,
 select for replacement one of its frames.
 select for replacement a frame from a process with
lower priority number
Operating System Concepts
10.30
Silberschatz, Galvin and Gagne 2002
Global vs. Local Allocation
 Global replacement
- process selects a replacement frame from
the set of all frames
- one process can take a frame from another
 Local replacement
- each process selects from only its own set
of frames
Operating System Concepts
10.31
Silberschatz, Galvin and Gagne 2002
Thrashing
 If a process does not have “enough” pages,
the page-fault rate is very high:
 process does page i/o
 low CPU utilization
 scheduler starts another process
 less memory per process
Operating System Concepts
10.32
Silberschatz, Galvin and Gagne 2002
Thrashing
 Why does paging work?
Locality model
 Process migrates from one locality to another
 Localities may overlap
 Why does thrashing occur?
 size of locality > total memory size
Operating System Concepts
10.33
Silberschatz, Galvin and Gagne 2002