You are on page 1of 7

Virtual Memory Management

Fundamental issues : A Recap

Key concept: Demand paging User


User Program
Program nn
 Load pages into memory only when a page
fault occurs

...
Issues: User
User Program
Program 22
 Placement strategies
 Place pages anywhere – no placement policy User
required User Program
Program 11
Page Replacement Algorithms
 Replacement strategies “System
“System Software”
Software”
 What to do when there exist more jobs than
can fit in memory
Operating
Operating System
System
 Load control strategies
 Determining how many jobs can be
in memory at one time
Memory

1 2

Page Replacement Algorithms Page Replacement Algorithms


Concept Evaluation methodology

Typically Σi VASi >> Physical Memory Record a trace of the pages accessed by a process
 Example: (Virtual) address trace...
With demand paging, physical memory fills quickly (3,0), (1,9), (4,1), (2,1), (5,3), (2,0), (1,9), (2,4), (3,1), (4,8)
When a process faults & memory is full, some page must be  generates page trace
swapped out 3, 1, 4, 2, 5, 2, 1, 2, 3, 4 (represented as c , a, d, b, e, b, a , b, c, d)
 Handling a page fault now requires 2 disk accesses not 1!

Simulate the behavior of a page replacement algorithm on the


Which page should be replaced?
Local replacement — Replace a page of the faulting process trace and record the number of page faults generated
Global replacement — Possibly replace the page of another process fewer faults better performance

3 4
Optimal Page Replacement Optimal Page Replacement
Clairvoyant replacement Clairvoyant replacement

Replace the page that won’t be needed for the longest time in Replace the page that won’t be needed for the longest time in
the future the future

Time 0 1 2 3 4 5 6 7 8 9 10 Time 0 1 2 3 4 5 6 7 8 9 10
Requests c a d b e b a b c d Requests c a d b e b a b c d

0 a 0 a a a a a a a a a a d
Frames

Frames
1 b 1 b b b b b b b b b b b
Page

Page
2 c 2 c c c c c c c c c c c
3 d 3 d d d d d e e e e e e
Faults Faults • •
a=7 a = 15
Time page Time page b=6 b = 11
needed next needed next c=9 c = 13
d = 10 d = 14
5 6

Local Page Replacement Local Page Replacement


FIFO replacement FIFO replacement

Physical Physical
3 Simple to implement 3
Simple to implement Memory Memory
0 0
 A single pointer suffices 2  A single pointer suffices 2

Frame List Frame List


Performance with 4 page frames: Performance with 4 page frames:

Time 0 1 2 3 4 5 6 7 8 9 10 Time 0 1 2 3 4 5 6 7 8 9 10
Requests c a d b e b a b c d Requests c a d b e b a b c d
0 a 0 a a a a a e e e e e d
Frames

Frames
Page

Page

1 b 1 b b b b b b b a a a a
2 c 2 c c c c c c c c b b b
3 d 3 d d d d d d d d d c c
Faults Faults • • • • •
7 8
Least Recently Used Page Replacement Least Recently Used Page Replacement
Use the recent past as a predictor of the near future Use the recent past as a predictor of the near future

Replace the page that hasn’t been referenced for the longest Replace the page that hasn’t been referenced for the longest
time time

Time 0 1 2 3 4 5 6 7 8 9 10 Time 0 1 2 3 4 5 6 7 8 9 10
Requests c a d b e b a b c d Requests c a d b e b a b c d
0 a 0 a a a a a a a a a a a
Frames

Frames
Page

Page
1 b 1 b b b b b b b b b b b
2 c 2 c c c c c e e e e e d
3 d 3 d d d d d d d d d c c
Faults Faults • • •
a=2 a=7 a=7
Time page Time page b=4 b=8 b=8
last used last used c=1 e=5 e=5
d=3 d=3 c=9
9 10

Least Recently Used Page Replacement Least Recently Used Page Replacement
Implementation Implementation

Maintain a “stack” of recently used pages Maintain a “stack” of recently used pages

