memory management virtual memory page replacement algorithms

29
1 Memory Management Virtual Memory Page replacement algorithms Chapter 4 Sec. 4.4

Upload: vaughan-garza

Post on 03-Jan-2016

48 views

Category:

Documents


0 download

DESCRIPTION

Memory Management Virtual Memory Page replacement algorithms. Chapter 4 Sec. 4.4. Structure of a page table entry. Issues with page tables. They can be very large It takes more time to access a memory location Virtual page first has to be mapped to its corresponding physical frame - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Memory Management Virtual Memory Page replacement algorithms

1

Memory ManagementVirtual Memory

Page replacement algorithms

Chapter 4

Sec. 4.4

Page 2: Memory Management Virtual Memory Page replacement algorithms

2

Structure of a page table entry

Page 3: Memory Management Virtual Memory Page replacement algorithms

3

Issues with page tables

• They can be very large• It takes more time to access a memory

location– Virtual page first has to be mapped to its

corresponding physical frame

• The spectrum of solutions– High speed registers to implement the table– Put entire page table in main memory

Page 4: Memory Management Virtual Memory Page replacement algorithms

4

Page swapping

• What to do if the memory reference is to a page that is not in main memory?– This is called a page fault– “Swap” a page out of main memory and “swap”

the needed page in its place

• The decision about which page to swap out is made by a page replacement algorithm

Page 5: Memory Management Virtual Memory Page replacement algorithms

5

When a page fault occurs…

• Page fault forces choice about which page should be overwritten

• Modified page may first have to be saved to the hard drive– But if is unmodified, it can just be overwritten

• Best not to choose “popular” page– It will probably need to be brought back in soon

Page 6: Memory Management Virtual Memory Page replacement algorithms

6

Page replacement algorithms

• Choosing the page that gets swapped out– Optimal– NRU– FIFO– Second Chance– Clock– LRU– NFU– Working Set– WSClock

Page 7: Memory Management Virtual Memory Page replacement algorithms

7

Optimal Page Replacement Algorithm

• Replace page that will be needed at the farthest point in future– Optimal but unrealizable

• Can estimate by logging page use on previous runs of a given process– Impractical

Page 8: Memory Management Virtual Memory Page replacement algorithms

8

Optimal algorithm exampleUsing 3 page frames

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 2 7 0 0 0 0 4 0 0 0 1 1 3 3 3 1 1

Total number of page faults is ___ . This algorithm (although the best) is implausible to implement since it requires future knowledge. It is used mainly for comparison studies.

Page 9: Memory Management Virtual Memory Page replacement algorithms

9

Not Recently Used (NRU)

• Each page has reference and modified bits– bits are set when page is referenced, modified

• Pages are classified not referenced, not modified: 0 0 not referenced, modified: 0 1 how can it be?

referenced, not modified: 1 0 referenced, modified: 1 1

• Removes page at random from the lowest numbered non-empty class

Page 10: Memory Management Virtual Memory Page replacement algorithms

10

FIFO

• Maintain a linked list of all pages – in order in which they came into memory

• The page at the head of the list swapped out

• Disadvantage?• Advantage?

Page 11: Memory Management Virtual Memory Page replacement algorithms

11

FIFO exampleUsing 3 frames

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 4 4 4 0 0 0 7 7 7 0 0 0 3 3 3 2 2 2 1 1 1 0 0 1 1 1 0 0 0 3 3 3 2 2 2 1

Total of ___ faults.

Suppose we had 4 frames instead of 3, would there be fewer page faults?

Page 12: Memory Management Virtual Memory Page replacement algorithms

12

Another FIFO example

Consider the reference string

1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

With three frames there are faults

With four frames there are faults.

This is an example of Belady's Anomaly.

Page 13: Memory Management Virtual Memory Page replacement algorithms

13

Belady's Anomaly?!?

P's show which page references show page faults

FIFO with 3 page frames

FIFO with 4 page frames

Page 14: Memory Management Virtual Memory Page replacement algorithms

14

Second ChanceFIFO modification

• A FIFO linked list is maintained, as before.– The page at the head of the list is chosen to be swapped

out unless its R bit is 0• In that case, it goes to the end of the list• And the next process on the list is examined for swapping…

