You are on page 1of 7

Virtual Memory Management

Fundamental issues : A Recap

Key concept: Demand paging

Issues:

Placement strategies

User Program 2 User Program 2 User Program 1 User Program 1 System Software System Software Operating System Operating System

Page Replacement Algorithms

Place pages anywhere no placement policy required What to do when there exist more jobs than can fit in memory

Replacement strategies

Load control strategies

Determining how many jobs can be in memory at one time

Memory

...

Load pages into memory only when a page fault occurs

User Program n User Program n

Page Replacement Algorithms


Concept

Page Replacement Algorithms


Evaluation methodology

Typically

i VASi >> Physical Memory

Record a trace of the pages accessed by a process


Example: (Virtual) address trace...
(3,0), (1,9), (4,1), (2,1), (5,3), (2,0), (1,9), (2,4), (3,1), (4,8)

With demand paging, physical memory fills quickly When a process faults & memory is full, some page must be swapped out
Handling a page fault now requires 2 disk accesses not 1!

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)

Which page should be replaced?

Local replacement Replace a page of the faulting process Global replacement Possibly replace the page of another process

Simulate the behavior of a page replacement algorithm on the trace and record the number of page faults generated
fewer faults better performance

Optimal Page Replacement


Clairvoyant replacement

Optimal Page Replacement


Clairvoyant replacement

Replace the page that wont be needed for the longest time in the future Time Requests

Replace the page that wont be needed for the longest time in the future Time Requests

0 0 1 2 3 a b c d

1 c

2 a

3 d

4 b

5 e

6 b

7 a

8 b

9 c

10 d

0 0 1 2 3 a b c d

1 c a b c d

2 a a b c d

3 d a b c d

4 b a b c d
a=7 b=6 c=9 d = 10

5 e a b c e

6 b a b c e

7 a a b c e

8 b a b c e

9 c a b c e
a = 15 b = 11 c = 13 d = 14

10 d d b c e

Page Frames

Faults Time page needed next


5

Page Frames
Faults

Time page needed next

Local Page Replacement


FIFO replacement 3 0 2
Frame List Physical Memory

Local Page Replacement


FIFO replacement 3 0 2
Frame List Physical Memory

Simple to implement
A single pointer suffices

Simple to implement
A single pointer suffices

Performance with 4 page frames:

Performance with 4 page frames:

Time Requests

0 0 1 2 3 a b c d

1 c

2 a

3 d

4 b

5 e

6 b

7 a

8 b

9 c

10 d

Time Requests

0 0 1 2 3 a b c d

1 c a b c d

2 a a b c d

3 d a b c d

4 b a b c d

5 e e b c d

6 b e b c d

7 a e a c d

8 b e a b d

9 c e a b c

10 d d a b c

Page Frames

Page Frames

Faults
7

Faults

Least Recently Used Page Replacement


Use the recent past as a predictor of the near future Replace the page that hasnt been referenced for the longest time

Least Recently Used Page Replacement


Use the recent past as a predictor of the near future Replace the page that hasnt been referenced for the longest time

Time Requests

0 0 1 2 3 a b c d

1 c

2 a

3 d

4 b

5 e

6 b

7 a

8 b

9 c

10 d

Time Requests

0 0 1 2 3 a b c d

1 c a b c d

2 a a b c d

3 d a b c d

4 b a b c d
a=2 b=4 c=1 d=3

5 e a b e d

6 b a b e d

7 a a b e d

8 b a b e d
a=7 b=8 e=5 d=3

9 c a b e c

10 d a b d c

Page Frames

Page Frames

Faults Time page last used


9

Faults Time page last used

a=7 b=8 e=5 c=9

10

Least Recently Used Page Replacement


Implementation Maintain a stack of recently used pages Time Requests 0 1 2 3 a b c d 0 1 c a b c d 2 a a b c d 3 d a b c d 4 b a b c d 5 e a b e d 6 b a b e d 7 a a b e d 8 b a b e d 9 c a b e c 10 d a b d c

Least Recently Used Page Replacement


Implementation Maintain a stack of recently used pages Time Requests 0 1 2 3 a b c d 0 1 c a b c d 2 a a b c d 3 d a b c d 4 b a b c d 5 e a b e d 6 b a b e d 7 a a b e d 8 b a b e d 9 c a b e c 10 d a b d c

Page Frames

Faults c a c d a c b d a c

e b d a c b e d a a b e d b a e d

c b a e d

d c b a e
11

Page Frames

Faults c a c d a c b d a c

e b d a c b e d a a b e d b a e d

c b a e d

d c b a e
12

LRU page stack

LRU page stack

Page to replace

Page to replace

Least Recently Used Page Replacement


Alternate Implementation --- Aging Register

Approximate LRU Page Replacement


The Clock algorithm Maintain a circular list of pages resident in memory

Maintain an n-bit aging register R = Rn-1Rn-2 R0 for each page frame


On a page reference, set Rn-1 to 1 Every T units of time, shift the aging vector right by one bit

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 havent been referenced for one complete revolution of the clock Page 7:

Clock hand sweeps over pages looking for one with used bit = 0

Key idea:
Aging vector can be interpreted as a positive binary number Value of R decreases periodically unless the page is referenced Page 1:

11 0
Page 4:

10 5

10 3

Page replacement algorithm:


On a page fault, replace the page with the smallest value of R Page 3:
resident bit used bit frame number

11 1

Page 0:

11 4

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
14

13

Clock Page Replacement


