操作系统实验报告班级:计算机科学与技术三班姓名:李生启学号:2013329620077实验三:页面置换算法一、实验目的1、熟悉内存分页管理策略。
2、编写OPT、FIFO、LRU,LFU四种置换算法并模拟实现。
3、锻炼知识的运用能力和实践能力。
二、实验内容设计主界面,输入一串系列模拟页面请求,实现以下算法:1) 最佳置换算法(OPT):将以后永不使用的或许是在最长(未来)时间内不再被访问的页面换出。
2) 先进先出算法(FIFO):淘汰最先进入内存的页面,即选择在内存中驻留时间最久的页面予以淘汰。
3) 最近最久未使用算法(LRU):淘汰最近最久未被使用的页面。
4) 最不经常使用算法(LFU)三、代码及运行结果分析1.代码:(采用C++,环境:VS2010)#include<iostream>#include<string>#include <fstream>#include<stdio.h>using namespace std;void OPT(int count){int all[50];int bracket[3];int bll[50]={0};ifstream fin("abc.txt");if( fin.is_open() ){for(int i=0;i<count;i++){fin>>all[i];}fin.close();}bracket[0]=all[0];bracket[1]=all[1];bracket[2]=all[2];for(int i=0;i<count;i++){if(i<3){if(i==0){cout<<"当前序列:"<<endl;cout<<bracket[0]<<endl;}if(i==1){cout<<"当前序列:"<<endl;cout<<bracket[0]<<" "<<bracket[1]<<endl;}if(i==2){cout<<"当前序列:"<<endl;cout<<bracket[0]<<" "<<bracket[1]<<" "<<bracket[2]<<endl;}}if(i>=3){int a_num=count+1;int b_num=count+1;int c_num=count+1;int qi=0;for(int x=0;x<3;x++){if(bracket[x]==all[i]){cout<<"当前序列:"<<endl;cout<<bracket[0]<<" "<<bracket[1]<<" "<<bracket[2]<<endl;qi=6;break;}}if(qi==0){for(int e=i+1;e<count;e++)if(bracket[0]==all[e]){a_num=e;break;}}for(int e=i+1;e<count;e++){if(bracket[1]==all[e]){b_num=e;break;}}for(int e=i+1;e<count;e++){if(bracket[2]==all[e]){c_num=e;break;}}}if((a_num>=b_num)&&(a_num>=c_num))bracket[0]=all[i];elseif((b_num>=a_num)&&(b_num>=c_num))bracket[1]=all[i];elseif((c_num>=b_num)&&(c_num>=a_num))bracket[2]=all[i];cout<<"当前序列:"<<endl;cout<<bracket[0]<<" "<<bracket[1]<<" "<<bracket[2]<<endl;}}}void FIFO(int count){int bll[50]={0};int all[50];int top=0;ifstream fin("abc.txt");if( fin.is_open() ){for(int i=0;i<count;i++){fin>>all[i];}fin.close();}for(int i=0;i<count;i++){if(top<3){bll[top]=all[i];top++;if(top==1){cout<<"当前序列:"<<endl;cout<<bll[top-1]<<endl;}if(top==2){cout<<"当前序列:"<<endl;cout<<bll[top-1]<<" "<<bll[top-2]<<endl;}if(top==3){cout<<"当前序列:"<<endl;cout<<bll[top-1]<<" "<<bll[top-2]<<" "<<bll[top-3]<<endl;}}if(top>=3){if((all[i]!=bll[top-1])&&(all[i]!=bll[top-2])&&(all[i]!=bll[top-3])) {bll[top]=all[i];top++;}cout<<"当前序列:"<<endl;cout<<bll[top-1]<<" "<<bll[top-2]<<" "<<bll[top-3]<<endl;}}}void LRU(int count){int bll[50]={0};int all[50];int top=0;ifstream fin("abc.txt");if( fin.is_open() ){for(int i=0;i<count;i++){fin>>all[i];}fin.close();}for(int i=0;i<count;i++){if(top<3){bll[top]=all[i];top++;if(top==1){cout<<"当前序列:"<<endl;cout<<bll[top-1]<<endl;}if(top==2){cout<<"当前序列:"<<endl;cout<<bll[top-1]<<" "<<bll[top-2]<<endl;}if(top==3){cout<<"当前序列:"<<endl;cout<<bll[top-1]<<" "<<bll[top-2]<<" "<<bll[top-3]<<endl;}}if(top>=3){int end[3];int point=0;bll[top]=all[i];top++;end[0]=bll[top-1];point++;for(int i=top-2;i>=0;i--){if(point==3){cout<<"当前序列:"<<endl;cout<<end[0]<<" "<<end[1]<<" "<<end[2]<<endl;point=0;break;}else if((point==1)&&(end[0]!=bll[i])){end[point]=bll[i];point++;}else if((point==2)&&(end[0]!=bll[i])&&(end[1]!=bll[i])){end[point]=bll[i];point++;}}}}void LFU(int count){int all[50];int bll[50]={0};int top=0;int bracket[3];ifstream fin("abc.txt");if( fin.is_open() ){for(int i=0;i<count;i++){fin>>all[i];}fin.close();}bracket[0]=all[0];bracket[1]=all[1];bracket[2]=all[2];for(int i=0;i<count;i++)int a_num=0;int b_num=0;int c_num=0;int qi=0;if(i<3){if(i==0){cout<<"当前序列:"<<endl;cout<<bracket[0]<<endl;}if(i==1){cout<<"当前序列:"<<endl;cout<<bracket[0]<<" "<<bracket[1]<<endl;}if(i==2){cout<<"当前序列:"<<endl;cout<<bracket[0]<<" "<<bracket[1]<<""<<bracket[2]<<endl;}}if(i>=3){for(int x=0;x<3;x++){if(bracket[x]==all[i]){cout<<"当前序列:"<<endl;cout<<bracket[0]<<" "<<bracket[1]<<" "<<bracket[2]<<endl;qi=6;break;}}if(qi==0){for(int e=0;e<=i;e++){if(bracket[0]==all[e]){a_num++;}}for(int e=0;e<=i;e++){if(bracket[1]==all[e]){b_num++;}}for(int e=0;e<=i;e++){if(bracket[2]==all[e]){c_num++;}}}if((a_num<=b_num)&&(a_num<=c_num)) {bracket[0]=all[i];}else if((b_num<=a_num)&&(b_num<=c_num)){bracket[1]=all[i];}else if((c_num<=b_num)&&(c_num<=a_num)){bracket[2]=all[i];}cout<<"当前序列:"<<endl;cout<<bracket[0]<<" "<<bracket[1]<<" "<<bracket[2]<<endl;}}}int main(){int a[50];int count=0;cout<<"请输入页面请求序列的个数:"<<endl;cin>>count;ofstream out("abc.txt");cout<<"请输入页面请求序列:"<<endl;if(out.is_open()){for(int i=0;i<count;i++){cin>>a[i];out << a[i]<< endl;}out.close( );}while(true){int choose;cout<<"--------------------------------"<<endl;cout<<"最佳置换算法(OPT)--------1"<<endl;cout<<"先进先出算法(FIFO)-------2"<<endl;cout<<"最近最久未使用算法(LRU)--3"<<endl;cout<<"最不经常使用算法(LFU)----4"<<endl;cout<<"退出系统-----------------5"<<endl;cout<<"--------------------------------"<<endl;cout<<"输入要执行的操作;"<<endl;cin>>choose;switch(choose){case 1:OPT(count);break;case 2:FIFO(count);break;case 3:LRU(count);break;case 4:LFU(count);break;default :break;}}}2.运行结果及分析:1.第一张图为输入的主界面,本程序可以选择输入任意长度页面和任意的页面请求序列,如上选择的是10个页面,页面请求序列如上,同时选择菜单如上。