操作系统先进先出置换算法(FIFO)实现
先进先出置换算法(FIFO):每次选择淘汰的页面是最早进入内存的页面实现方法:把调入内存的页面根据调入的先后顺序排成一个队列,需要换出页面时选择队头页面即可。队列的最大长度取决于系统为进程分配了多少个内存块代码如下:#include <iostream>#include<stdlib.h>//#include<conio.h>#include<stdio.
·
先进先出置换算法(FIFO):每次选择淘汰的页面是最早进入内存的页面
实现方法:把调入内存的页面根据调入的先后顺序排成一个队列,需要换出页面时选择队头页面即可。
队列的最大长度取决于系统为进程分配了多少个内存块
代码如下:
#include <iostream>
#include<stdlib.h>
//#include<conio.h>
#include<stdio.h>
#define size 4
using namespace std;
typedef struct BLOCK//声明一种新类型——物理块类型
{
int pagenum;//页号
int accessed;//访问字段,其值表示多久未被访问
}BLOCK;
int count;//程序计数器,用来记录指令的序号
int n;//缺页计数器,用来记录缺页的次数
static int temp[320];//用来存储320条随机数
BLOCK block[size]; //定义一大小为4的物理块数组
void init( ); //程序初始化函数
int findExist(int curpage);//查找物理块中是否有该页面
int findSpace( );//查找是否有空闲物理块
int findReplace( );//查找应予置换的页面
void display ( );//显示
void Random( );//产生320条随机数,显示并存储到temp[320]
void pagestring( );//显示调用的页面队列
void FIFO( );//FIFO算法
void init( )
{
block[0].pagenum = 0;
block[1].pagenum = 1;
block[2].pagenum = 2;
block[3].pagenum = 3;
for(int i=0;i<size;i++)
{
//block[i].pagenum=-1;
block[i].accessed=0;
count=n=0;
}
}
int findExist(int curpage) //查找物理块中是否有该页面
{
for(int i=0; i<size; i++)
{
if(block[i].pagenum == curpage )
return i; //检测到内存中有该页面,返回block中的位置
}
return -1;
}
int findSpace( ) //查找是否有空闲物理块
{
for(int i=0; i<size; i++)
{
if(block[i].pagenum == -1)
return i; //找到空闲的block,返回block中的位置
}
return -1;
}
int findReplace( ) //查找应予置换的页面
{
int pos = 0;
for(int i=0; i<size; i++)
{
if(block[i].accessed >block[pos].accessed)
pos = i;//找到应予置换页面,返回BLOCK中位置
}
return pos;
}
void display( )
{
for(int i=0; i<size; i++)
{
if(block[i].pagenum != -1) //物理块不空
{
printf(" %02d",block[i].pagenum);
}
}
cout<<endl;
}
void Random()
{
int flag=0;
temp[0]=0;
temp[1]=40;
temp[2]=10;
temp[3]=50;
temp[4]=20;
temp[5]=10;
temp[6]=30;
temp[7]=20;
temp[8]=0;
temp[9]=40;
temp[10]=60;
temp[11]=60;
}
void pagestring( ) //显示调用的页面队列
{
for(int i=0;i<12;i++)
{
printf(" %02d",temp[i]/10);
if((i+1)%10==0) cout<<endl;
}
}
//--------------- 先进先出算法
void FIFO( )
{
int exist,space,position ;
int curpage;
for(int i=0;i<12;i++)
{
//if(i%100==0) getch( ); //getch直接从键盘获取键值
count=temp[i];
curpage=count/10;
exist = findExist(curpage); //查找物理块中是否有该页面
if(exist==-1)
{
space = findSpace( ); //查找是否有空闲物理块
if(space != -1) //有空闲物理块
{
block[space].pagenum = curpage;
display( );
n=n+1;
}
else //无空闲物理块,则寻找置换页面
{
position = findReplace( ); //查找应予置换的页面
block[position].pagenum = curpage;
display( );
n++;
block[position].accessed--; //置换页面所在的物理块中访问标记设为-1
}
}
else//若存在该页
{
for(int i=0; i<size; i++)
{ if(block[i].pagenum != -1) //物理块不空
printf(" %02d ",block[i].pagenum);
}
cout<<"The instruction already exists! Its physical block address is:"<<&block[exist]<<endl;
}
for(int j=0; j<size; j++)
block[j].accessed++;
}
cout<<"Page faults:"<<n<<endl;
cout<<"Page fault rate:"<<(n/12.0)*100<<"%"<<endl;
}
int main( )
{
Random( );
init();
cout<<"Call page queue"<<endl;
pagestring( );
cout<<endl<<" Begin execution"<<endl;
FIFO();
getchar();
return 0;
}
运行截图:
更多推荐
已为社区贡献1条内容
所有评论(0)