page replacement algorithms

advertisement
CSC468/2204 TUTORIAL – WEEK 8
by Tristan Miller -- Updated 7 November 2000
OVERVIEW OF TODAY’S TUTORIAL
 announcements regarding A2Q1
 page replacement
 overview of A2Q2
 page replacement algorithms
 questions & answers
ANNOUNCEMENTS REGARDING A2Q1
 clarifications regarding previous tutorial (briefly):
o for FCFS tie-breaking rule, select the process that has been in
the system the longest
o job service and interarrival times are figured as real numbers,
not integers – after you generate the random numbers, do not
round off to the nearest integer
o except for round-robin, preemptions should be performed only
when new processes arrive
o the HRRN algorithm presented in tutorial was a preemptive
version;
the
assignment
calls
for
a
non-preemptive
implementation
 all the above clarifications are explained in detail on the newsgroup
 tutorial
notes
are
now
available
on
http://www.cs.utoronto.ca/~psy/teaching/2000/468f/
the
web
at
 suggested reading list for discrete event simulation is available on the
newsgroup
 diagram of discrete event simulation:
Generate service
time, s1, for job j1.
Generate
interarrival time,
t2, for job j2.
Generate
interarrival time,
t0, for job j0
n
t i
t0
0
i 0
t
t1+t0
Generate service
time, s0, for job j0.
Generate
interarrival time,
t1, for job j1.
Generate service time, sn,
for job jn. Generate
interarrival time, tn+1, for
job jn+1.
 non-preemptive schedulers need to recompute priorities only when a
process finishes executing
 except for round-robin, preemptive schedulers need to recompute
priorities only a process arrives or finishes
 for round-robin, the scheduler needs to recompute priorities when a
process finishes or one quantum after a process begins executing
(whichever is sooner)
 your simulation can conveniently ignore the time between events –
just advance the clock to the next event (i.e. a process arrival, a
process completion, or the end of a round-robin quantum) instead of
waiting
 remember, no scheduling algorithm, including round robin, will allow
the CPU to remain idle while there are waiting jobs
PAGE REPLACEMENT
 each process is allocated frames (memory) which hold the process’s
pages (data)
 frames are filled with pages as needed – this is called demand paging
 over-allocation of memory is prevented by modifying the page-fault
service routine to replace pages
 the job of the page replacement algorithm is to decide which page gets
victimized to make room for a new page
 page replacement completes separation of logical and physical
memory
OVERVIEW OF A2Q2
 given a certain memory size and a sequence of page numbers (the
“reference string”), use each of three different page replacement
algorithms to determine the performance
 calculate, tabulate, and graph page faults vs. number of frames
 the performance calculations should be done with as few passes of the
reference string as possible – i.e. even though there are nine different
test cases (3 algorithms  3 parameters each), you may not have to run
a simulation nine times
PAGE REPLACEMENT ALGORITHMS
Optimal algorithm
 ideally we want to select an algorithm with the lowest page-fault rate
 such an algorithm exists, and is called (unsurprisingly) the optimal
algorithm:
 procedure: replace the page that will not be used for the longest time
(or at all) – i.e. replace the page with the greatest forward distance in
the reference string
 example using 4 frames:
Reference #
1
2
3
4
5
6
7
8
9 10 11 12
Page referenced
1
2
3
4
1
2
5
1
2
3
4
5
Frames
1
1
1
1
1
1
1
1
1
1
4
4
2
2
2
2
2
2
2
2
2
2
2
3
3
3
3
3
3
3
3
3
3
4
4
4
5
5
5
5
5
5
_ = faulting page
 analysis: 12 page references, 6 page faults, 2 page replacements.
Page faults per number of frames = 6/4 = 1.5
 unfortunately, the optimal algorithm requires special hardware (crystal
ball, magic mirror, etc.) not typically found on today’s computers
 optimal algorithm is still used as a metric for judging other page
replacement algorithms
FIFO algorithm
 replaces pages based on their order of arrival: oldest page is replaced
 example using 4 frames:
Reference #
1
2
3
4
5
6
7
8
9 10 11 12
Page referenced
1
2
3
4
1
2
5
1
2
3
4
5
Frames
1
1
1
1
1
1
5
5
5
5
4
4
2
2
2
2
2
2
1
1
1
1
5
3
3
3
3
3
3
2
2
2
2
4
4
4
4
4
4
3
3
3
_ = faulting page
 analysis: 12 page references, 10 page faults, 6 page replacements.
Page faults per number of frames = 10/4 = 2.5
LFU algorithm (page-based)
 procedure: replace the page which has been referenced least often
 for each page in the reference string, we need to keep a reference
count. All reference counts start at 0 and are incremented every time
a page is referenced.

