You are on page 1of 8

Page-Replacement Algorithms

A page replacement algorithm picks a page to paged out and free up a frame
FIFO rst-in, rst-out
Optimal the one that leads to the least faults
LRU least-recently used
LRU approximations
1
FIFO
77
007
1107
2210
0210
3321
0032
4403
2240
3324
0032
3032
2032
1103
2210
0210
1210
7721
0072
1107
11

221
3321
4432
1143
2214
5521
1521
2521
3352
4435
5435
11
221
3321
44321
14321
24321
55432
11543
22154
33215
44321
55432
Belady's anomaly: more frames can be worse
2-5
Optimal
77
070
1701

2201
0201
3203
0203
4243
2243
3243
0203
3203
2203
1201
2201
0201
1201
7701
0701
1701
11
212
3123
4124
1124
2124
5125
1125
2125
3135
4145
5145

11
212
3123
41234
11234
21234
51235
11235
21235
31235
41245
51245
But how do you predict the future?
6-7
LRU
7 70
0 71 00
1 72 01 10
2 20 02 11
0 21 00 12
3 22 01 30
0 23 00 31
4 40 01 32
2 41 02 20
3 42 30 21
0 00 31 22
3 01 30 23
2 02 31 20
1 10 32 21

2 11 33 20
0 12 00 21
1 10 01 22
7 11 02 70
0 12 00 71
1 10 01 72
1 10
2 11 20
3 12 21 30
4 40 22 31
1 41 10 32
2 42 11 20
5 50 12 21
1 51 10 22
2 52 11 20
3 30 12 21
4 31 40 22
5 32 41 50
1 10
2 11 20
3 12 21 30
4 13 22 31 40
1 10 23 32 41
2 11 20 33 42
5 12 21 50 43
1 10 22 51 44
2 11 20 52 45
3 12 21 53 30
4 13 22 40 31

5 50 23 41 32
Searching all times is expensive on fault; keeping a sorted list is expensive on access
8-9
Second Chance
7 71
0 71 01
1 71 01 11
2 21 00 10
0 21 01 10
3 21 00 31
0 21 01 31
4 41 00 30
2 41 21 30
3 41 21 31
0 40 20 01
3 31 20 01
2 31 21 01
1 30 11 00
2 30 11 21
0 01 11 21
1 01 11 21
7 00 71 20
0 01 71 20
1 01 71 11
1 11
2 11 21
3 11 21 31
4 41 20 30
1 41 11 30

2 41 11 21
5 51 10 20
1 51 11 20
2 51 11 21
3 50 31 20
4 50 31 41
5 51 31 41
1 11
2 11 21
3 11 21 31
4 11 21 31 41
1 11 21 31 41
2 11 21 31 41
5 51 20 30 40
1 51 11 30 40
2 51 11 21 40
3 51 11 21 31
4 41 10 20 30
5 41 51 20 30
10
Second Chance
11
Enhanced Second Chance
Track both use and modies (relative to copy on disk):
Not used, Not modied good to replace
Not used, Modied ok, but have to write out
Used, Not modied rather keep it
Used, Modied really rather keep it
12

Allocating Frames
Frame allocation can be dened orthogonal to page replacement:
Allocation algorithm determines candidate pages to evict
Replacement algorithm picks a specic 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 les shared memory
17

You might also like