Time 0 1 2 3 4 5 6 7 8 9 10 Time 0 1 2 3 4 5 6 7 8 9 10
Requests c a d b e b a b c d Requests c a d b e b a b c d
0 a a a a a a a a a a a 0 a a a a a a a a a a a
Frames

Frames
Page

Page

1 b b b b b b b b b b b 1 b b b b b b b b b b b
2 c c c c c e e e e e d 2 c c c c c e e e e e d
3 d d d d d d d d d c c 3 d d d d d d d d d c c
Faults • • • Faults • • •

c a d b e b a b c d c a d b e b a b c d
LRU c a d b e b a b c LRU c a d b e b a b c
page stack page stack
c a d d e e a b c a d d e e a b
c a a d d e a c a a d d e a
Page to replace c d e Page to replace c d e
11 12
Least Recently Used Page Replacement Approximate LRU Page Replacement
Alternate Implementation --- Aging Register The Clock algorithm

Maintain a circular list of pages resident in memory


 Use a clock (or used/referenced ) bit to track how often a page is
Maintain an n-bit aging register R = Rn-1Rn-2 …R0 for each page accessed
frame  The bit is set whenever a page is referenced
 On a page reference, set Rn-1 to 1
Clock hand sweeps over pages looking for one with used bit = 0
 Every T units of time, shift the aging vector right by one bit  Replace pages that haven’t been referenced for one complete
revolution of the clock
Key idea: Page 7: 11 0 func Clock_Replacement
 Aging vector can be interpreted as a positive binary number begin
while (victim page not found) do
 Value of R decreases periodically unless the page is referenced
if(used bit for current page = 0) then
Page 1: 10 5 Page 4: 10 3 replace current page
else
Page replacement algorithm: reset used bit
 On a page fault, replace the page with the smallest value of R end if
advance clock pointer
Page 3: 11 1 Page 0: 11 4 end while
end Clock_Replacement
resident bit
used bit
13 frame number 14

Clock Page Replacement Clock Page Replacement


Example Example

Time 0 1 2 3 4 5 6 7 8 9 10 Time 0 1 2 3 4 5 6 7 8 9 10
Requests c a d b e b a b c d Requests c a d b e b a b c d

0 a a a a a 0 a a a a a e e e e e d
Frames

Frames

1 b b b b b 1 b b b b b b b b b b b
Page

Page

2 c c c c c 2 c c c c c c c a a a a
3 d d d d d 3 d d d d d d d d d c c
Faults Faults • • • •

1 a 1 e 1 e 1 e 1 e 1 e 1 d 1 a 1 e 1 e 1 e 1 e 1 e 1 d
Page table entries Page table entries
1 b 0 b 1 b 1 b 1 b 1 b 0 b 1 b 0 b 1 b 0 b 1 b 1 b 0 b
for resident pages: for resident pages:
1 c 0 c 0 c 1 a 1 a 1 a 0 a 1 c 0 c 0 c 1 a 1 a 1 a 0 a
1 d 0 d 0 d 0 d 0 d 1 c 0 c 1 d 0 d 0 d 0 d 0 d 1 c 0 c
15 16
Optimizing Approximate LRU Replacement The Second Chance Algorithm
The Second Chance algorithm Example

There is a significant cost to replacing “dirty” pages


Time 0 1 2 3 4 5 6 7 8 9 10
Modify the Clock algorithm to allow dirty pages to always survive Requests c aw d bw e b aw b c d
one sweep of the clock hand
 Use both the dirty bit and the used bit to drive replacement 0 a a a a a

Frames
1 b b b b b

Page
Page 7: 1 1 0 0 Second Chance Algorithm 2 c c c c c
Before clock After clock 3 d d d d d
sweep sweep
Page 1: 1 0 0 5 Page 4: 1 0 0 3 Faults
used dirty used dirty
0 0 replace page
10 a 11 a 00 a* 00 a* 11 a 11 a 00 a*
0 1 0 0 Page table
entries 10 b 11 b 00 b* 10 b* 10 b* 10 b*10 d
Page 3: 1 1 1 9 Page 0: 1 1 1 4 1 0 0 0
1 1 0 1 for resident 10 c 10 c 10 e 10 e 10 e 10 e 00 e
resident bit pages:
used bit 10 d 10 d 00 d 00 d 00 d 10 c 00 c
dirty bit 17 18