Example

Clock Page Replacement


Example

Time Requests

0 0 1 2 3 a b c d

1 2 c a a b c d a b c d

3 d a b c d

4 b a b c d

5 e

6 b

7 a

8 b

9 c

10 d

Time Requests

0 0 1 2 3 a b c d

1 2 c a a b c d a b c d

3 d a b c d

4 b a b c d

5 e e b c d 1 0 0 0 e b c d 1 1 0 0

6 b e b c d

7 a e b a d e b c d 1 0 1 0 e b a d 1 1 1 0

8 b e b a d

9 c e b a c 1 1 1 1 e b a c

10 d d b a c 1 0 0 0 d b a c
16

Page Frames

Faults

Page table entries for resident pages:

1 1 1 1

a b c d

1 0 0 0

e b c d

1 1 0 0

e b c d

1 1 1 0

e b a d

1 1 1 0

e b a d

1 1 1 1

e b a c

1 0 0 0

d b a c
15

Page Frames
Faults

Page table entries for resident pages:

1 1 1 1

a b c d

e b a d

Optimizing Approximate LRU Replacement


The Second Chance algorithm There is a significant cost to replacing dirty pages Modify the Clock algorithm to allow dirty pages to always survive one sweep of the clock hand
Use both the dirty bit and the used bit to drive replacement

The Second Chance Algorithm


Example

Time Requests

0 0 1 2 3 a b c d

1 2 3 c aw d a b c d a b c d a b c d

4 bw a b c d

5 e

6 b

7 aw

8 b

9 c

10 d

Page 7: 1 1 0 0

Second Chance Algorithm


Before clock sweep
used dirty

Page 1: 1 0 0 5

Page 4: 1 0 0 3

After clock sweep


used dirty replace page

Page 3: 1 1 1
resident bit used bit dirty bit

Page 0: 1 1 1 4

0 0 1 1

0 1 0 1

Page Frames
Faults

0 0 0

0 0 1
17

10 Page table entries 10 for resident 10 pages: 10

a b c d

a b 10 c 10 d
11 11

00 a* 00 b* 10 00

00 a* 10 b*

11

10 b*

e 10 e 10 e d 00 d 00 d

a 00 a* 10 d 10 e 00 e 10 c 00 c
11 10 b*
18

The Second Chance Algorithm


Example

The Problem With Local Page Replacement


How much memory do we allocate to a process?

Time Requests

0 0 1 2 3 a b c d

1 2 3 c aw d a b c d a b c d a b c d

4 bw a b c d

5 e a b e d
00 a*

6 b a b e d

7 aw a b e d

8 b a b e d

9 c a b e c a 10 b* 10 e 10 c
11

10 d a d e c
00 a*

Time Requests 0 1 2

1 a

2 b

3 c

4 d

5 a

6 b

7 c

8 d

9 a

10 b

11 c

12 d

Page Frames

Page Frames
Faults

a b c

Faults

a Page table entries 10 b for resident 10 c pages: 10 d


10

a 11 b 10 c 10 d
11

a 00 b* 10 b* 10 b* 10 e 10 e 10 e 00 d 00 d 00 d
11

00 a*

Page Frames
Faults
19

0 1 2 3

a b c
20

d 00 e 00 c
10

The Problem With Local Page Replacement


How much memory do we allocate to a process?

Page Replacement Algorithms


Performance

Local page replacement Time Requests 0 1 2 0 1 a a b c 2 b a b c 3 c a b c 4 d d b c 5 a d a c 6 b d a b 7 c c a b 8 d c d b 9 a c d a 10 b b d a 11 c b c a 12 d b c d


LRU Ages pages based on when they were last used FIFO Ages pages based on when theyre brought into memory

Page Frames

a b c

Towards global page replacement ... with variable number of 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

Faults 0 1 2 3 Faults a b c a b c a b c a b c

a b c d

a b c d

a b c d

a b c d

a b c d

a b c d

a b c d

a b c d

a b c d
21

Page Frames

22

Optimal Page Replacement


For processes with a variable number of frames

Optimal Page Replacement


For processes with a variable number of frames

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


Example: = 4

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


Example: = 4

Time Requests Page a Page b Page c Page d Page e

0 t = -1 t=0

1 c

2 c

3 d

4 b

5 c

6 e

7 c

8 e

9 a

10 d

Time Requests Page a Page b Page c Page d Page e

0 t = -1 t=0

1 c
F -

2 c
-

3 d
-

4 b
F -

5 c
-

6 e
F

7 c

8 e

9 a
F -

10 d
F -

Pages in Memory

Pages in Memory

Faults
23

Faults

24

Explicitly Using Locality


The working set model of page replacement

Working Set Page Replacement


Implementation Keep track of the last references

Assume recently referenced pages are likely to be referenced again soon ... and only keep those pages recently referenced in memory (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

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

Example: Working set computation, = 4 references:

Time Requests Page a Page b Page c Page d Page e

0 = 0 t t -1 = t -2 =

The working set model performs implicit load control

25

Pages in Memory

A process is allowed to execute only if its working set fits into memory

1 c

2 c

3 d

4 b

5 c

6 e

7 c

8 e

9 a

10 d

Faults
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 Requests Page a Page b Page c Page d Page e

0 = 0 t t -1 = t -2 =

1 c
F

2 c
-

3 d
-

4 b
F -

5 c
-

6 e
F

7 c

8 e

9 a
F

10 d
F

Pages in Memory

Faults

27

You might also like