example using 4 frames:
Reference #
1
2
3
4
5
6
7
8
9 10 11 12
Page referenced
1
2
3
4
1
2
5
1
2
Frames
_ = faulting page
n
= reference count
1
1
3
4
5
1
1
1
2
2
2
3
3
3
3
3
1
1
1
1
2
2
2
3
3
3
3
1
1
1
1
1
1
1
2
2
2
1
1
1
1
1
1
1
2
2
1
2
1
2
3
1
2
3
4
1
2
3
4
1
2
3
4
1
2
5
4
1
2
5
4
1
2
5
4
1
2
3
4
1
2
3
4
1
2
5
4
 at the 7th page in the reference string, we need to select a page to be
victimized. Either 3 or 4 will do since they have the same reference
count (1). Let’s pick 3.
 likewise at the 10th page reference; pages 4 and 5 have been
referenced once each. Let’s pick page 4 to victimize. Page 3 is
brought in, and its reference count (which was 1 before we paged it
out a while ago) is updated to 2.
 analysis: 12 page references, 7 page faults, 3 page replacements.
Page faults per number of frames = 7/4 = 1.75
LFU algorithm (frame-based)
 procedure: replace the page in the frame which has been referenced
least often
 need to keep a reference count for each frame which is initialized to 1
when the page is paged in, incremented every time the page in the
frame is referenced, and reset every time the page in the frame is
replaced
 example using 4 frames:
Reference #
1
2
3
4
5
6
7
8
9 10 11 12
Page referenced
1
2
3
4
1
2
5
1
2
Frames
_ = faulting page
n
= reference count
1
1
3
4
5
1
1
1
2
2
2
3
3
3
3
3
1
1
1
1
2
2
2
3
3
3
3
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
2
2
1
2
1
2
3
1
2
3
4
1
2
3
4
1
2
3
4
1
2
5
4
1
2
5
4
1
2
5
4
1
2
3
4
1
2
3
4
1
2
5
4
 at the 7th reference, we victimize the page in the frame which has
been referenced least often -- in this case, pages 3 and 4 (contained
within frames 3 and 4) are candidates, each with a reference count of
1. Let’s pick the page in frame 3. Page 5 is paged in and frame 3’s
reference count is reset to 1.
 at the 10th reference, we again have a page fault. Pages 5 and 4
(contained within frames 3 and 4) are candidates, each with a count of
1. Let’s pick page 4. Page 3 is paged into frame 3, and frame 3’s
reference count is reset to 1.
 analysis: 12 page references, 7 page faults, 3 page replacements.
Page faults per number of frames = 7/4 = 1.75
LRU algorithm
 replaces pages based on their most recent reference – replace the page
with the greatest backward distance in the reference string
 example using 4 frames:
Reference #
1
2
3
4
5
6
7
8
9 10 11 12
Page referenced
1
2
3
4
1
2
5
1
2
3
4
5
Frames
1
1
1
1
1
1
1
1
1
1
1
5
2
2
2
2
2
2
2
2
2
2
2
3
3
3
3
5
5
5
5
4
4
4
4
4
4
4
4
3
3
3
_ = faulting page
 analysis: 12 page references, 8 page faults, 4 page replacements.
Page faults per number of frames = 8/4 = 2
 one possible implementation (not necessarily the best):
o every frame has a time field; every time a page is referenced,
copy the current time into its frame’s time field
o when a page needs to be replaced, look at the time stamps to
find the oldest
PFF algorithm
 thus far, all the algorithms presented assume each process is granted a
fixed amount of memory (i.e. number of frames)
 in the real world, different processes have different memory
requirements
 allocating too few frames to a process may result in that process
thrashing (rapid concentrated page replacement)
 allocating too many frames to a process may result in other processes
thrashing
 PFF aims to prevent thrashing by allocating or deallocating frames as
required
 procedure:
o every frame has a reference bit (initially 0)
o whenever a page is referenced, set its frame’s reference bit
o when a page fault occurs, compare the IFT (inter-fault time)
with a certain threshold
o if IFT < threshold, allocate a new frame to the process and reset
all reference bits
o if IFT  threshold, deallocate all frames whose reference bit is
not set, allocate a new frame for the faulting page, and reset all
reference bits
 example with threshold = 3:
Reference #
1
2
3
4
5
6
7
8
9 10 11 12
Page referenced
1
2
3
4
1
2
5
1
2
3
4
5
Frames
1
1
1
1 *1 *1
1 *1 *1
1
1
1
2
2
2
2 *2
2
2 *2
2
2
2
3
3
3
3
4
4
4
5
5
3
3
3
4
4
* = reference bit set
_ = faulting page
5
5
 since the number of frames allocated to a process is dynamic with this
algorithm, when performing the analysis, use the mean number of
frames in use per reference
 in the above example, there were 39 frames in use over 12 page
references, so the mean number of frames is 39/12 = 3.25
 analysis for the above example: 12 page references, 8 page faults.
Page faults per number of frames = 8/3.25  2.4615
Download