页面调度算法 FIFO,LRU,OPT,及C++代码
来源:互联网 发布:网络部招新面试问题 编辑:程序博客网 时间:2024/05/17 07:40
页面调度算法 FIFO,LRU,OPT
介绍了三种页面调度算法,给出了C++代码
1.FIFO
先来先去算法这个非常好理解,给出分析图
可以看出,缺页次数为8次,缺页中断率为8/12=66.7%,依次置换的页面为:1,2,5,3,4
C++代码在最后给出
2.LRU
LRU,Least Recently Used 近期最少使用算法,先给出分析图
这个也不难理解,我们只需要从之前的一个页面开始往左寻找就可以了,比如在第一次遇到页面3时,我们发现3左边是1,再左边是5,所以1,5近期使用了,就把1,5留下,另外一个页面置换掉,所以此时内存中的页面2就被置换了。
C++代码在最后给出
3.OPT
最佳置换算法,是一种理论算法,对未来的页面检测从而判断置换页。分析图如下
OPT跟LRU非常相似,LRU是往左找,OPT就是往右找啦,比如第一次遇到页面3时,往右依次是2,5,所以2,5页面保留,把内存中的页面1置换掉。
4. 三种算法的C++代码
FIFO:
//FILENAME: FIFO.cpp#include<iostream>using namespace std;int main(){ int memory[3]={-1,-1,-1}; //-1 means no page in this memory page int page[12]={1,2,5,1,3,2,5,4,1,4,5,2};//the test pages int pm=0; //pointer of memory int count=0;//missing page count int replace[12]; int pr=0; cout<<"######FIFO#######"<<endl; //search begin for (int i=0;i<12;i++) { //check if there is page[i] in memory bool exist = false; for (int j=0;j<3;j++) { if (page[i]==memory[j]) { exist=true; break; } } //not exist , replace this memory page if (exist==false) { if(memory[pm]!=-1) { replace[pr]=memory[pm]; pr++; } count++; memory[pm]=page[i]; pm++; if (pm==3) pm=0; } //output cout<<page[i]<<": [ "; for(int j=0;j<3;j++) { if (memory[j]==-1) cout<<"* "; else cout<<memory[j]<<" "; } cout<<"]"<<endl; } //output cout<<"######################"<<endl; cout<<"the lack page count = " <<count<<endl; cout<<"repalce pages are : "; for (int i=0;i<pr;i++) { cout<<replace[i]<<" "; } cout<<endl; cout<<"the rate of page lack is "<<count/12.0*100<<"%"<<endl; return 0;}
LRU:
//FILENAME: LRU.cpp#include<iostream>using namespace std;int main(){ int memory[3]={-1,-1,-1}; int page[12]={1,2,5,1,3,2,5,4,1,4,5,2};//the test pages int count=0;//lack page count int replace[12]; //replace page record int pr=0; //pointer of replace cout<<"######LRU#######"<<endl; //search begin for (int i=0;i<12;i++) { //there are 3 memory pages in memory ,so if i<3,just put it in memory if(i<3) { memory[i]=page[i]; count++; } else { //check if this page is in memory already bool exist=false; for(int j=0;j<3;j++) { if(page[i]==memory[j]) { exist=true; break; } } if (exist==false) { //begin to choose a memory page to replace int last=0; bool ok[3]; for (int j=0;j<3;j++) ok[j]=false; //check from i step -1 till 0 for(int j=i;j>=0;j--) { for(int k=0;k<3;k++) { if (page[j]==memory[k]) { ok[k]=true; last++; break; } } if (last==2)break; } //check which ok ==false for (int j=0;j<3;j++) { if (ok[j]==false) { //replace this memory[j] count++; replace[pr]=memory[j]; pr++; memory[j]=page[i]; break; } } } } //output cout<<page[i]<<": [ "; for(int j=0;j<3;j++) { if (memory[j]==-1) cout<<"* "; else cout<<memory[j]<<" "; } cout<<"]"<<endl; } //out put cout<<"######################"<<endl; cout<<"the lack page count = " <<count<<endl; cout<<"repalce pages are : "; for (int i=0;i<pr;i++) { cout<<replace[i]<<" "; } cout<<endl; cout<<"the rate of page lack is "<<count/12.0*100<<"%"<<endl; return 0;}
OPT:
//FILENAME: OPT.cpp#include<iostream>using namespace std;int main(){ int memory[3]={-1,-1,-1}; int page[12]={1,2,5,1,3,2,5,4,1,4,5,2};//the test pages int count=0;//missing page count int replace[12]; //replace page record int pr=0; // pointer of replace cout<<"######OPT#######"<<endl; //search begin for (int i=0;i<12;i++) { //there are 3 memory pages in memory ,so if i<3,just put it in memory if(i<3) { memory[i]=page[i]; count++; } else { //check if this page is in memory already bool exist=false; for(int j=0;j<3;j++) { if(page[i]==memory[j]) { exist=true; break; } } if (exist==false) { //############################### //begin to choose a memory page to replace int later=0; bool ok[3]; for (int j=0;j<3;j++) ok[j]=false; //check from i step -1 till 0 for(int j=i+1;j<12;j++) { for(int k=0;k<3;k++) { if (page[j]==memory[k]) { ok[k]=true; later++; break; } } if (later==2)break; } //check which ok ==false for (int j=0;j<3;j++) { if (ok[j]==false) { //replace this memory[j] count++; replace[pr]=memory[j]; pr++; memory[j]=page[i]; break; } } //############################# } } //output cout<<page[i]<<": [ "; for(int j=0;j<3;j++) { if (memory[j]==-1) cout<<"* "; else cout<<memory[j]<<" "; } cout<<"]"<<endl; } cout<<"######################"<<endl; cout<<"the lack page count = " <<count<<endl; cout<<"repalce pages are : "; for (int i=0;i<pr;i++) { cout<<replace[i]<<" "; } cout<<endl; cout<<"the rate of page lack is "<<count/12.0*100<<"%"<<endl; return 0;}
0 0
- 页面调度算法 FIFO,LRU,OPT,及C++代码
- FIFO、LRU、OPT页面调度算法及例子
- OPT, LRU, FIFO页面调度算法的模拟
- 页面置换算法(FIFO,LRU,OPT)c模拟
- FIFO, LRU, OPT 页面置换算法
- 页面置换算法-FIFO、LRU、OPT
- 页面置换算法总结-FIFO-LRU-OPT
- FIFO,LRU,OPT页面置换算法
- 页面置换算法 FIFO,OPT,LRU
- 操作系统模拟页面调度算法(OPT、FIFO、LRU)演示(vc6.0调试通过)
- 操作系统模拟页面调度算法(OPT、FIFO、LRU)演示(vc2010调试通过)(二)
- opt,fifo,lru算法实验
- FIFO,LRU,OPT置换算法
- 转载:操作系统页面置换算法(opt,lru,fifo,clock)实现
- JAVA实现页面置换算法(OPT,FIFO,LRU)
- 操作系统页面置换算法(opt,lru,fifo,clock)实现
- 关于缓存调度算法FIFO、LRU、OPT这三种置换算法的缺页次数
- 页置换算法FIFO、LRU、OPT
- 一句话概括下spring框架及spring cloud框架主要组件
- 百度天气预报接口介绍
- 凯撒密码
- delphi property
- 万能排序法compare 自定义排序规则
- 页面调度算法 FIFO,LRU,OPT,及C++代码
- iOS - 页面之间传值的实现 - block
- Android6.0手机权限处理工具类
- ElementUI案例演示:导航、布局、加载动画
- 如何在Android智能手机上捕获数据包?
- socket编程之addrinfo结构体与getaddrinfo函数
- 占楼
- 初涉python,用户登录功能
- MyBatis入门——动态SQL