replacement algorithm

12
Performance of Demand Paging Page Fault Rate: 0 p 1.0 Effective Access Time (EAT) EAT = (1 – p) x memory access time + p x page fault time e.g. P = 0.1 Memory access time = 200 ns page fault time = 8 ms EAT = (1 – 0.1) x 200 + 0.1 x 8000000 ns Why The page fault is very expensive? The page fault rate significantly affect the System performance reduce page fault rate P !

Upload: arch-sidz

Post on 29-Jul-2015

47 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Replacement algorithm

Performance of Demand Paging

• Page Fault Rate: 0 p 1.0• Effective Access Time (EAT)

EAT = (1 – p) x memory access time + p x page fault time

e.g.• P = 0.1• Memory access time = 200 ns• page fault time = 8 ms

EAT = (1 – 0.1) x 200 + 0.1 x 8000000 ns

Why The page fault is very expensive?

The page fault rate significantly affect theSystem performance reduce page fault rate P !

Page 2: Replacement algorithm

Page Replacement

Basic page replacement algorithm1. Find the location of the desired page on disk2. Find a free frame:

if there is a free frame, use it if there is no free frame, use a page replacement

algorithm to select a victim frame. Check if the victim frame is modified (“dirty”). If it’s dirty, write back to disk

3. Load the demand page from disk into the (newly) free frame. Update the page and frame tables

4. Restart the interrupted process(page replacement diagram is next …)

Page 3: Replacement algorithm

Page Replacement Algorithms

• The goal of page replacement algorithm: lowest page-fault rate

• Evaluate algorithm by running it on a particular string of memory references (reference string) and computing the number of page faults

• E.g.

If The address sequence is

1, 1, 1, 2, 3, 3, 4, 1, 2, 2, 2, 2, 5, 1, 1, 2, 2, 3, 4, 4, 4, 5, – What is the reference string?– Do we need to know other conditions to compute the

number of page faults?

Page 4: Replacement algorithm

Page Faults vs. the Number of Frames

• Ans: also need to know the number of frames available• There are two major problems to implement demand

paging:– Page replacement: how to select victim frame– Frame allocation: how many frames to allocate to each

process (discuss after finishing page replacement part)

number of Page faults

Vs.

number of frames

(general case)

Page 5: Replacement algorithm

FIFO Page Replacement Algorithm

• Always replace the oldest page – A FIFO queue• Easy to implement, but the performance is not always good.

e.g. 1

Reference string: 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 2, 1, 2, 0, 1, 7, 0, 1

(Pure demand paging)

3 pages in memory at a time per process

e.g. 2

Reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

3 pages in memory at a time per process

vs.

4 pages in memory at a time per process

Page 6: Replacement algorithm

FIFO Illustrating Belady’s Anomaly

Belady’s Anomaly: for some page replacement algorithm, the page fault rate may increase as the number of allocated frames increases

Page 7: Replacement algorithm

Optimal (OPT) Page Replacement Algorithm

• OPT has the lowest page fault rate: replace page that will not be used for longest period of time

• 4 frames example 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

How do you know which page will not be used for longest period of time?

• A benchmark used for measuring how well other page replacement algorithm performs

1

2

3

4

4 5

Page 8: Replacement algorithm

Least-Recently Used (LRU) Algorithm

• Motivation: – We cannot know the future– Use recent past as approximation of the near future

• LRU algorithm is an approximation of OPT algorithm– Choose the page that has not been used for the longest

period of time to replace– Like OPT, LRU does NOT suffer from Belady’s anomaly

• E.g. reference string: 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5

• Two implementations of LRU– Counter implementation– Stack implementation

1

2

3

4

Page 9: Replacement algorithm

LRU Algorithm Implementations• Counter implementation

– Every page entry has a counter; every time page is referenced through this entry, copy the clock (time stamp) into the counter

– When a page needs to be changed, look at the counters to find the smallest time stamp -- determine which are to change

• Stack implementation – keep a stack of page numbers in a double linked list:

– When a page is referenced (example is on the next slide)

» move it to the top» requires 6 pointers to be changed

– No search for replacement• Problem with both implementations

– Need additional hardware support– Expensive housekeeping is required at each memory

reference. Interrupt handling overheadAny solutions ?

Page 10: Replacement algorithm

An Example of Stack Implementation of LRU

Page 11: Replacement algorithm

Allocation of Frames• How does OS allocate the fixed amount of free memory

(frames) among the various processes?• Simple frame allocation algorithm: in a single user system, OS

takes some frames, the rest of frames are assigned to a user process

– Some variations:» Demand paging on the buffer and table space of OS» Always reserve a couple of free frames in free frame list

Why?

• Allocate at least a minimum number of frames for each process

defined by the computer architecture

e.g. In IBM 370 MVC instruction– instruction might span 2 pages– 2 pages to handle from– 2 pages to handle to

Hence minimum 6 pages are needed.What if there are total 5 frames in this system?

Page 12: Replacement algorithm

Fixed Allocation Algorithm

• Equal allocation If there are 100 frames and 5 processes, give each process

20 frames.• Proportional allocation – Allocate according to the size of

process

mSs

pa

m

sS

ps

iii

i

ii

for allocation

frames of number total

process of size

5964137127

56413710

127

10

64

2

1

2

a

a

s

s

m

i