(C语言)用FIFO和LRU算法处理缺页中断

// FIFO3.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include
#include
#include
using namespace std;
#define SizeOfPage 100
#define SizeOfBlock 128
#define M 4


struct info//页表
{
bool flag; //标志
long block;//主存块号
long disk;//在磁盘上的位置
bool dirty;//修改标志
int time;
}
pagelist[SizeOfPage];
long po;//队列标记
long P[M];
int queue[100];
int K;
void init_ex1()
{
memset(pagelist,0,sizeof(pagelist));
pagelist[0].flag=1;
pagelist[0].block=5;
pagelist[0].disk=011;
pagelist[1].flag=1;
pagelist[1].block=8;
pagelist[1].disk=012;
pagelist[2].flag=1;
pagelist[2].block=9;
pagelist[2].disk=013;
pagelist[3].flag=1;
pagelist[3].block=1;
pagelist[3].disk=021;
}
void work_ex1()
{
bool stop=0;
long p,q;
char s[128];
do
{
printf("\n请输入指令的页号和单元号:\n");
if (scanf("%ld%ld",&p,&q)!=2)
{
scanf("%s",s);
if (strcmp(s,"exit")==0)
{
stop=1;
}
}
else
{
if (pagelist[p].flag)//该页在主存中
{
int result;
result=pagelist[p].block*SizeOfBlock+q;
cout<<"绝对地址为:"<}
else//该页不在主存中
{
printf("* %ld\n",p);
}
}
}while (!stop);
}
void init_ex2()
{
po=0;
P[0]=0;P[1]=1;P[2]=2;P[3]=3;
memset(pagelist,0,sizeof(pagelist));
pagelist[0].flag=1;
pagelist[0].block=5;
pagelist[0].disk=011;
pagelist[1].flag=1;
pagelist[1].block=8;
pagelist[1].disk=012;
pagelist[2].flag=1;
pagelist[2].block=9;
pagelist[2].disk=013;
pagelist[3].flag=1;
pagelist[3].block=1;
pagelist[3].disk=021;
}
int GetMax()
{


int max=-1;
int tag=0;
for(po=0;po{
if(pagelist[P[po]].time>max)
{
max=pagelist[P[po]].time;
tag=po;
}
}
return tag;
}

void work_ex2()
{
long p,q,i;
char s[100];
bool stop=0;
do
{
printf("\n请输入指令的:\n页号 单元号 是否为存指令(y/n) \n");
if (scanf("%ld%ld",&p,&q)!=2)
{
scanf("%s",s);
if (strcmp(s,"exit")==0)
{
stop=1;
}
}
else
{
scanf("%s",s);
if (pagelist[p].flag)//该页在主存中
{
int result;
result=pagelist[p].block*SizeOfBlock+q;
cout<<"绝对地址为:"<if (s[0]=='Y' || s[0]=='y')
{
pagelist[p].dirty=1;
//是存指令,修改标志为“1”
}

}
else//该页不在主存中,产生缺页中断
{
if (pagelist[p].dirty)//不是存指令,修改标志为“0”
{
//将更新后的内容写回外存
pagelist[p].dirty=0;
}
pagelist[P[po]].flag=0;//从主存中调出该页
if(pagelist[P[po]].dirty==1)
{printf("out %ld\n",P[po]);
printf("in %ld\n",p);}//输出调出的页号
else prin

tf("in %ld\n",p);//输入装入的页号
pagelist[p].block=pagelist[P[po]].block;
pagelist[p].flag=1;
P[po]=p;
po=(po+1)%M;
}
}
}
while (!stop);
printf("数组P 的值为:\n");
for (i=0;i{
printf("P[%ld]=%ld\n",i,P[i]);
}
}
void init_ex3()
{
po=0;
P[0]=0;P[1]=1;P[2]=2;P[3]=3;
memset(pagelist,0,sizeof(pagelist));
pagelist[0].flag=1;
pagelist[0].block=5;
pagelist[0].disk=011;
pagelist[1].flag=1;
pagelist[1].block=8;
pagelist[1].disk=012;
pagelist[2].flag=1;
pagelist[2].block=9;
pagelist[2].disk=013;
pagelist[3].flag=1;
pagelist[3].block=1;
pagelist[3].disk=021;
}
void work_ex3()
{
long p,q,i;
char s[100];
bool stop=0;
do
{
printf("\n请输入指令的:\n页号 单元号 是否为存指令(y/n) \n");
if (scanf("%ld%ld",&p,&q)!=2)
{
scanf("%s",s);
if (strcmp(s,"exit")==0)
{
stop=1;
}
}
else
{
scanf("%s",s);
if (pagelist[p].flag)//该页在主存中
{
int result;
result=pagelist[p].block*SizeOfBlock+q;
cout<<"绝对地址为:"<if (s[0]=='Y' || s[0]=='y')
{
pagelist[p].dirty=1;
//是存指令,修改标志为“1”
}


for(po=0;po{ if(P[po]!=p)
{
pagelist[P[po]].time++;
}
else
{pagelist[P[po]].time=0;}

}



}
else//该页不在主存中,产生缺页中断
{
if (pagelist[p].dirty)//不是存指令,修改标志为“0”
{
//将更新后的内容写回外存
pagelist[p].dirty=0;
}

po=GetMax();
pagelist[P[po]].flag=0;//从主存中调出该页


//if(pagelist[P[po]].dirty==1)
printf("out %ld\n",P[po]);
printf("in %ld\n",p);//输出调出的页号
//else printf("in %ld\n",p);//输入装入的页号
pagelist[p].block=pagelist[P[po]].block;
pagelist[p].flag=1;
P[po]=p;

for(po=0;po{ if(P[po]!=p)
{
pagelist[P[po]].time++;
}
else
{pagelist[P[po]].time=0;}

}




}
}
}
while (!stop);
printf("数组P 的值为:\n");
for (i=0;i{
printf("P[%ld]=%ld\n",i,P[i]);
}

}
void select()
{
long se;
char s[128];
do
{
printf("请选择第一题or第二题or第三题:");
if (scanf("%ld",&se)!=1)
{
scanf("%s",s);
if (strcmp(s,"exit")==0)
{
return;
}
}
else
{
if (se==1)
{
init_ex1();
work_ex1();
}
if (se==2)
{
init_ex2();
work_ex2();
}
if(se==3)
{
init_ex3();
work_ex3();
}
}
}while (1);
}
int main()
{
select();
return 0;
}

相关文档
最新文档