FIFO页面置换算法
本文以序列长度20的{ 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};以及页面4;为例;
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,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1}; 7 8 void FIFO(int py[],int pg[]) 9 { 10 int i,q,j,table[InitPysiBlocks][MaxPages]; 11 char flag,f[MaxPages]; 12 for (i = 0; i < MaxPages; i++) 13 { 14 q = 0; 15 while(pg[i] != py[q] && q != InitPysiBlocks) 16 q++; 17 if(q == InitPysiBlocks) 18 flag = '*'; 19 else 20 flag = ' '; 21 if(flag == '*') 22 { 23 for (j = InitPysiBlocks - 1; j > 0; j--) 24 py[j] = py[j - 1]; 25 py[0] = pg[i]; 26 } 27 for (j = 0; j < InitPysiBlocks; j++) 28 table[j][i] = py[j]; 29 f[i] = flag; 30 } 31 printf("输出结果为下表(-1代表为空,*代表有缺页):\n"); 32 for(i = 0; i < InitPysiBlocks; i++) 33 { 34 for(j = 0; j < MaxPages; j++) 35 printf("%3d",table[i][j]); 36 printf("\n"); 37 } 38 for(i = 0; i < MaxPages; i++) 39 printf("%3c",f[i]); 40 printf("\n"); 41 } 42 43 void main() 44 { 45 FIFO(PysicalBlocks,PageSequence); 46 }
结果: