COMP 530: Operating Systems
Page Replacement Algorithms
Don Porter Portions courtesy Emmett Witchel and Kevin Jeffay
1
Page Replacement Algorithms Don Porter Portions courtesy Emmett - - PowerPoint PPT Presentation
COMP 530: Operating Systems Page Replacement Algorithms Don Porter Portions courtesy Emmett Witchel and Kevin Jeffay 1 COMP 530: Operating Systems Virtual Memory Management: Recap Key concept: Demand paging User Program n Load
COMP 530: Operating Systems
1
COMP 530: Operating Systems
– Load pages into memory only when a page fault occurs
– Placement strategies
policy required
– Replacement strategies
than can fit in memory
– Load control strategies
in memory at one time
Operating System User Program 1 User Program 2 User Program 2 User Program n
Memory
COMP 530: Operating Systems
swapped out
– Handling a page fault now requires 2 disk accesses not 1! Which page should be replaced? Local replacement — Replace a page of the faulting process Global replacement — Possibly replace the page of another process
COMP 530: Operating Systems
– Example: (Virtual page, offset) address trace...
(3,0), (1,9), (4,1), (2,1), (5,3), (2,0), (1,9), (2,4), (3,1), (4,8)
– generates page trace
3, 1, 4, 2, 5, 2, 1, 2, 3, 4 (represented as c, a, d, b, e, b, a, b, c, d)
– Set a used bit in the page table entry – Increment or shift a register
Simulate the behavior of a page replacement algorithm on the trace and record the number of page faults generated fewer faults better performance
COMP 530: Operating Systems
future
c a d b e b a b c d
Faults
Page Frames 1 2 3 a b c d 1 2 3 4 5 6 7 8 9 10
Requests Time Time page needed next
Initial allocation
COMP 530: Operating Systems
future
c a d b e b a b c d a a a a a a a a a d b b b b b b b b b b c c c c c c c c c c
Faults
Frames d d d d e e e e e e 1 2 3 a b c d 1 2 3 4 5 6 7 8 9 10
Requests Time a = 7 b = 6 c = 9 d = 10 Time page needed next a = 15 b = 11 c = 13 d = 14
COMP 530: Operating Systems
– A single pointer suffices
c a d b e b a b c d
Faults
Page Frames 1 2 3 a b c d 1 2 3 4 5 6 7 8 9 10
Requests Time Physical Memory 1 2 Frame List
COMP 530: Operating Systems
– A single pointer suffices
c a d b e b a b c d a a a a e e e e e d b b b b b b a a a a c c c c c c c b b b
Faults
Frames d d d d d d d d c c 1 2 3 a b c d 1 2 3 4 5 6 7 8 9 10
Requests Time Physical Memory 2 3 Frame List
COMP 530: Operating Systems
c a d b e b a b c d
Faults
Page Frames 1 2 3 a b c d 1 2 3 4 5 6 7 8 9 10
Requests Time Time page last used
COMP 530: Operating Systems
c a d b e b a b c d a a a a a a a a a a b b b b b b b b b b c c c c e e e e e d
Faults
Frames d d d d d d d d c c 1 2 3 a b c d 1 2 3 4 5 6 7 8 9 10
Requests Time a = 2 b = 4 c = 1 d = 3 Time page last used a = 7 b = 8 e = 5 d = 3 a = 7 b = 8 e = 5 c = 9
COMP 530: Operating Systems
c a d b e b a b c d a a a a a a a a a a b b b b b b b b b b c c c c e e e e e d Faults
Frames
d d d d d d d d c c 1 2 3 a b c d 1 2 3 4 5 6 7 8 9 10 Requests Time LRU page stack Page to replace
COMP 530: Operating Systems
c a d b e b a b c d a a a a a a a a a a b b b b b b b b b b c c c c e e e e e d Faults
Frames
d d d d d d d d c c 1 2 3 a b c d 1 2 3 4 5 6 7 8 9 10 Requests Time c c a c a d c a d b a d b e a d e b d e b a d e a b e a b c a b c d LRU page stack Page to replace c d e
COMP 530: Operating Systems
– A. Make life easier for OS implementer – B. Reduce the number of page faults – C. Reduce the penalty for page faults when they
– D. Minimize CPU time of algorithm
COMP 530: Operating Systems
– Use a clock (or used/referenced) bit to track how often a page is accessed – The bit is set whenever a page is referenced
– Replace pages that haven’t been referenced for one complete revolution
func Clock_Replacement begin while (victim page not found) do if(used bit for current page = 0) then replace current page else reset used bit end if advance clock pointer end while end Clock_Replacement
resident bit used bit frame number
1
Page 7: 1
5
Page 1: 1
3
Page 4: 1
4 1
Page 0: 1
1 1
Page 3: 1
COMP 530: Operating Systems
d c b a c
Faults
Page Frames 1 2 3 a b c d
Requests Time Page table entries for resident pages:
1 d c b a a 2 d c b a d 3 d c b a b 4 e 5 b 6 a 7 b 8 c 9 d 10 1 1 1 1 a b c d
COMP 530: Operating Systems
d c b a c
Faults
Page Frames 1 2 3 a b c d
Requests Time Page table entries for resident pages:
1 d c b a a 2 d c b a d 3 d c b a b 4 d c b e e 5
c b e b 6 d a b e a 7
a b e b 8 c a b e c 9
a b d d 10
e b c d 1 1 e b c d 1 1 e b a d 1 1 1 e b a d 1 1 1 1 e b a c 1 d b a c 1 1 1 1 a b c d
COMP 530: Operating Systems
– Why?
sweep of the clock hand
– Use both the dirty bit and the used bit to drive replacement
1 Page 7: 1 5 Page 1: 1 3 Page 4: 1 4 1 Page 0: 1 9 1 Page 3: 1 1 1
Before clock sweep After clock sweep
used dirty
1 1 1 1
used dirty
1
replace page
Second Chance Algorithm
resident bit used bit frame number
COMP 530: Operating Systems
d c b a c
Faults
Page Frames 1 2 3 a b c d
Requests Time Page table entries for resident pages:
1 d c b a aw 2 d c b a d 3 d c b a bw 4 b 6 aw 7 b 8
10 10 10 10
a b c d e 5 c 9 d 10
COMP 530: Operating Systems
d c b a c
Faults
Page Frames 1 2 3 a b c d
Requests Time Page table entries for resident pages:
1 d c b a aw 2 d c b a d 3 d c b a bw 4 d e b a b 6 d e b a aw 7 d e b a b 8
00 00 10 00
a* b* e d
00 10 10 00
a b e d
11 10 10 00
a b e d
11 10 10 10
a b e c
00 10 00 00
a* d e c
10 10 10 10
a b c d
11 11 10 10
a b c d d e b a e 5
e b a c 9
e d a d 10
COMP 530: Operating Systems
Faults
Page Frames
1 2 3 a b c a b c d a b c d a b c d Faults
Page Frames
1 2 a b c 1 2 3 4 5 6 7 8 9 10 11 12 Requests Time –
COMP 530: Operating Systems
Faults
Page Frames
1 2 3 a b c a b c d a b c d a b c d a a a d d d c c c b b b b b b b a a a d d d c c c c c c c b b b a a a d Faults
Frames
1 2 a b c 1 2 3 4 5 6 7 8 9 10 11 12 Requests Time – a a a a a a a a a a a a b b b b b b b b b b b b c c c c c c c c c c c c d d d d d d d d d
COMP 530: Operating Systems
– LRU — Ages pages based on when they were last used – FIFO — Ages pages based on when they’re brought into memory
page frames allocated to processes
The principle of locality
Ø 90% of the execution of a program is sequential Ø Most iterative constructs consist of a relatively small number of instructions Ø When processing large data structures, the dominant cost is sequential processing on individual structure elements Ø Temporal vs. physical locality
COMP 530: Operating Systems
accesses
c c d b c e c e a d
Faults
Pages in Memory
Page a Page b Page c Page d
2 3 4 5 6 7 8 9 10
Requests Time Page e
t = -1
COMP 530: Operating Systems
c c d b c e c e a d
in Memory
Page a Page b Page c Page d
2 3 4 5 6 7 8 9 10
Requests Time
t = -1
accesses
COMP 530: Operating Systems
soon…
the working set)
– Thus pages may be removed even when no page fault occurs – The number of frames allocated to a process will vary over time
memory
– The working set model performs implicit load control
COMP 530: Operating Systems
– The pages referenced during the last t memory accesses are the working set – t is called the window size
c c d b c e c e a d
Faults
Pages in Memory
Page a Page b Page c Page d
2 3 4 5 6 7 8 9 10
Requests Time Page e
t = -1 t = -2
COMP 530: Operating Systems
c c d b c e c e a d
in Memory
Page a Page b Page c Page d
2 3 4 5 6 7 8 9 10
Requests Time
t = -1 t = -2
– The pages referenced during the last t memory accesses are the working set
– t is called the window size
COMP 530: Operating Systems
– When page fault frequency is high — increase working set – When page fault frequency is low — decrease working set Algorithm: Keep track of the rate at which faults occur When a fault occurs, compute the time since the last page fault Record the time, tlast, of the last page fault If the time between page faults is “large” then reduce the working set If tcurrent – tlast > t, then remove from memory all pages not referenced in [tlast, tcurrent ] If the time between page faults is “small” then increase working set If tcurrent – tlast ≤ t, then add faulting page to the working set
COMP 530: Operating Systems
the working set
tcur – tlast c c d b c e c e a d
Faults
Pages in Memory
Page a Page b Page c Page d
2 3 4 5 6 7 8 9 10
Requests Time Page e
COMP 530: Operating Systems
3
tcur – tlast
2 3 1
c c d b c e c e a d
in Memory
Page a Page b Page c Page d
2 3 4 5 6 7 8 9 10
Requests Time
the working set
COMP 530: Operating Systems
Issues Ø What criterion should be used to determine when to increase or decrease the MPL? Ø Which task should be swapped out if the MPL must be reduced? Low paging overhead Ø MPLmin = 1 process minimum number of frames required for a process to execute number of page frames Ø MPLmax =
COMP 530: Operating Systems
i.e., based on CPU utilization ◆ Assume memory is nearly full ◆ A chain of page faults occur
– A queue of processes forms at the paging device
◆ CPU utilization falls
– New processes fault, taking memory away from existing processes
System is thrashing — spending all of its time paging
I/O Device
Paging Device CPU
COMP 530: Operating Systems
Better criteria for load control: Adjust MPL so that: Ø mean time between page faults (MTBF) = page fault service time (PFST) Ø S WSi = size of memory
1.0
CPU Utilization Multiprogramming Level
Nmax NI/O-BALANCE MTBF PFST
1.0
COMP 530: Operating Systems
decreased, which process should be swapped
Suspended
suspended queue ready queue semaphore/condition queues
Waiting Running Ready
?
Paging Disk
Physical Memory Ø Lowest priority process? Ø Smallest process? Ø Largest process? Ø Oldest process? Ø Faulting process?