LRU page replacement algorithm and lru replacement algorithm
In this article, the {,} of the sequence length 20, and page 4; are used as an example;
1 # include <stdio. h> 2 3 # define InitPysiBlocks 4 4 # define MaxPages 20 5 int PysicalBlocks [InitPysiBlocks] = {-1,-1,-1,-1 }; 6 int PageSequence [MaxPages] = {,}; 7 8 void LRU (int py [], int pg []) 9 {10 int I, j, k, m, flag [MaxPages], temp [MaxPages] [InitPysiBlocks]; 11 int max = 0; 12 char f [MaxPages]; 13 for (I = 0; I <InitPysiBlocks; I ++) 14 {15 py [I] = pg [I]; 16 flag [I] = I; 17 f [I] = '*'; 18 for (j = 0; j <InitPysiBlocks; j ++) 19 temp [I] [j] = py [j]; 20} 21 for (I = InitPysiBlocks; I <MaxPages; I ++) 22 {23 for (j = 0, k = 0; j <InitPysiBlocks; j ++) 24 {25 if (py [j]! = Pg [I]) 26 k ++; 27 else28 flag [j] = I; 29} 30 if (k = InitPysiBlocks) 31 {32 f [I] = '*'; 33 max = flag [0] <flag [1]?; 34 for (m = 2; m <InitPysiBlocks; m ++) 35 if (flag [m] <flag [max]) 36 max = m; 37 py [max] = pg [I]; 38 flag [max] = I; 39 for (j = 0; j <InitPysiBlocks; j ++) 40 temp [I] [j] = py [j]; 41} 42 else43 {44 f [I] = ''; 45 for (j = 0; j <InitPysiBlocks; j ++) 46 temp [I] [j] = py [j]; 47} 48} 49 printf ("the output result is the following table (-1 indicates null, * Indicates missing pages): \ n "); 50 for (I = 0; I <InitPysiBlocks; I ++) 51 {52 for (j = 0; j <MaxPages; j ++) 53 printf ("% 3d", temp [j] [I]); 54 printf ("\ n"); 55} 56 for (I = 0; I <MaxPages; I ++) 57 printf ("% 3c", f [I]); 58 printf ("\ n"); 59} 60 61 void main () 62 {63 LRU (PysicalBlocks, PageSequence); 64}
Test: