9.4 page replacement what if there is no free frame? page replacement –find some page in memory,...

28
9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same page may be brought into memory several times

Upload: samantha-plante

Post on 31-Mar-2015

213 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

9.4 Page Replacement What if there is no free frame?

Page replacement –find some page in memory, but not really in use, swap it out

In this case, same page may be brought into memory several times

Page 2: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Basic Page Replacement

Page 3: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Page Replacement

Page 4: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Page Replacement Algorithms Goal: Want 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 on that string

In all our examples, the reference string is

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

Page 5: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

FIFO When a page must be replaced, the oldest

page is chosen

In all our examples, the reference string is 1, 2, 3, 4, 1, 2, 5, 1, 2, 3, 4, 5 3 frame (9 page faults) 4 frame (10 page faults)

Notice that the number of faults for 4 frames is greater than the umber of faults for 3 frames!! This unexpected result is known as Belady’s anomaly

Page 6: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

FIFO Illustrating Belady’s Anomaly

Page 7: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

FIFO Algorithm

Page 8: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Optimal Page-Replacement Algorithm

Replace page that will not be used for longest period of time

This is a design to guarantee the lowest page-fault rate for a fixed number of frames

Page 9: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Optimal Page-Replacement Algorithm

Page 10: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Optimal Page-Replacement Algorithm

Page 11: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Optimal Page-Replacement Algorithm

Unfortunately, the optimal page-replacement is difficult to implement, because it requires future knowledge of the reference string

Page 12: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Least-recently-used (LRU) algorithm

LRU replacement associates with each page the time of that page’s last use

When a page must be replaced, LRU chooses the page that has not been used for the longest period of time

Page 13: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Least-recently-used (LRU) algorithm

Page 14: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Least-recently-used (LRU) algorithm

Page 15: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Least-recently-used (LRU) algorithm The major problem is how to implement LRU

replacement:1. Counter: whenever a reference to a page is

made, the content of the clock register are copied to the time-of-use filed in the page table entry for the page. We replace the page with the smallest time value

2. Stack: Whenever a page is referenced, it is removed from the stack and put on the top. In this way, the most recently used page is always at the top of the stack

Page 16: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Stack implementation

Page 17: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Stack implementation

Note that both implementation of LRU requires hardware support

If we were to use an interrupt for every reference to allow software to update such data, it would slow every memory reference

Page 18: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

LRU Approximation Page Replacement The system without hardware support provide

some help for LRU, in a form of a reference bit

Initially the bits are cleared (to 0) by the OS. As a user process executes, the bit associated with each page reference is set (to 1) by the hardware

After a while, the clear step is activated

In this method, although we can not get the order of page reference, we can know that whether the page is referenced or not

Page 19: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Second-Chance Algorithm Basically, it’s a FIFO algorithm When a page has been selected, we

inspect its reference bit. If the value is 0, we proceed to replace

this page, otherwise, we give the page a second chance and move on to select the next FIFO page

When a page get a second chance, it’s reference bit is cleared, and its arrival time is reset to the current time

Page 20: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Second-Chance Algorithm

When a page get a second chance, it’s reference bit is cleared, and its arrival time is reset to the current time

If a page is used often enough to keep its reference bit set, it will never be replaced

Page 21: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Second-Chance Algorithm One way to implement the second-

chance algorithm is as a circular queue

In the worst case, when all bits are set, the pointer cycles through the whole queue, giving each page a second chance

Enhanced second-chance algorithm

Page 22: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Second-Chance Algorithm

Page 23: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Additional-Reference-Bits Algorithm We can gain additional ordering

information by recording the reference bits at regular intervals.

We can keep 8-bit byte for each page in a table in memory

The OS shifts the reference bit for each page into the high-order bit of its 8-bit byte, shifting the other bit right by 1 bit and discard the low-order bit

Page 24: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Additional-Reference-Bits Algorithm

For example: 00000000 means the page has not been used for eight-time interval

11111111 means a page that is used at least once in each period

11000100 has used more recently with both value of 01110111 and 00111111

Page 25: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Counting Based Page Replacement

Least Frequently used (LFU) page-replacement algorithm

Most frequently used (MFU) page-replacement algorithm

Page 26: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Page Buffering Algorithm

We can keep some amount of frames (for example 3 frames) always free

When a page fault occurs, the system does not have to wait the execution of replace algorithm.

The system can load the page into free space and swapped out frames

Page 27: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Allocation of Frames

When we have more than one processor, we need to allocate frames to each processor

Two major algorithms: 1. equal allocation 2. proportional allocation

Page 28: 9.4 Page Replacement What if there is no free frame? Page replacement –find some page in memory, but not really in use, swap it out In this case, same

Allocation of Frames