Page 15: Memory Management Virtual Memory Page replacement algorithms

15

ClockVariation of second chance

Page 16: Memory Management Virtual Memory Page replacement algorithms

16

Least Recently Used (LRU)

• Rationale is that pages used recently will be used again soon– replace pages that have been unused for longest time

• If implemented with a linked list of pages– the list must be updated with every memory reference

• Other techniques require hardware support– Counter is maintained

• When a page is referenced, it receives the counter’s value

– LRU matrix

Page 17: Memory Management Virtual Memory Page replacement algorithms

17

LRU matrix

LRU using a matrix – pages referenced in order

0,1,2,3,2,1,0,3,2,3

Page 18: Memory Management Virtual Memory Page replacement algorithms

18

LRU example

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 4 4 0 1 1 1 0 0 0 0 0 0 3 3 3 0 0 1 1 3 3 2 2 2 2 2 7

Total number of page faults is _____. This is often the best page-replacement algorithm that we can use.

Page 19: Memory Management Virtual Memory Page replacement algorithms

19

Additional reference bits (LRU enhancement )

• Instead of just clearing the bits every so often, we can keep extra information by having a reference byte

• At regular intervals (100 ms, e.g.) a clock interrupt occurs– OS shifts the reference bit into the high order

position of the reference byte, and shifts all other bits down to the right

Page 20: Memory Management Virtual Memory Page replacement algorithms

20

Additional reference bits example

• For example, the reference byte is 01101101 and the reference bit is 1 when the timer expires.

• The reference byte now becomes 10110110, and the reference bit is set back to 0

• Interpreting these bytes as unsigned integers, the least recently referenced page is the one with the smallest reference byte.

Page 21: Memory Management Virtual Memory Page replacement algorithms

21

Not frequently used (NFU)

• Counter in each page table entry is initialized to 0• At each clock interrupt, the value of the reference

bit is added to the counter, and then set back to 0.• At a page fault, the page with the smallest counter

is swapped out.• Not very useful, but a variation of it accounts for

the aging of pages.– This is a simulation of LRU

– See pp. 220-221

Page 22: Memory Management Virtual Memory Page replacement algorithms

22

Working Set / Locality Model• The working-set strategy starts by looking

at how many frames a process is actually using.

• This approach defines the locality model of process execution– As a process executes, it moves from one

locality to another. • A locality is a set of pages actively used together

– A program is composed of several (possibly overlapping) localities

Page 23: Memory Management Virtual Memory Page replacement algorithms

23

What is locality?

• For example, when a procedure is called, it defines a new locality. When the procedure is exited, the locality changes

• If we allocate enough frames to accommodate the process’ current locality– it will fault for the pages in its current locality

until they are all in memory. – it will not fault again until it changes its

locality.

Page 24: Memory Management Virtual Memory Page replacement algorithms

24

Working-Set Model• This model uses a parameter, to define the

working-set window• The idea is to examine the most recent page

references. The set of pages in the most recent page references is the working set– If a page is in active use, it will be in the

working set– If it is no longer being used, it will drop

from the working set units after its last reference

Page 25: Memory Management Virtual Memory Page replacement algorithms

25

ExampleA page reference string, with= 10 2 6 1 5 7 7 7 7 5 1 6 2 3 4 1 2 3 4 4 4 3 4 3 4 4 4

t1 t2

At time t1 the working set is {1,2,5,6,7}At time t2 the working set is {3, 4}

The accuracy of the working set depends upon the selection of if it is too small, it may not encompass the working set if it is too large, it may overlap several localities

Page 26: Memory Management Virtual Memory Page replacement algorithms

26

Implementation• The OS monitors the working-set of each

process and allocates to that working-set enough frames to provide it with its working-set size– If there are any extra frames, another process

can be started.

• If the sum of the working-sets increases, exceeding the total number of frames, a process is suspended. That process’ pages are written out and its frames reallocated.

Page 27: Memory Management Virtual Memory Page replacement algorithms

27

Working Set

Page 28: Memory Management Virtual Memory Page replacement algorithms

28

WSClock

Page 29: Memory Management Virtual Memory Page replacement algorithms

29

Summary