FIFO页面置换算法

时间:2023-02-01 19:45:33

本文以序列长度20的{ 7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};以及页面4;为例;

 #include <stdio.h>

 #define InitPysiBlocks 4
#define MaxPages 20
int PysicalBlocks[InitPysiBlocks] = { -,-,-,- };
int PageSequence[MaxPages] = { ,,,,,,,,,,,,,,,,,,,}; void FIFO(int py[],int pg[])
{
int i,q,j,table[InitPysiBlocks][MaxPages];
char flag,f[MaxPages];
for (i = ; i < MaxPages; i++)
{
q = ;
while(pg[i] != py[q] && q != InitPysiBlocks)
q++;
if(q == InitPysiBlocks)
flag = '*';
else
flag = ' ';
if(flag == '*')
{
for (j = InitPysiBlocks - ; j > ; j--)
py[j] = py[j - ];
py[] = pg[i];
}
for (j = ; j < InitPysiBlocks; j++)
table[j][i] = py[j];
f[i] = flag;
}
printf("输出结果为下表(-1代表为空,*代表有缺页):\n");
for(i = ; i < InitPysiBlocks; i++)
{
for(j = ; j < MaxPages; j++)
printf("%3d",table[i][j]);
printf("\n");
}
for(i = ; i < MaxPages; i++)
printf("%3c",f[i]);
printf("\n");
} void main()
{
FIFO(PysicalBlocks,PageSequence);
}

结果:

FIFO页面置换算法