请求分页存储管理虚拟存储.docx
《请求分页存储管理虚拟存储.docx》由会员分享,可在线阅读,更多相关《请求分页存储管理虚拟存储.docx(9页珍藏版)》请在冰豆网上搜索。
请求分页存储管理虚拟存储
任务四、请求分页存储管理(虚拟存储)
一、实验目的
通过请求分页存储管理的设计,让学生了解虚拟存储器的概念和实现方法。
进行运行时不需要将所有的页面都调入内存,只需将部分调入内存,即可运行,在运行的过程中若要访问的页面不在内存时,则需求有请求调入的功能将其调入。
假如此时若内存没有空白物理块,则通过页面置换的功能将一个老的不用的页面淘汰出来,其中淘汰的算法有多种。
二、实验内容
模拟仿真请求分页调度算法,其中淘汰的算法可选下列其一
1、先进先出算法
2、最近最久算法
3、CLOCK算法
三、实验代码
#include
#include
usingnamespacestd;
intn;
typedefstructQueue{
inttime;
intdata;
structQueue*next;
}Queue,*QueuePtr;
typedefstruct{
QueuePtrfront;
QueuePtrrear;
}LinkQueue;
//fifo=======================================
voidInitQueue(LinkQueue&Q);
voidFiFoEnQueueRear(LinkQueue&Q,inte,vector&v);
voidFiFoDeQueueFront(LinkQueue&Q);
inlinevoidPrintQueue(LinkQueue&Q);
voidFiFoFiFoDoQueueEarly(LinkQueue&Q,inta,vector&v);
voidFiFoDoQueue(LinkQueue&Q,inta,vector&v);
inlineintPanDuan(LinkQueue&Q,inta);
inlineintYeMianCount(LinkQueue&Q);
voidfifo();
//lru=============================================
voidInitQueue(LinkQueue&Q);
voidEnQueueMid(LinkQueue&Q,inte,QueuePtrp,vector&v);
voidEnQueueTheFist(LinkQueue&Q,inte);
voidPrintQueue(LinkQueue&Q);
//voidZhiZhenInit(intn);
voidDoQueueEarly(LinkQueue&Q,inte,vector&v);
voidEnQueueRear(LinkQueue&Q,inte,QueuePtrp,vector&v);
//voidDeQueueFront(LinkQueue&Q);
QueuePtrZhiZhen(LinkQueue&Q,inte);
voidEnQueue(LinkQueue&Q,inte,vector&v);
//voidDeQueue(LinkQueue&Q,QueuePtrp);
intPanDuan(LinkQueue&Q,inta);
intYeMianCount(LinkQueue&Q);
voidlru();
QueuePtrOptimalZhiZhen(LinkQueue&Q,inte);//求出需要置换的页面的上一个页面的位置
voidEnQueue(LinkQueue&Q,inte,vector&v,inti,vector&vc);
inlineintYeMianCount(LinkQueue&Q);//使用内敛函数,提高性能
inlineintDestance(vector&v,inti);
inlinevoidSubbDestance(LinkQueue&Q);
//=================================================
intmain(){
intk;
for(;;)
{
cout<<"请选择!
"<cout<<"1——FiFo算法"<cout<<"2——LRU算法"<cout<<"0——退出"<cin>>k;
if(k==0)
break;
else{
switch(k)
{
case1:
fifo();break;
case2:
lru();break;
default:
cout<<"请从以上的选择中选择一个选项!
!
"<}
}
}
}
//fifo========================================
voidInitQueue(LinkQueue&Q){
Q.front=Q.rear=(QueuePtr)malloc(sizeof(Queue));
if(!
Q.front)
cout<<"initqueueworng!
"<Q.front->next=NULL;
//returnOK;
}
voidFiFoEnQueueRear(LinkQueue&Q,inte,vector&v){
QueuePtrp;
p=(QueuePtr)malloc(sizeof(Queue));
if(!
p)
cout<<"cannotmallocEnQueueofthep"<p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
v.push_back(e);
//cout<data;
//returnOK;
}
voidFiFoDeQueueFront(LinkQueue&Q){
QueuePtrp;
if(Q.front==Q.rear)
cout<<"theQueueisempty,cannotdelete!
!
"<p=Q.front->next;
Q.front->next=p->next;
free(p);
if(Q.rear==p)
Q.rear=Q.front;
//returnOK
}
voidPrintQueue(LinkQueue&Q){
QueuePtrp;
if(Q.front==Q.rear)
cout<<"theQueueisempty,cannotprint!
"<p=Q.front;
for(p=Q.front;p->next!
=NULL;p=p->next)
{
cout<next->data<<" ";
}
cout<//retrunOK;
}
voidFiFoFiFoDoQueueEarly(LinkQueue&Q,inta,vector&v){
QueuePtrp;
intcount=0;//设置标志位,记录重复的个数。
假如COUNT为0,表示没有重复
intb,c;
b=a;
c=PanDuan(Q,b);
//p=Q.front;
if(c==1)
{
FiFoEnQueueRear(Q,b,v);
PrintQueue(Q);
if(v[v.size()-1]!
=a) v.push_back(a);
}
else
{
cout<};
//v.push_back(a);
//cout<!
!
!
"<}
voidFiFoDoQueue(LinkQueue&Q,inta,vector&v){
QueuePtrp;
intcount=1;
//p=Q.front;
intb,c;
b=a;
c=PanDuan(Q,b);
//p=Q.front;
if(c==1)
{
FiFoEnQueueRear(Q,b,v);
FiFoDeQueueFront(Q);
PrintQueue(Q);
}
else
{
cout<};
//returnOK;
}
intPanDuan(LinkQueue&Q,inta){
QueuePtrp;
for(p=Q.front;p->next!
=Q.rear->next;p=p->next)
{
if(p->next->data!
=a)
{}
else
return0;
}
return1;
}
intYeMianCount(LinkQueue&Q){
QueuePtrp;
intcount=0;
for(p=Q.front;p->next!
=Q.rear->next;p=p->next)
count+=1;
returncount;
}
voidfifo(){
vectora;
vectorv;
LinkQueueQ;
intm,n,b,c;
InitQueue(Q);
m=n=b=c=0;
cout<<"请输入页块数"<cin>>n;
cout<<"请输入序列的个数"<cin>>m;
cout<<"请输入序列"<for(inti=0;i{
cin>>c;
a.push_back(c);
//cout<}
cout<<"开始页面置换"<b=a[0];
//cout<
FiFoEnQueueRear(Q,b,v);
PrintQueue(Q);
//cout<<"#"<for(i=1;i{
b=a[i];
//cout<
FiFoFiFoDoQueueEarly(Q,b,v);
//PrintQueue(Q);
//cout<}
for(i=n;i{
b=a[i];
c=YeMianCount(Q);
if(c{
FiFoFiFoDoQueueEarly(Q,b,v);