The Second Chance Algorithm The Problem With Local Page Replacement
Example How much memory do we allocate to a process?

0 1 2 3 4 5 6 7 8 9 10 Time 0 1 2 3 4 5 6 7 8 9 10 11 12
Time
Requests a b c d a b c d a b c d
Requests c aw d bw e b aw b c d

0 a a a a a a a a a a a 0 a
Frames
Page
Frames

1 b b b b b b b b b b d
Page

1 b
2 c c c c c e e e e e e 2 c
3 d d d d d d d d d c c Faults
Faults • • •
0 a
Frames
Page

a 10 11 a 00 a* 00 a* a 11 11 a 00 a* 1 b
Page table
entries 10 b 11 b 00 b* 10 b* 10 b* 10 b* 10 d 2 c
for resident 10 c 10 c 10 e 10 e 10 e 10 e 00 e 3 –
pages:
10 d 10 d 00 d 00 d 00 d 10 c 00 c Faults
19 20
The Problem With Local Page Replacement Page Replacement Algorithms
How much memory do we allocate to a process? Performance

Local page replacement


Time 0 1 2 3 4 5 6 7 8 9 10 11 12  LRU — Ages pages based on when they were last used
Requests a b c d a b c d a b c d  FIFO — Ages pages based on when they’re brought into
memory
0 a a a a d d d c c c b b b Towards global page replacement ... with variable number of
Frames
Page

1 b b b b b a a a d d d c c page frames allocated to processes


2 c c c c c c b b b a a a d The principle of locality
Faults • • • • • • • • •
 90% of the execution of a program is sequential
 Most iterative constructs consist of a relatively small number of
0 a a a a a a a a a a a a a
instructions
Frames

b b b b b b b b b b b b
Page

1 b
 When processing large data structures, the dominant cost is sequential
2 c c c c c c c c c c c c c processing on individual structure elements
3 – d d d d d d d d d

Faults •
21 22

Optimal Page Replacement Optimal Page Replacement


For processes with a variable number of frames For processes with a variable number of frames

VMIN — Replace a page that is not referenced in the next τ VMIN — Replace a page that is not referenced in the next τ
accesses accesses

Example: τ = 4 Example: τ = 4

Time 0 1 2 3 4 5 6 7 8 9 10 Time 0 1 2 3 4 5 6 7 8 9 10
Requests c c d b c e c e a d Requests c c d b c e c e a d
Page a • Page a • - - - - - - - - F -
in Memory

in Memory

t=0 t=0
Page b - Page b - - - - F - - - - - -
Pages

Pages

Page c - Page c - F • • • • • • - - -
Page d •
t = -1
Page d •
t = -1
• • • - - - - - - F
Page e - Page e - - - - - - F • • - -
Faults Faults • • • • •

23 24
Explicitly Using Locality Working Set Page Replacement
The working set model of page replacement Implementation

Keep track of the last τ references


Assume recently referenced pages are likely to be referenced  The pages referenced during the last τ memory accesses are
again soon… the working set
 τ is called the window size
... and only keep those pages recently referenced in memory Example: Working set computation, τ = 4 references:
(called 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 Time 0 1 2 3 4 5 6 7 8 9 10
Requests c c d b c e c e a d
A process is allowed to execute only if its working set fits into
Page a •t = 0

in Memory
memory Page b -

Pages
 The working set model performs implicit load control Page c -
Page d t•= -1
Page e t•= -2

Faults
25 26

Working Set Page Replacement


Implementation

Keep track of the last τ references


 The pages referenced during the last τ memory accesses are
the working set
 τ is called the window size

Example: Working set computation, τ = 4 references:

Time 0 1 2 3 4 5 6 7 8 9 10
Requests c c d b c e c e a d
Page a •t = 0 • • • - - - - - F •
in Memory

Page b - - - - F • • • - - -
Pages

Page c - F • • • • • • • • •
Page d t•= -1 • • • • • • - - - F
Page e t•= -2 • - - - - F • • • •
Faults • • • • •
27

You might also like