📄 os2.cpp
字号:
#include <stdlib.h>
#include <stdio.h>
#include <iostream.h>
#include <iomanip.h>
#define max 4
int i,j,n,m,x,y,p,b1;
int N=0;
char w,ch;
float N1=0;//缺页次数
float N2=0;//访问次数
int a[100][2];//页表
int b[5];//块号
int c[max][max];//位示图
//**********************位示图*******************************
void from()//位示图
{
cout<<" 位示图:"<<endl;
//int c[max][max];
c[0][0]=1;
c[0][1]=1;
c[0][2]=1;
c[0][3]=1;
for(i=1;i<4;i++)
for(j=0;j<4;j++)
{
c[i][j]=rand()%2;//随机产生0/1
}
for(i=0;i<4;i++)//输出矩阵
{
for(j=0;j<4;j++)
cout<<c[i][j]<<" ";
if(j%max==0)
cout<<endl;
}
}
//***************************显示状态*************************
void show()
{
cout<<"显示页表状态"<<endl;
for(j=0;j<2;j++)
{
for(i=0;i<n;i++)
cout<<a[i][j]<<" ";
cout<<endl;
}
cout<<endl;
cout<<"显示内存状态"<<endl;
for(i=0;i<m;i++)
{
cout<<"第"<<i<<"块内存:"<<endl;
cout<<b[i]<<" "<<endl;
}
}
//****************************初始状态*************************
void start()
{
from();
cout<<"请输入作业块数<10块:"<<endl;
cin>>n;
for(j=0;j<2;j++)
for(i=0;i<=n;i++)
{
a[i][j]=0;
}
cout<<"请输入内存分配的块数:3或4 "<<endl;
cin>>m;
for(i=0;i<m-1;i++)
{
b[i]=0;
}
show();
cout<<"输入块大小Kb"<<endl;
cin>>hex>>y;
}
//**********************************先来先服务*************************************
void FIFO()
{
{ loop0: if(N<m)
{
cout<<"请输入逻辑地址:"<<endl;
cin>>hex>>x;
if(a[x/(y*1024)][1]==1)
{ cout<<"命中!!!"<<endl;
show();
N1;
N2++;
cout<<"访问次数:"<<N2<<endl;
cout<<"缺页次数:"<<N1<<endl;
cout<<"缺页率:"<<((N1*100)/N2)<<"%"<<endl;
}
else{
cout<<"第"<<x/(y*1024)<<"块"<<endl;
a[x/(y*1024)][1]=1;//状态位置1
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
while(c[i][j]==0)
{
a[x/(y*1024)][0]=i*4+j+1;//查位示图找第 x/y页对应的空闲块号
cout<<"物理地址为:"<<endl;
cout<<hex<<(i*4+j+1)*(y*1024)+x%(y*1024)<<endl;
c[i][j]=1;
b[N]=x/(y*1024);
N++;
N1++;
N2++;
cout<<"缺页次数:"<<N1<<endl;
cout<<"访问次数:"<<N2<<endl;
cout<<"缺页率"<<((N1*100)/N2)<<"%"<<endl;
show();
goto loop0;
}
}
}
}
goto loop0;
}
else
cout<<"警告:内存已经满了"<<endl;
}
{
loop1:
cout<<"输入逻辑地址:"<<endl;
cin>>hex>>x;
if(x==0)
exit(1);
else
{
if(a[x/(y*1024)][1]==1)
{
cout<<"命中!"<<endl;
show();
N1;
N2++;
cout<<"访问次数:"<<N2<<endl;
cout<<"缺页次数:"<<N1<<endl;
cout<<"缺页率:"<<((N1*100)/N2)<<"%"<<endl;
}
else
{
cout<<"没有命中需要置换!"<<endl;
cout<<"把第"<<b[0]<<"释放"<<endl;
p=a[b[0]][0];
for(j=0;j<2;j++)
a[b[0]][j]=0;
for(i=0;i<m-1;i++)//特殊栈下压
{
b[i]=b[i+1];
}
b[i+1]=0;
a[x/(y*1024)][1]=1;//状态位置1
a[x/(y*1024)][0]=p;
cout<<"物理地址为:"<<endl;
cout<<p*(y*1024)+x%(y*1024);
cout<<endl;
c[i][j]=1;
b[m-1]=x/(y*1024);
show();
N2++;
N1++;
cout<<"缺页次数:"<<N1<<endl;
cout<<"访问次数:"<<N2<<endl;
cout<<"缺页率:"<<(int)((N1*100)/N2)<<"%"<<endl;
goto loop1;
}
goto loop1;
}
}
}
//********************最近最久未使用******************************
void LRU()
{
{ loop0: if(N<m)
{
cout<<"请输入逻辑地址:"<<endl;
cin>>hex>>x;
if(a[x/(y*1024)][1]==1)
{ cout<<"命中!!!"<<endl;
b1=x/(y*1024);
for(i=0;i<=N;i++)
{
if(b1==b[i])
{ j=i;
for(i=j;i<=N;i++)
{
b[i]=b[i+1];
}
b[N-1]=b1;
}
}
show();
N1;
N2++;
cout<<"访问次数:"<<N2<<endl;
cout<<"缺页次数:"<<N1<<endl;
cout<<"缺页率:"<<((N1*100)/N2)<<"%"<<endl;
goto loop0;
}
else{
cout<<"第"<<x/(y*1024)<<"块"<<endl;
a[x/(y*1024)][1]=1;//状态位置1
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
while(c[i][j]==0)
{
a[x/(y*1024)][0]=i*4+j+1;//查位示图找第 x/y页对应的空闲块号
cout<<"物理地址为:"<<endl;
cout<<hex<<(i*4+j+1)*(y*1024)+x%(y*1024)<<endl;
c[i][j]=1;
b[N]=x/(y*1024);
N++;
N1++;
N2++;
cout<<"缺页次数:"<<N1<<endl;
cout<<"访问次数:"<<N2<<endl;
cout<<"缺页率"<<((N1*100)/N2)<<"%"<<endl;
show();
goto loop0;
}
}
}
}
}
else
cout<<"警告:内存已经满了"<<endl;
}
{
loop1: { cout<<"输入逻辑地址:"<<endl;
cin>>hex>>x;
if(x==0)
exit(1);
else
{
if(a[x/(y*1024)][1]==1)
{
cout<<"命中!"<<endl;
b1=x/(y*1024);
for(i=0;i<m-1;i++)
{
if(b1==b[i])
{ j=i;
for(i=j;i<m-1;i++)
{
b[i]=b[i+1];
}
b[m-1]=b1;
}
}
show();
N1;
N2++;
cout<<"访问次数:"<<N2<<endl;
cout<<"缺页次数:"<<N1<<endl;
cout<<"缺页率:"<<((N1*100)/N2)<<"%"<<endl;
goto loop1;
}
else
{
cout<<"没有命中需要置换!"<<endl;
cout<<"把第"<<b[0]<<"释放"<<endl;
p=a[b[0]][0];
for(j=0;j<2;j++)
a[b[0]][j]=0;
for(i=0;i<m-1;i++)//特殊栈下压
{
b[i]=b[i+1];
}
b[m-1]=0;
a[x/(y*1024)][1]=1;//状态位置1
a[x/(y*1024)][0]=p;
cout<<"物理地址为:"<<endl;
cout<<hex<<p*(y*1024)+x%(y*1024);
cout<<endl;
c[i][j]=1;
b[m-1]=x/(y*1024);
show();
N2++;
N1++;
cout<<"缺页次数:"<<N1<<endl;
cout<<"访问次数:"<<N2<<endl;
cout<<"缺页率:"<<((N1*100)/N2)<<"%"<<endl;
goto loop1;
}
}
}
}
}
void main()
{
start();
cout<<"选择算法"<<endl;
cin>>ch;
if(ch=='f')
FIFO();
else
LRU();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -