Page-Replacement Algorithms

advertisement
Page-Replacement Algorithms
A page replacement algorithm picks a page to
paged out and free up a frame
• FIFO — first-in, first-out
• Optimal — the one that leads to the least faults
• LRU — least-recently used
• LRU approximations
1
FIFO
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
7 0 1 2 2 3 0 4 2 3 0 0 0 1 2 2 2 7 0 1
7 0 1 1 2 3 0 4 2 3 3 3 0 1 1 1 2 7 0
7 0 0 1 2 3 0 4 2 2 2 3 0 0 0 1 2 7
1 2 3 4 1 2 5 1 2 3 4 5
1 2 3 4 1 2 5 5 5 3 4 4
1 2 3 4 1 2 2 2 5 3 3
1 2 3 4 1 1 1 2 5 5
1 2 3 4 1 2 5 1 2 3 4 5
1 2 3 4 4 4 5 1 2 3 4 5
1 2 3 3 3 4 5 1 2 3 4
1 2 2 2 3 4 5 1 2 3
1 1 1 2 3 4 5 1 2
Belady's
anomaly:
more frames
can be worse
2-5
Optimal
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 2 2 2 2 2 2 2 2 2 7 7 7
0 0 0 0 0 0 4 4 4 0 0 0 0 0 0 0 0 0 0
1 1 1 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1
1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 3 4 4
3 4 4 4 5 5 5 5 5 5
1 2 3 4 1 2 5 1 2 3 4 5
1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 4 4
4 4 4 5 5 5 5 5 5
But how do
you predict
the future?
6-7
LRU
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
70 71 72 20 21 22 23 40 41 42 00 01 02 10 11 12 10 11 12 10
00 01 02 00 01 00 01 02 30 31 30 31 32 33 00 01 02 00 01
10 11 12 30 31 32 20 21 22 23 20 21 20 21 22 70 71 72
1 2 3 4 1 2 5 1 2 3 4 5
10 11 12 40 41 42 50 51 52 30 31 32
20 21 22 10 11 12 10 11 12 40 41
30 31 32 20 21 22 20 21 22 50
1 2 3 4 1 2 5 1 2 3 4 5
10 11 12 13 10 11 12 10 11 12 13 50
20 21 22 23 20 21 22 20 21 22 23
30 31 32 33 50 51 52 53 40 41
40 41 42 43 44 45 30 31 32
Searching all
times is
expensive on
fault; keeping
a sorted list is
expensive on
access
8-9
Second Chance
7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
71 71 71 21 21 21 21 41 41 41 40 31 31 30 30 01 01 00 01 01
01 01 00 01 00 01 00 21 21 20 20 21 11 11 11 11 71 71 71
11 10 10 31 31 30 30 31 01 01 01 00 21 21 21 20 20 11
1 2 3 4 1 2 5 1 2 3 4 5
11 11 11 41 41 41 51 51 51 50 50 51
21 21 20 11 11 10 11 11 31 31 31
31 30 30 21 20 20 21 20 41 41
1 2 3 4 1 2 5 1 2 3 4 5
11 11 11 11 11 11 51 51 51 51 41 41
21 21 21 21 21 20 11 11 11 10 51
31 31 31 31 30 30 21 21 20 20
41 41 41 40 40 40 31 30 30
10
Second Chance
11
Enhanced Second Chance
Track both use and modifies (relative to copy on
disk):
• Not used, Not modified — good to replace
• Not used, Modified — ok, but have to write out
• Used, Not modified — rather keep it
• Used, Modified — really rather keep it
12
Allocating Frames
Frame allocation can be defined orthogonal to
page replacement:
• Allocation algorithm determines candidate pages
to evict
• Replacement algorithm picks a specific page to
evict
• Local allocation: each process has some frames
• Global allocation: frames shared among all
processes
13
Thrashing
#define SIZE (1024*1024*32)
int main (void)
{
while (1) {
char *x;
int i;
x = (char *) malloc (SIZE);
for (i=0; i<SIZE; i++) x[i] = 1;
fork();
}
}
14
Thrashing
15
Measuring Working Set
16
Interaction with I/O
• Some memory is used for a disk cache
• Communicating with an I/O device may require
physical memory ⇒ lock bits
• Memory-mapped files ⇒ shared memory
17
Download