Lecture 22

advertisement
Lecture 22



Process Management Project handed back
Reminders: no class on Friday; Homework 5
due Wednesday after spring break
Questions?
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
1
Outline

Page replacement algorithms

Frame allocation

Thrashing
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
2
Replacement Algorithms


Recall: Criteria for choosing includes:

Low page fault rate

Efficient in choosing victim frame
Evaluate by simulating scenario data that is a
string of logical page references with a fixed
number of available physical frames. For most
examples will use 3 physical frames.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
3
FIFO Replacement

As usual, simplest algorithm is FIFO (first in,
first out). Associate a time with each frame.
Victim frame is the oldest one. Can be
implemented using a basic queue.
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2 2 2 2 4 4 4 0
0 0 0 0 3 3 3 2 2 2
1 1 1 1 0 0 0 3 3
*

*
*
*
*
*
*
*
*
*
Easy to understand, but doesn't always give
th
good performance. E.g., 5 fault (page 3).
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
4
Belady's Anomaly


Consider following reference string with 3
physical frames that results in 9 faults:
1
2
3
4
1
2
5
1
1
1
4
4
4
2
2
2
1
3
3
3
1
2
3
4
5
5
5
1
1
3
3
2
2
2
4
5
Do it again with 4 frames; results in 10 faults!
1
2
3
4
1
1
1
1
2
2
2
3
3
1
2
5
1
2
3
4
5
4
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
5
OPT Replacement

OPT is the optimal algorithm - replace the page
that will be used farthest into the future.
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2
2
2
0 0 0
0
4
1 1
3
3
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
6
OPT Replacement



OPT is provably optimal for a finite reference
string.
Of course, generally do not know the entire
reference string, but it is good to know a
theoretical minimum so we can say things like
"at worst within 12% of optimal" or "4.7% of
optimal on average".
OPT is unimplementable in real systems, so
need to approximate. FIFO is not good.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
7
LRU Replacement

LRU chooses the "least recently used" page as
the victim page. The theory is to use recent
past usage as a predictor of new future usage.
It replaces the page that has not been used
for the longest time.
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 7 7 2
2
4
0 0 0
0
0
1 1
3
3
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
8
LRU Replacement

The main problem for LRU is how to implement
it. There are a couple of feasible
implementations


Counters: use CPU time counter and store it into
the PT on each reference. Replace the lowest
numbered entry. Need to search PT. Need to deal
with context switches and counter rollover.
"Stack": keep a "stack" of page references. When a
page is referenced, move it to the top of stack.
Victim is bottom of stack. Since want to remove
from middle, usually use doubly-linked list.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
9
Other Replacement Algorithms

Counting algorithms - replacement based on
frequency of use; increment a page counter
each time referenced




LFU ("Least Frequently Used") - victim page is the
one that has the lowest counter
MFU ("Most Frequently Used") - victim page is the
one that has the highest counter
Why should we expect these might work?
Not too common, since they do not
approximate OPT very well.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
10
Other Replacement Algorithms


LRU approximation - many architectures do
not provide sufficient hardware for true LRU,
often providing only one reference bit that is
initially clear (value 0) and set (value 1) when a
page is referenced. When a victim is needed,
can tell which pages have been used/not used,
but not exact order.
Extend to multiple reference bits. Periodically
shift right with 0 added to left end. References
set the leftmost bit.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
11
Other Replacement Algorithms


For example, if there are 8 reference bits, get a
history of reference usage in 8 time periods.

11111111 - refs in each of the last 8 time periods

00000011 - refs in far past

11000000 - refs in recent past
When viewed as an integer, pages with recent
references have a larger reference number, so
page with the smallest reference number is the
LRU pick. Note that this number is not
necessarily unique.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
12
Other Replacement Algorithms

Second chance algorithm - can use a single
reference bit in the following way to make FIFO
a better algorithm. Keep track of a current
"head" of page list. When a victim needs to be
selected do the following:


Check the head page, if reference bit is 0, replace it
and advance the head pointer.
If the reference bit is 1 ("recently used"), clear bit to
0 and skip. Repeat until find a 0.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
13
Other Replacement Algorithms


