/******************************
改一下output[][]输出格式
字符串bug
****************************/
#include <bits/stdc++.h>
using namespace std;
#define max1 200
#define max2 0x3f3f3f
struct LRU ///LRU算法所用到的结构体
{
int num; ///内存块的数字
int time;///最近访问时间
};
struct table ///表格的结构体
{
int queyecishu;
int mingzhongcishu;
int zhihuancishu;
double queyelv;
double mingzhonglv;
string name;
};
table lru2,option2,clock2,a[3];///a[3]用于比较三个大小使用
int output1[max1][max1],output2[max1][max1],output3[max1][max1],s=0,s2=0,s3=0,zhong1=0,zhong2=0,zhong3=0,kk;///
LRU lru[max1];///定义一个结构体数组
char ch;
int pre[max1],dist[max1][max1],block1[max1];
int block,length,page[max1],papelack=0;
int n,m,pages,replace1,replace2,replace3;
int a1[max1],a2[max1],a3[max1];
int num1=0;
int num2=0;
int num3=0;
void menu();
void menu1();
void menu2();
bool flag;
void delay(int d)
{
unsigned int i;
for(; d>0; d--)
{
for(i=0; i<25; i++)
{
printf(" \b");
}
}
}
void init()///初始化访问时间和物理块中存的数
{
for (int i = 0; i < n; i++)
{
lru[i].time = 0;
lru[i].num = -1;
}
}
/******************************
返回找到的位置或者替换的位置
*******************************/
int findpage(int k)
{
int i = 0;
for(int j = 0; j < m; j++)
{
if(lru[j].num == page[k])
{
i = j;
flag = false;
break;
}
}
if(flag)///没有找到
{
for(int j = 1; j < m; j++)
{
if(lru[i].time < lru[j].time)
{
i = j; ///返回最后使用的内存块用来被替换。
}
}
}
return i;
}
int LRU1()
{
papelack=0;
init(); //初始化
int line = 0;///可以理解为当前内存块中的页面个数。
bool flag1; ///访问标志
int i, j, tmp;
for (i = 1; i <= n; i++) ///依次访问每一个页面
{
flag = true; ///用来标记是否能在内存块中找到该页面。
if (line < m) ///物理块没装满的情况下进行该页面访问
{
flag1 = false;
for (j = 0; j < line; j++)
{
if (lru[j].num == page[i]) ///找到,然后用outputp1[][]记录当前内存块的页面
{
for(int pp = 0; pp < m; pp++)
{
output1[s][pp] = lru[pp].num;
}
s++;
flag1 = true;
lru[j].time = 0; ///更新访问时间
break;
}
}
if (!flag1) ///没找到要置换
{
lru[line++].num = page[i];
lru[line].time = 0;
for(int pp = 0; pp < m; pp++)
{
output1[s][pp] = lru[pp].num;
}
s++;
papelack++;
}
}
else ///当前内存块已满,要么找到,否则就要置换
{
tmp = findpage(i); ///接收找到的位置或者置换的位置。
if (flag) ///flag为真代表没找到。
{
a1[num1++] = lru[tmp].num;
lru[tmp].num = page[i]; ///先把他替换再跟新该位置的访问时间
lru[tmp].time = 0;
for(int pp = 0; pp < m; pp++)
{
output1[s][pp] = lru[pp].num;
}
s++;
replace1++; ///中断加一
papelack++; ///缺页加一
}
else ///找到
{
lru[tmp].time = 0; ///更新访问时间
zhong1++; ///命中次数加一
for(int pp = 0; pp < m; pp++)
{
output1[s][pp] = lru[pp].num;
}
s++;
}
}
if (line > m) ///内存装满
{
for (int k = 0; k < m; k++)
{
lru[k].time++;
}
}
else
{
for (int k= 0; k < line; k++)
{
lru[k].time++;
}
}
}
if(line >= m)
kk = m;
else kk = line;
return papelack;
}
int OPT1()
{
int pre[max1];
int papelack = 0;
fill(pre,pre+max1,0);
fill(dist[0],dist[0]+max1*max1,max2);
fill(block1,block1+max1,-1);
for(int i = n; i >= 1; i--)
{
for(int j = 0; j <= pages; j++)
{
if(pre[j])
{
dist[i][j] = pre[j] - i;
}
pre[page[i]] = i;
}
}
for(int i = 1; i <= n; i++)
{
int j;
int maxdist = 0,p;
for( j = 1; j <= m; j++)
{
if(block1[j] == -1)
{
block1[j] = page[i];
papelack++;
for(int k = 1; k <= m; k++)
{
output2[s2][k] = block1[k];
}
s2++;
break;
}
else if(block1[j] == page[i])
{
zhong2++;
for(int k = 1; k <= m; k++)
{
output2[s2][k] = block1[k];
}
s2++;
break;
}
if(maxdist < dist[i][block1[j]])
{
maxdist = dist[i][block1[j]];
p = j;
}
}
if(j > m)
{
replace2++;
a2[num2++] = block1[p];
block1[p] = page[i];
papelack++;
for(int k = 1; k <= m; k++)
{
output2[s2][k] = block1[k];
}
s2++;
}
}
return papelack;
}
int nru[max1];//表示 物理块 i 最近时候被访问过
int page_in_block[max1];//页面 i 在 block的下标索引
int CLOCK1()
{
int index = 1;
papelack = 0;
memset(nru,0,sizeof(nru));
memset(page_in_block,0,sizeof(page_in_block));
memset(block1, -1, sizeof(block1));
for(int i=1; i<=n; i++)
{
if(page_in_block[page[i]])
{
//如果page[i]已经在内存中
nru[page_in_block[page[i]]] = 1;//重新标记这个物理块中的页面被访问过了
zhong3++;
}
else
{
while(true)
{
if(index > m) index = 1;
if(block1[index] == -1)
{
nru[index] = 1;
page_in_block[page[i]] = index;
block1[index++] = page[i];
papelack++;
break;
}
if(block1[index] == page[i])
{
nru[index++] = 1;
break;
}
else
{
if(nru[index] == 0) //替换该页面
{
nru[index] = 1;
page_in_block[block1[index]] = 0;
page_in_block[page[i]] = index;
block1[index++] = page[i];
papelack++;
replace3++;
a3[num3++] = block1[index];
break;
}
else
nru[index++] = 0;
}
}
}
for(int k = 1; k <=m; k++)
output3[s3][k] = block1[k];
s3++;
}
return papelack;
}
/*********************
设计一个表格输出三种算法的结果
*********************/
void compare()
{
system("CLS");
lru2.name = "LRU";
lru2.queyecishu = LRU1();
lru2.zhihuancishu = replace1;
lru2.mingzhongcishu = zhong1;
lru2.queyelv = 1.0*lru2.queyecishu/n;
lru2.mingzhonglv = 1.0*zhong1/n;
option2.name = "OPTION";
option2.queyecishu = OPT1();
option2.zhihuancishu = replace2;
option2.mingzhongcishu = zhong2;
option2.queyelv = 1.0*option2.queyecishu/n;
option2.mingzhonglv = 1.0*zhong2/n;
clock2.name = "CLOCK";
clock2.queyecishu = CLOCK1();
clock2.zhihuancishu = replace3;
clock2.mingzhongcishu = zhong3;
clock2.queyelv = 1.0*clock2.queyecishu/n;
clock2.mingzhonglv = 1.0*zhong3/n;
a[0] = lru2;
a[1] = option2;
a[2] = clock2;
for(int i = 0; i < 2; i++)
{
for(int j = 0; j < 2-i; j++)
{
if(a[j].mingzhonglv < a[j+1].mingzhonglv)
{
table t = a[j];
a[j] = a[j+1];
a[j+1] = t;
}
}
}
cout<<"结果加载中....."<<endl;
for(int i=0; i<51; i++)
printf("\b");
for(int i=0; i<50; i++)
{
delay(5);
printf(">");
}
system("CLS");
cout<<endl;
cout<<"\t\t\t\t\t\t\t*******************************************"<<endl;
cout<<"\t\t\t\t\t\t\t 三种算法分析表"<<endl; ///可以弄成加载
cout<<"\t\t\t\t\t\t\t*******************************************"<<endl<<endl;
cout<<"\t\t\t\t\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━-━━━━━━━━━━━━━━━━━"<<endl;
cout<<"\t\t\t\t\t \t\tLRU"<<"\t\t"<<"OPTION"<<"\t\t"<<"CLOCK"<<endl;
cout<<"\t\t\t\t\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━-━━━━━━━━━━━━━━━━━"<<endl;
cout<<"\t\t\t\t\t缺页次数\t\t"<<lru2.queyecishu<<"\t\t"<<option2.queyecishu<<"\t\t"<<clock2.queyecishu<<endl;
cout<<"\t\t\t\t\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━-━━━━━━━━━━━━━━━━━"<<endl;
cout<<"\t\t\t\t\t置换次数\t\t"<<lru2.zhihuancishu<<"\t\t"<<option2.zhihuancishu<<"\t\t"<<clock2.zhihuancishu<<endl;
cout<<"\t\t\t\t\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━-━━━━━━━━━━━━━━━━━"<<endl;
cout<<"\t\t\t\t\t命中次数\t\t"<<lru2.mingzhongcishu<<"\t\t"<<option2.mingzhongcishu<<"\t\t"<<clock2.mingzhongcishu<<endl;
cout<<"\t\t\t\t\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━-━━━━━━━━━━━━━━━━━"<<endl;
cout<<"\t\t\t\t\t缺页率 \t\t"<<lru2.queyelv<<"\t\t"<<option2.queyelv<<"\t\t"<<clock2.queyelv<<endl;
cout<<"\t\t\t\t\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━-━━━━━━━━━━━━━━━━━"<<endl;
cout<<"\t\t\t\t\t命中率 \t\t"<<lru2.mingzhonglv<<"\t\t"<<option2.mingzhonglv<<"\t\t"<<clock2.mingzhonglv<<endl;
cout<<"\t\t\t\t\t━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━-━━━━━━━━━━━━━━━━━"<<endl<<endl;
cout<<"\t\t\t\t\t算法性能排名";
cout<<"\t\t\t\t\t =============================================="<<endl;
cout<<"\t\t\t\t\t"<<a[0].name<<"\t>\t"<<a[1].name<<"\t>\t"<<a[2].name<<endl;
cout<<"\t\t\t\t\t ==============================================="<<endl;
///为循环使用清空参数
s =s2=s3= replace1=replace2=replace3=num1=num2=num3=zhong1=zhong2=zhong3=papelack=0;
memset(a1,0,sizeof(a1));
memset(a2,0,sizeof(a2));
memset(a3,0,sizeof(a3));
memset(output2,0,sizeof(output2));
memset(output3,0,sizeof(output3));
memset(output1,0,sizeof(output1));
system("pause");
system("CLS");
menu();
}
void menu2(string string1)
{
system("color 0D");
printf("\t\t\t╔════════════╗\n");
printf("\t\t\t║正在加载算法!!!║\n");
printf("\t\t\t╚════════════╝\n");
printf(" ");
for(int i=0; i<51; i++)
printf("\b");
for(int i=0; i<50; i++)
{
delay(5);
printf(">");
}
system("CLS");
cout<<endl;
cout<<"\t\t\t\t\t\t ▂▃▅▆▇█████▇▆▅▃▂ "<<endl;
cout<<"\t\t\t\t\t\t☆ =================================================================== ☆"<<endl;
cout<<"\t\t\t\t\t\t "<<endl;
cout<<"\t\t\t\t\t\t 您已经选择";
cout<<string1;
cout<<"\t\t\t\t\t\t "<<endl;
cout<<"\t\t\t\t\t\t 请输入页面访问串个数及访问串: "<<endl;
cout<<"\t\t\t\t\t\t 若需返回主菜单请按-1 "<<endl;
cout<<"\t\t\t\t\t\t "<<endl;
cout<<"\t\t\t\t\t\t☆ =================================================================== ☆"<<endl;
}
void menu1()
{
string k;
cin>>k;
if(k!="1"&&k!="2"&&k!="3"&&k!="4"&&k!="5")
{
system("CLS");
menu();
cout<<endl;
menu();
cout<<" 请您按要求输入哟! "<<endl;
}
else if(k == "1")
{
system("CLS");
menu2("LRU");
cin>>n;
if(n == -1)
{
system("CLS");
menu();
}
else
{
for(int i = 1; i <= n ; i++)
{
cin>>page[i];
pages = max(pages,page[i]);
}
cout<<"请输入物理块个数:";
cin>>m;
system("CLS");
cout<<"*******************************"<<endl;
cout<<"LRU算法运行完成"<<endl; ///可以弄成加载
cout<<"*******************************"<<endl;
int pa = LRU1();
printf("----------------------------------------------------------\n");
cout<<"缺页中断次数:"<<"\t\t"<<pa<<endl;
cout<<"页面置换次数:"<<"\t\t"<<replace1<<endl;
printf("缺页率: \t\t%2.2lf",1.0*pa/n*100);
cout<<"%"<<endl;
printf("命中率: \t\t%.2lf",1.0*zhong1/n*100);
cout<<"%"<<endl;
cout<<"依次被置换的页面:\t";
for(int i = 0; i < num1; i++)
cout<<a1[i]<<" ";
cout<<endl;
printf("----------------------------------------------------------\n\n");
cout<<"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"<<endl;
for(int i = 0; i < kk; i++)
{
for(int j = 0; j < s; j++)
{
printf("%3d ",output1[j][i]);
}
cout<<endl;
}
cout<<"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"<<endl;
///为循环使用清空参数
s = 0;
replace1 = 0;
num1 = 0;
zhong1 = 0;
papelack = 0;
memset(a1,0,sizeof(a1));
memset(output1,0,sizeof(output1));
system("pause");
system("CLS");
menu();
}
}
else if(k == "2")
{
system("CLS");
menu2("OPTION");
cin>>n;
if(n == -1)
{
system("CLS");
menu();
}
else
{
for(int i=1; i<=n; ++i)
{
cin>>page[i];
pages = max(pages, page[i]) ;
}
cout<<"请输入物理块个数:";
cin>>m;
system("CLS");
int pa = OPT1();
cout<<"*******************************"<<endl;
cout<<"OPTION算法运行完成"<<endl; ///可以弄成加载
cout<<"*******************************"<<endl;
printf("----------------------------------------------------------\n");
cout<<"缺页中断次数:"<<"\t\t"<<pa<<endl;
cout<<"页面置换次数:"<<"\t\t"<<replace2<<endl;
printf("缺页率: \t\t%2.2lf",1.0*pa/n*100);
cout<<"%"<<endl;
printf("命中率: \t\t%.2lf",1.0*zhong2/n*100);
cout<<"%"<<endl;
cout<<"依次被置换的页面:\t";
for(int i = 0; i < num2; i++)
cout<<a2[i]<<" ";
cout<<endl;
printf("----------------------------------------------------------\n");
cout<<" ";
cout<<"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"<<endl;
for(int i = 1; i <= m; i++)
{
for(int j = 0; j < s2; j++)
{
printf("%3d ",output2[j][i]);
}
cout<<endl;
}
cout<<"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"<<endl;
s2 = 0;
replace2 = 0;
num2 = 0;
zhong2 = 0;
papelack = 0;
memset(a2,0,sizeof(a2));
memset(output2,0,sizeof(output2));
system("pause");
system("CLS");
menu();
replace2 = 0;
}
}
else if(k == "3")
{
system("CLS");
menu2("CLOCK");
cin>>n;
if(n == -1)
{
system("CLS");
menu();
}
else
{
for(int i = 1; i <= n ; i++)
{
cin>>page[i];
pages = max(pages,page[i]);
}
cout<<"请输入物理块个数:";
cin>>m;
int pa = CLOCK1();
cout<<"*******************************"<<endl;
cout<<"CLOCK算法运行完成"<<endl; ///可以弄成加载
cout<<"*******************************"<<endl;
printf("----------------------------------------------------------\n");
cout<<"缺页中断次数:"<<"\t\t"<<pa<<endl;
cout<<"页面置换次数:"<<"\t\t"<<replace3<<endl;
printf("缺页率: \t\t%2.2lf",1.0*pa/n*100);
cout<<"%"<<endl;
printf("命中率: \t\t%.2lf",1.0*zhong3/n*100);
cout<<"%"<<endl;
cout<<"依次被置换的页面:\t";
for(int i = 0; i < num3; i++)
cout<<a3[i]<<" ";
cout<<endl;
printf("----------------------------------------------------------\n");
cout<<"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"<<endl;
for(int i = 1; i <= m; i++)
{
for(int j = 0; j < s3; j++)
{
printf("%3d ",output3[j][i]);
}
cout<<endl;
}
cout<<"━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"<<endl;
s3 = 0;
num3 = 0;
zhong3 = 0;
papelack = 0;
memset(a3,0,sizeof(a3));
memset(output3,0,sizeof(output3));
replace3 = 0;
system("pause");
system("CLS");
menu();
}
}
else if(k == "4")
{
system("CLS");
menu2("算法比较");
cin>>n;
if(n == -1)
{
system("CLS");
menu();
}
else
{
for(int i = 1; i <= n ; i++)
{
cin>>page[i];
pages = max(pages,page[i]);
}
cout<<"请输入物理块个数:";
cin>>m;
compare();
}
}
else if(k == "5")
{
return ;
}
}
void menu() //默认主菜单界面 ***********************************************
{
system("color 8e");
printf("╔════════════╗\n");
printf("║正在加载菜单,请稍候 !!!║\n");
printf("╚════════════╝\n");
printf(" ");
for(int i=0; i<31; i++)
printf("\b");
for(int i=0; i<50; i++)
{
delay(5);
printf("█");
}
system("CLS");
cout<<endl;
cout<<" ▂▃▅▆▇█████▇▆▅▃▂ "<<endl;
cout<<"☆ =================================================================== ☆"<<endl;
cout<<" "<<endl;
cout<<" 欢迎来到操作系统页面置换算法界面 "<<endl;
cout<<" "<<endl;
cout<<"☆ =================================================================== ☆"<<endl;
cout<<" 请输入一下数字选择相应算法 "<<endl;
cout<<" "<<endl;
cout<<" 【1】 LRU算法 "<<endl;
cout<<" 【2】 OPTION算法 "<<endl;
cout<<" 【3】 CLOCK算法 "<<endl;
cout<<" 【4】 查看算法比较表 "<<endl;
cout<<" 【5】 退出程序 "<<endl;
cout<<"☆ =================================================================== ☆"<<endl;
cout<<endl;
cout<<" 请输入【1—5】:";
menu1();
}
int main()
{
cout<<setiosflags(ios::fixed)<<setprecision(2);///保留两位小数
cout.setf(std::ios::right);///右对齐
system("color 8e");
menu();
return 0;
}
/*
3
0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
3
3 2 1 5 2 4 5 3 2 5 2
*/
12.20 1
最新推荐文章于 2023-12-20 21:54:58 发布