本文以序列長度20的{ 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};以及頁面4;為例; 結果: ...
本文以序列長度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 }
結果: