#include <iostream>
using namespace std;
void FIFO(int[], int, int);
void LRU(int[], int, int);
void main() {
int len; //序列长度
int *list; //序列
int m; //页面数
cout<<"Please input the length of list: ";
cin>>len;
list = new int[len];
cout<<"Please input the list: ";
for(int i = 0; i < len; i++)
cin>>list[i];
cout<<"Please input the number of pages: ";
cin>>m;
cout<<endl<<"-----------------The result of FIFO:------------------"<<endl;
FIFO(list,len,m);
cout<<"------------------------------------------------------"<<endl;
cout<<endl<<"-----------------The result of LRU:------------------"<<endl;
LRU(list,len,m);
cout<<"-----------------------------------------------------"<<endl<<endl<<endl;
return ;
}
/************************************************
**函数功能:判断数组Arr中是否存在x
**函数参数:int Arr[]:整型数组
int Length:数组长度
int x:待查找元素
**返回值:数组Arr中存在x则返回位置i;否则返回-1
*************************************************/
int isExist(int Arr[], int Length, int x) {
for(int i = 0; i < Length; i++) {
if(Arr[i] == x) break;
}
if(i >= Length) return -1;
return i;
}
/************************************************
**函数功能:格式化输出数组Arr[]中的元素
**函数参数:int Arr[]:整型数组
int Length:数组长度
bool isMiss:是否缺页
**返回值:无
*************************************************/
void OResult(int Arr[], int Length, bool isMiss) {
cout<<"*";
for(int i = 0; i < Length; i++) {
cout<<' ';
if(Arr[i] != -1) cout<<Arr[i];
else cout<<' ';
cout<<' '<<"*";
}
if(isMiss) cout<<" +";
cout<<endl;
return ;
}
/************************************************
**函数功能:FIFO算法
**函数参数:int List[]:模拟序列
int Length:数组长度
int M:页面数
**返回值:无
*************************************************/
void FIFO(int List[], int Length, int M) {
int miss = 0; //缺页次数
bool ismiss = false; //是否缺页
int i,j;
int *page = new int[M]; //页面数组
for(i = 0; i < M; i++)
page[i] = -1;
for(i = 0; i < Length; i++) {
ismiss = false;
if(isExist(page,M,List[i]) == -1) {
ismiss = true;
miss++;
for(j = M - 1; j > 0; j--)
page[j] = page[j - 1];
page[0] = List[i];
}
cout<<" *************"<<endl<<List[i]<<" ->";
OResult(page,M,ismiss);
}
cout<<" *************"<<endl;
cout<<"The number of missing page is: "<<miss<<endl;
cout<<"The rate of missing page is: "<<miss*100/Length<<'%'<<endl;
return ;
}
/************************************************
**函数功能:LRU算法
**函数参数:int List[]:模拟序列
int Length:数组长度
int M:页面数
**返回值:无
*************************************************/
void LRU(int List[], int Length, int M) {
int pos = -1; //待查找元素位置
bool ismiss = false; //是否缺页
int miss = 0; //缺页次数
int i,j;
int *page = new int[M]; //页面数组
for(i = 0; i < M; i++)
page[i] = -1;
for(i = 0; i < Length; i++) {
ismiss = false;
pos = isExist(page,M,List[i]);
if(pos == -1) {
ismiss = true;
miss++;
for(j = M - 1; j > 0; j--)
page[j] = page[j - 1];
page[0] = List[i];
}
else if(pos > 0) {
int temp = page[pos];
for(j = pos; j > 0; j--)
page[j] = page[j - 1];
page[0] = temp;
}
cout<<" *************"<<endl<<List[i]<<" ->";
OResult(page,M,ismiss);
}
cout<<" *************"<<endl;
cout<<"The number of missing page is: "<<miss<<endl;
cout<<"The rate of missing page is: "<<miss*100/Length<<'%'<<endl;
return ;
}
FIFO & LRU
最新推荐文章于 2024-07-28 22:11:47 发布