📄 zhihuansuanfa.cpp
字号:
#include<iostream>
using namespace std;
#define N 40
#define M 1024//与页表的零页冲突
void main()
{ int yesize,neicun,pagesize,jishu,ld,yehao,kuaihao,x;
long jizhi;
int flag=1,lru,a[8][8],page[N][2] ,memory[N],top=-1,n=0,s1,t1,s2,t2,s3,t3,i,j;
char ch0,ch1,ch2,ch3;
for(j=0;j<8;j++)
a[0][j]=1;
for(i=1;i<8;i++)
{
for(j=0;j<8;j++)
a[i][j]=rand()%2;
}
for(i=0;i<=7;i++)
{
for(j=0;j<=7;j++)
cout<<a[i][j]<<" "; cout<<endl;
}
cout<<"Enter the yesize:"<<endl;
cin>>yesize;
for(i=0;i<yesize;i++)
for(j=0;j<2;j++)
page[i][j]=0;
cout<<"Enter the memorysize:"<<endl;
cin>>neicun;
jishu=neicun;
for(i=0;i<neicun;i++)
memory[i]=M;
cout<<"Enter the size(1/kb):"<<endl;
cin>>pagesize;
jizhi=pagesize*1024;
cout<<" ****************************************************"<<endl;
cout<<" ** **"<<endl;
cout<<" ** Enter the 'L' to LRU **"<<endl;
cout<<" ** **"<<endl;
cout<<" ** if you want out please enter '#' **"<<endl;
cout<<" ****************************************************"<<endl;
cout<<">",cin>>ch0;
while((ch0=='L')&&(flag))
{
cout<<"Enter the number (10):"<<endl;
cin>>ld;
yehao=ld/jizhi;
if(page[yehao][1]==0)
{
cout<<" "<<endl;
cout<<"不在内存,发生缺页中断"<<endl;
cout<<" "<<endl;
if(jishu>0)
{for(i=1;i<8;i++)
{for(j=0;j<8;j++)
{if(a[i][j]==0)
{a[i][j]=1;break;}
else if(a[i][j]!=0)
{;}
}
}
kuaihao=i*8+j;
jishu=jishu-1;
page[yehao][0]=kuaihao;
page[yehao][1]=1;
top=top+1;
memory[top]=yehao;
cout<<"show the ye:"<<endl;
cout<<" 块号"<<" 标志位"<<" 内存"<<endl;
cout<<" "<<endl;
s1=yesize-1;//这里的s1是为输出页表的时候可以倒着输出而设定的
t1=neicun-1;//这里的t1是为输出内存的时候可以倒着输出而设定的
while(t1>=0)
{cout<<" "<<"第"<<s1<<"页 "<<page[s1][0]<<" "<<page[s1][1]<<" "<<"第"<<t1<<"块 "<<memory[t1]<<endl;
s1--; t1--; }
while((t1<0)&&(s1>=0))
{
cout<<" "<<"第"<<s1<<"页 "<<page[s1][0]<<" "<<page[s1][1]<<endl;
s1--;
}
cout<<" Do you want go on yes is enter 'y',else 'n',please choose:"<<endl;
cout<<"-";
cin>>ch1;
if(ch1=='Y')
{flag=1;}
else if(ch1=='N')
{cout<<"Think you see you"<<endl;flag=0;}
else if((ch1=='y')||(ch1=='n'))
{cout<<"enter the big'Y'or'N'"<<endl;}
else
{ cout<<"enter error please do it again"<<endl;}
}
else
{
cout<<" 发生缺页并发生置换 "<<endl;
x=memory[0];
page[yehao][0]=page[x][0];
page[yehao][1]=1;
page[x][0]=0;
page[x][1]=0;
for(i=0;i<neicun-1;i++)
{
memory[i]=memory[i+1];
}
memory[neicun-1]=yehao;
cout<<"此时页表为:"<<endl;
cout<<" "<<endl;
cout<<" 块号"<<" 标志位"<<" 内存"<<endl;
s2=yesize-1;//这里的s2是为输出页表的时候可以倒着输出而设定的
t2=neicun-1;//这里的t2是为输出内存的时候可以倒着输出而设定的
while(t2>=0)
{cout<<" "<<"第"<<s2<<"页 "<<page[s2][0]<<" "<<page[s2][1]<<" "<<"第"<<t2<<"块 "<<memory[t2]<<endl;
s2--;t2--;}
while((t2<0)&&(s2>=0))
{
cout<<" "<<"第"<<s2<<"页 "<<page[s2][0]<<" "<<page[s2][1]<<endl;
s2--;
}
cout<<" Do you want go on yes is enter 'y',else 'n',please choose:"<<endl;
cout<<"-";
cin>>ch2;
if(ch2=='Y')
{flag=1;}
else if(ch2=='N')
{cout<<"欢迎使用本程序"<<endl;flag=0;}
}
}
else if(page[yehao][1]==1)
{
if(memory[neicun-1]==yehao)
{;}
else if ((memory[neicun-1]==M)&&(memory[neicun-2]==M))
{;}
else if ((memory[neicun-1]==M)&&(memory[neicun-3]==yehao))//不是3也不会错,只是内存不变而以!!!
{memory[neicun-3]=memory[neicun-2];memory[neicun-2]=yehao;}
else if(memory[neicun-1]!=yehao)
{for(i=0;i<neicun-1;i++)
{if(memory[i]==yehao)lru=i;break;}
while(lru<neicun-1)
{memory[lru]=memory[lru+1];lru++;}
memory[neicun-1]=yehao;
}
cout<<"该页在内存,发生命中"<<endl;
cout<<"此时页表为:"<<endl;
cout<<" 块号"<<" 标志位"<<" 内存"<<endl;
s3=yesize-1;//这里的s3是为输出页表的时候可以倒着输出而设定的
t3=neicun-1;//这里的t3是为输出内存的时候可以倒着输出而设定的
while(t3>=0)
{cout<<" "<<"第"<<s3<<"页 "<<page[s3][0]<<" "<<page[s3][1]<<" "<<"第"<<t3<<"块 "<<memory[t3]<<endl;
s3--; t3--;}
while((t3<0)&&(s3>=0))
{cout<<" "<<"第"<<s3<<"页 "<<page[s3][0]<<" "<<page[s3][1]<<endl;
s3--;}
cout<<" Do you want go on yes is enter 'y',else 'n',please choose:"<<endl;
cout<<"-";
cin>>ch3;
if(ch3=='Y')
{flag=1;}
else if(ch3=='N')
{cout<<"欢迎使用本程序"<<endl;
flag=0;}
}
else if(yehao>6)
{cout<<"!!!页号超出了了页表长度,发生越界中断"<<endl;}
}
while(ch0=='#')
{ cout<<"Thinks see you"<<endl; break;}
}//共167行
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -