页面置换算法 C++
1.代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int MAX_SIZE = 1e3+5;
const int INF = 0x3f3f3f3f;
int pg_last[MAX_SIZE];
bool vis[MAX_SIZE];
class Node
{
public:
int node[MAX_SIZE];
int iSize;
int cur;
bool isFull();
bool exist(int x);
Node()
{
cur = 0;
}
Node(int sz)
{
iSize = sz;
cur = 0;
}
};
struct Pair
{
int pos;
int id;
bool operator<(const Pair x)const
{
return id > x.id;
}
} op[MAX_SIZE];
bool Node::isFull()
{
return cur == iSize;
}
void createPages(int n,Node &pg)
{
cout<<"请输入长度为"<<n<<"的页面访问序列:";
pg.iSize = n;
for(int i = 0; i < n; ++i)
{
cin>>pg.node[i];
}
}
void printResult(int n,int fail_cnt,int replace_cnt)
{
double fail_rate = fail_cnt / double(n) * 100;
cout<<" 置换次数: "<<fail_cnt<<" 置换率: "<<fail_rate<<"%"<<endl;
cout<<" 缺页次数: "<<replace_cnt<<endl;
cout<<"--------------------------------------"<<endl;
}
bool Node::exist(int x)
{
for(int i = 0; i < cur; ++i)
{
if(x == node[i])
{
return true;
}
}
return false;
}
void FIFO(int m,Node pg)
{
Node st(m);
int fail_cnt = 0;
int replace_cnt = 0;
queue<int>q;
memset(vis,false,sizeof(vis));
for(int i = 0; i < pg.iSize; ++i)
{
if(vis[pg.node[i]]) continue;
if(int(q.size()) < m)
{
q.push(pg.node[i]);
vis[pg.node[i]] = true;
fail_cnt++;
}
else
{
int aim = q.front();
q.pop();
vis[aim] = false;
q.push(pg.node[i]);
vis[pg.node[i]] = true;
fail_cnt++;
replace_cnt++;
}
}
cout<<"-------------FIFO Result--------------"<<endl;
printResult(pg.iSize,fail_cnt,replace_cnt);
}
int findRepOPT(int s,Node st,Node pg)
{
int k = 0;
bool flag;
for(int i = 0; i < st.iSize; ++i)
{
flag = false;
for(int j = s; j < pg.iSize; ++j)
{
if(st.node[i] == pg.node[j])
{
flag = true;
op[k].id = j;
op[k++].pos = i;
break;
}
}
if(!flag)
{
return i;
}
}
sort(op,op+k);
return op[0].pos;
}
void OPT(int m,Node pg)
{
Node st(m);
int fail_cnt = 0;
int replace_cnt = 0;
for(int i = 0; i < pg.iSize; ++i)
{
if(st.exist(pg.node[i])) continue;
if(!st.isFull())
{
st.node[st.cur++] = pg.node[i];
fail_cnt++;
}
else
{
int pos = findRepOPT(i,st,pg);
st.node[pos] = pg.node[i];
fail_cnt++;
replace_cnt++;
}
}
cout<<"-------------OPT Result--------------"<<endl;
printResult(pg.iSize,fail_cnt,replace_cnt);
}
int findRepLRU(Node st)
{
int minx = INF,pos = 0;
for(int i = 0; i < st.iSize; ++i)
{
if(pg_last[st.node[i]] < minx)
{
minx = pg_last[st.node[i]];
pos = i;
}
}
return pos;
}
void LRU(int m,Node pg)
{
Node st(m);
int fail_cnt = 0;
int replace_cnt = 0;
for(int i = 0; i < pg.iSize; ++i)
{
pg_last[pg.node[i]] = i;
if(st.exist(pg.node[i])) continue;
if(!st.isFull())
{
st.node[st.cur++] = pg.node[i];
fail_cnt++;
}
else
{
int pos = findRepLRU(st);
st.node[pos] = pg.node[i];
fail_cnt++;
replace_cnt++;
}
}
cout<<"-------------LRU Result--------------"<<endl;
printResult(pg.iSize,fail_cnt,replace_cnt);
}
int main()
{
int m,n;
cout<<"请输入分配给进程的页框数 和 进程页面数:";
cin>>m>>n;
Node pg;
createPages(n,pg);
cout<<"-------------选择页面置换算法---------------"<<endl;
cout<<"-------------1.FIFO---------------"<<endl;
cout<<"-------------2.OPT---------------"<<endl;
cout<<"-------------3.LRU---------------"<<endl;
cout<<"-------------0.退出---------------"<<endl;
int op;
while(cin>>op)
{
switch(op)
{
case 1:
FIFO(m,pg);
break;
case 2:
OPT(m,pg);
break;
case 3:
LRU(m,pg);
break;
case 0:
exit(0);
}
}
return 0;
}
2.案例
3 20 7 0 1 2 0 3 0 4 2 3 0 3 2 1 2 0 1 7 0 1
|