页面调度算法 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;
}