Worst case, all reference bits are set and
algorithm degenerates to FIFO after each page
has been given a second chance. If a page is
used often enough, its reference bit likely will
be 1 and it stays in.
May also want to take the dirty bit into
consideration, since dirty pages are more
expensive to replace.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
14
Frame Allocation


To speed up page replacement, some systems
reserve a few free frames at all times (called a
buffer pool), so that there is always a free
frame when a page fault occurs.
Selection of the victim page and swapping out
is done concurrently with the page fault service.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
15
Frame Allocation


How is the fixed amount of free memory
allocated among the various processes?
Consider simplest case of a single-user system.
For example, 128KB of memory divided into
128 1KB pages. The OS may need as many as
35 frames, leaving a minimum of 93 frames for
the user process.


What if the OS does not use 35 frames at all times?
Should the OS compete with user process?
If not, what would be a fair division of the frames?
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
16
Frame Allocation


In general, what would a fair division of frames
among multiple processes?
Some things to note:


Maximum allocation is the maximum of the system
Performance is better with more frames. In
particular, for any particular architecture, can
determine the minimum number of frames needed
to avoid really bad performance. E.g., PDP-11
move instruction could straddle two pages and have
two indirect address operands, thus requiring 6
pages for one instruction.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
17
Frame Allocation


What are some ways to divide m frames among
n processes?
How do we make the division "fair"?
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
18
Frame Allocation


Equal allocation gives equal-sized shares to
each process. I.e. m/n frames per process.
Any leftover frames can be used as the buffer
pool. Continuing the example, 93 frames
divided among 5 processes gives 18
frames/process and a 3 frame buffer pool.
Of course, not all programs are the same size.
Suppose 2 processes, one is 10KB (like an
editor) and another is 127KB (like a database).
Conceptually, "wastes" 36 (93/2 - 10) frames.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
19
Frame Allocation

Can do proportional allocation instead, where
allocation is based on size of each process' VM
size as follows:

Let the VM size of process Pi be si.

Define S = sum of all si

Allocate ai frames to Pi where ai = si / S x m
Or the minimum number of frames, whichever is
larger.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
20
Frame Allocation



Continuing the example, this would allocate
10/137 x 93 = 7 frames to the 10KB process
and 127/137 x 93 = 86 frames to the 127KB
process.
The actual allocation will depend on the amount
of multiprogramming. Also additional
processes reduce the allocation to existing
processes and vice versa.
Could also proportionally allocated on the basis
of priority or other factors rather than size.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
21
Global vs. Local Replacement



In addition to allocation, need to decide how to
handler page replacement when there are
multiple processes.
Global replacement - choose victim from the
set of all frames, even ones currently allocated
to another process
Local replacement - choose victim from set of
frames currently allocated to the process.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
22
Global vs. Local Replacement



Local replacement seems "fairer", but if frames
are unused, results in lower utilization
Global replacement causes the page fault rate
of a process to be affected by other processes.
It also can cause a process to lose "too many"
pages and fall below the minimum, especially in
priority-based schemes.
But generally, global replacement increases
throughput, so is more commonly used (and is
what is to be simulated in the final project).
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
23
Thrashing



When a process loses too many pages under
global replacement, the system should suspend
the process by swapping it out and releasing its
remaining frames.
Later the process can be swapped back in and
resumed. The intermediate CPU scheduler
handles this.
"Not enough" is simply more active pages than
allocated frames. Then each new reference
causes a page fault that throws out a page that
will be used soon.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
24
Thrashing


This high paging activity is called thrashing.
The process/system spends more time paging
than executing. CPU utilization is very low.
Cause of thrashing is:


Too much multiprogramming - processes keep
stealing frames from each other, or the fixed
number of frames is not enough for current activity.
Even worse, some systems may interpret low CPU
utilization as a need to increase multiprogramming.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
25
Thrashing

Difference is that there is very high disk activity
when process/system is thrashing. (Causes a
"thrashing" noise as disk seeks from frame to
frame on the backing store.)
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
26
Thrashing



When a local replacement strategy is used,
thrashing may be limited to one process.
However, this may still affect overall paging
performance, since the backing store is shared
by all processes.
The only way to deal with thrashing is to reduce
the amount of multiprogramming. I.e., suspend
or abort some processes to release memory
resources.
Wednesday, February 28
CS 470 Operating Systems - Lecture 22
27
Download