Title: Virtual Memory
1(No Transcript)
2Virtual Memory
- Basic Concepts
- Demand Paging
- Page Replacement Algorithms
- Working-Set Model
3Last week in Memory Management
- Assumed all of a process is brought into memory
in order to run it. - Contiguous allocation
- Paging
- Segmentation
- What if we remove this assumption? What do we
gain/lose?
4Process address Space
5Virtual Memory
- Separation of user logical memory from physical
memory - Only part of the program needs to be in memory
for execution. - The logical address space can be much larger
than the physical address space. - Virtual memory can be implemented via
- Demand paging
- Demand segmentation
6Virtual Memory and Physical Memory
7Demand Paging
- When time to run a process, bring only a few
necessary pages into memory. While running the
process, bring a page into memory only when it is
needed (lazy swapping) - Less I/O needed
- Less memory needed
- Faster response
- Support more processes/users
- Page is needed
- If memory resident ? use the reference to page
from page table - If not in memory ? Page fault trap
8Valid-Invalid Bit
- How do we know if a page is in main memory or
not? - With each page table entry, a validinvalid bit
is associated (1 ? legal/in-memory, 0 ?
not-in-memory) -
- During address translation, if validinvalid bit
in page table entry is 0 ? page fault trap
9Use of the Page Table
10Handling a Page Fault
- If there is a reference to a page which is not
in memory, this reference will result in a trap
? page fault - Typically, the page table entry will contain the
address of the page on disk. - Major steps
- Locate empty frame
- Initiate disk I/O
- Move page (from disk) to the empty frame
- Update page table set validation bit to 1.
11Steps in Handling a Page Fault
12Impact of Page Faults
- Each page fault affects the system performance
negatively - The process experiencing the page fault will not
be able to continue until the missing page is
brought to the main memory - The process will be blocked (moved to the waiting
state) so its data (registers, etc.) must be
saved. - Dealing with the page fault involves disk I/O
- Increased demand to the disk drive
- Increased waiting time for process experiencing
the page fault
13Performance of Demand Paging
- Page Fault Rate p (0 ? p ? 1.0)
- if p 0, no page faults
- if p 1, every reference is a page fault
- Effective Access Time with Demand Paging
- (1 p) (effective memory access
time) - p (page fault overhead)
- Example
- Effective memory access time 100 nanoseconds
- page fault overhead 25 milliseconds
- p 0.001
- Effective Access Time with Demand Paging 25
microseconds -
14Page Replacement
- As we increase the degree of multi-programming,
over-allocation of memory becomes a problem. - What if we are unable to find a free frame at the
time of the page fault? - One solution Swap out a process (writing it back
to disk), free all its frames and reduce the
level of multiprogramming - May be good under some conditions
- Another option free a memory frame already in
use.
15Basic Page Replacement
- Find the location of the desired page on disk.
- Locate a free frame- If there is no free frame,
use a page replacement algorithm to select a
victim frame. - - Write the victim page back to the disk
update the page and frame tables accordingly. - Read the desired page into the free frame.Update
the page and frame tables. - Put the process (that experienced the page fault)
back to the ready queue.
16Page Replacement
17Page Replacement
- Observe If there are no free frames, two page
transfers needed at each page fault! - We can use a modify (dirty) bit to reduce
overhead of page transfers only modified pages
are written back to disk. - Page replacement completes the separation between
the logical memory and the physical memory
large virtual memory can be provided on a smaller
physical memory.
18Page Replacement Algorithms
- When page replacement is required, we must select
the frames that are to be replaced. - Primary Objective Use the algorithm with lowest
page-fault rate. - Efficiency
- Cost
- Evaluate algorithm by running it on a particular
string of memory references (reference string)
and computing the number of page faults on that
string. - We can generate reference strings artificially or
we can use specific traces.
19First-In-First-Out (FIFO) Algorithm
- Simplest page replacement algorithm.
- FIFO replacement algorithm chooses the oldest
page in the memory. - Implementation FIFO queue holds identifiers of
all the pages in memory. - We replace the page at the head of the queue.
- When a page is brought into memory, it is
inserted at the tail of the queue.
20FIFO Page Replacement
Consider what happens with the reference string
above (where each number corresponds to a
different page) Each of the first three
references generate a page fault and are brought
into memory Total faults 3
21FIFO Page Replacement
2 Page fault replace 7 0 no fault in
memory already Total faults 4
22FIFO Page Replacement
3 Page fault replace 0 0 Page fault - must
be brought back in to replace the 1 Total faults
6
23FIFO Page Replacement
4 Page fault replace 2 2 Page fault - must
be brought back in to replace the 3 3 Page
fault - must be brought back in to replace the
0 Total faults 9
24FIFO Page Replacement
0 Page fault replace 4 3 in memory 2 in
memory Total faults 10
25FIFO Page Replacement
1 Page fault replace 2 2 Page fault
replace 3 0 in memory Total faults 12
26FIFO Page Replacement
1 in memory 7 Page fault replace 0 0 Page
fault replace 1 1 Page fault replace 2 Total
faults 15
27FIFO Page Replacement
7
7
7
7
0
0
0
1
1
2
7 Page fault 0 Page fault 1 Page fault 2
Page fault Total faults 4
28FIFO Page Replacement
7
7
7
7
3
3
0
0
0
0
4
1
1
1
1
2
2
2
0 in memory 3 Page fault 0 in memory 4
Page fault Total faults 6
29FIFO Page Replacement
7
7
7
7
3
3
3
0
0
0
0
4
4
1
1
1
1
0
2
2
2
2
2 in memory 3 in memory 0 Page fault 3
in memory Total faults 7
30FIFO Page Replacement
7
7
7
7
3
3
3
3
2
0
0
0
0
4
4
4
4
1
1
1
1
0
0
0
2
2
2
2
1
1
2 in memory 1 Page fault 2 Page fault 0
in memory Total faults 9
31FIFO Page Replacement
7
7
7
7
3
3
3
3
2
2
0
0
0
0
4
4
4
4
7
1
1
1
1
0
0
0
0
2
2
2
2
1
1
1
1 in memory 7 Page fault 0 In memory 1
in memory Total faults 10
32Page Faults Versus The Number of Frames
- Usually, for a given reference string the number
of page faults decreases as we increase the
number of frames.
33FIFO Page Replacement
- But the oldest page may contain a heavily used
variable. - Will need to bring back that page in near future!
- Reference string 1, 2, 3, 4, 1, 2, 5, 1, 2, 3,
4, 5 - 3-frame case results in 9 page faults
- 4-frame case results in 10 page faults
-
- Program runs potentially slower with more memory!
- Beladys Anomaly
- more frames ? more page faults for some reference
strings!
34FIFO Page Replacement
4 faults
2
2
2
3
3
1
1
1
4
2
2
2
4 faults
3
3
35FIFO Page Replacement
5 faults
2
2
2
3
3
5
1
1
1
4
4
4
2
2
2
1
1
1
7 faults
3
3
3
2
2
36FIFO Page Replacement
8 faults
2
2
2
3
3
5
5
1
1
1
4
4
4
2
2
2
1
1
1
3
8 faults
3
3
3
2
2
2
37FIFO Page Replacement
10 faults
2
2
2
3
3
5
5
5
1
1
1
4
4
4
2
2
2
1
1
1
3
3
9 faults
3
3
3
2
2
2
4
38Optimal Page Replacement
- Is there an algorithm that yields the minimal
page fault rate for any reference string and a
fixed number of frames? - Replace the page that will not be used for the
longest period of time ? Algorithm OPT (MIN)
- Can be used as a yardstick for the performance of
other algorithms - What is the best we can do for the reference
string we have been looking at?
39Optimal Page Replacement
40Optimal Page Replacement
41Optimal Page Replacement
For this reference string, 9 faults is the best
we can do.
42Least-Recently-Used (LRU) Algorithm
- Idea Use the recent past as an approximation of
the near future. - Replace the page that has not been used for the
longest period of time ? Least-Recently-Used
(LRU) algorithm
43Least-Recently-Used (LRU) Algorithm
44Least-Recently-Used (LRU) Algorithm
45Least-Recently-Used (LRU) Algorithm
46LRU and Beladys Anomaly
- Neither OPT nor LRU suffers from Beladys
anomaly. - In general, LRU gives good (low) page-fault
rates. - Run-time overhead of the exact LRU
implementations is typically unacceptable. - Counters (work like timestamps)
- Stack when use a page, take it from the inside
of the stack (if there), and push it on the top.
LRU always on the bottom of the stack
47LRU Approximations
- Real systems often employ LRU approximation
algorithms, making use of the reference bits - Ex Additional-Reference-Bits Algorithm
- Each page keeps 8 bits initialized to 00000000
- If a page gets touched (used), its leftmost bit
gets changed to 1 - At some interval, the bits get shifted right
- Page with the lowest value at some point in time
is LRU - 00000000 page has not been touched for 8
intervals - 11111111 page has been touched every interval
for the last 8
48LRU Approximations
- Real systems often employ LRU approximation
algorithms, making use of the reference bits - Ex Second-Chance Algorithm (Clock Algorithm)
- FIFO with the addition of an extra reference bit
- When a page is used, its reference bit is set to
1 - When looking for a new page, use FIFO but skip
pages whose reference bit 1 - If a page gets skipped, its reference bit is
set to 0 and it is put at the end of the queue. - Can be enhanced by adding consideration of the
modification bit
49Second Chance Page Replacement
50Page-Buffering Algorithm
- In addition to a specific page-replacement
algorithm, other procedures are also used. - Systems commonly keep a pool of free frames.
- When a page fault occurs, the desired page is
read into a free frame first, allowing the
process to restart as soon as possible. The
victim page is written out to the disk later, and
its frame is added to the free-frame pool. - Other enhancements are also possible.
51Allocation of Frames
- How do we allocate the fixed number of free
memory among the various processes? - In addition to performance considerations, each
process needs a minimum number of pages,
determined by the instruction-set architecture. - Since a page fault causes an instruction to
restart, consider an architecture that contains
the instruction ADD M1, M2, M3 (with three
operands). - Consider also the case where indirect memory
references are allowed.
52Global vs. Local Allocation
- Page replacement algorithms can be implemented
broadly in two ways. - Global replacement process selects a
replacement frame from the set of all frames one
process can take a frame from another. - Under global allocation algorithms, the
page-fault rate of a given process depends also
on the paging behavior of other processes. - Local replacement each process selects from
only its own set of allocated frames. - Less used pages of memory are not made available
to a process that may need them.
53Memory Allocation for Local Replacement
- Equal allocation If we have n processes and m
frames, give each process m/n frames. - Proportional allocation Allocate according to
the size of process. - Priority of processes?
54CPU Utilization versusthe Degree of
Multiprogramming
55Thrashing
- High-paging activity The system is spending more
time paging than executing. - How can this happen?
- OS observes low CPU utilization and increases the
degree of multiprogramming. - Global page-replacement algorithm is used, it
takes away frames belonging to other processes - But these processes need those pages, they also
cause page faults. - Many processes join the waiting queue for the
paging device, CPU utilization further decreases.
- OS introduces new processes, further increasing
the paging activity.
56Thrashing
- To avoid thrashing, we must provide every process
in memory as many frames as it needs to run
without an excessive number of page faults. - Programs do not reference their address spaces
uniformly/randomly - A locality is a set of pages that are actively
used together. - According to the locality model, as a process
executes, it moves from locality to locality. - A program is generally composed of several
different localities, which may overlap.
57Locality in a Memory-Reference Pattern
58Working-Set Model
- Introduced by Peter Denning
- A model based on locality principle
- The parameter ?, defines the working set window
- The set of pages in the most recent ? page
references of process Pi constitutes the working
set
59Working-Set Model
- The accuracy of the working set depends on the
selection of ? - if ? is too small, it will not encompass the
entire locality. - if ? is too large, it will encompass several
localities. - if ? ? ? will encompass the entire program.
- D ? WSSi ? total demand of frames
- if D gt the number of frames in memory ? Thrashing
- The O.S. will monitor the working set of each
process and perform the frame allocation
accordingly. It may suspend processes if needed.
- Difficulty is how to keep track of the moving
working-set window.
60Working Sets and Page Fault Rates
61Controlling Page-Fault Rate
- Maintain acceptable page-fault rate.
- If actual rate too low, process loses frame.
- If actual rate too high, process gains frame.