BUAAOS3.2作业编程实现OPT,LRU,FIFO页面置换算法
本次编程用C++实现,代码可能冗余量大,敬请见谅。
可以一直输入得到结果,不用退出。
如有错误,请联系作者更改,谢谢大家的帮助。
#include<bits/stdc++.h>
using namespace std;
int input[] = {0, 9, 8, 4, 4, 3, 6, 5, 1, 5, 0, 2, 1, 1, 1, 1, 8, 8, 5,
3, 9, 8, 9, 9, 6, 1, 8, 4, 6, 4, 3, 7, 1, 3, 2, 9, 8, 6, 2, 9, 2, 7, 2, 7, 8, 4, 2, 3, 0, 1, 9, 4,
7, 1, 5, 9, 1, 7, 3, 4, 3, 7, 1, 0, 3, 5, 9, 9, 4, 9, 6, 1, 7, 5, 9, 4, 9, 7, 3, 6, 7, 7, 4, 5, 3, 5, 3, 1, 5, 6, 1,
1, 9, 6, 6, 4, 0, 9, 4, 3,-1};
void OPT(int n, int cnt)
{
int page[15] = {0};
int num = -1;
int total = 0;
int flag[15] = {0};//判断未来是否使用
for(int i = 0; i < cnt;i++){
if(num != n-1){
bool judge = false;
for(int j = 0; j <= num ;j++){
if(input[i]==page[j]){
judge = true;
break;
}
}
if(!judge){
total++;
}
page[++num] = input[i];
}else{
sort(page,page+n);
if(binary_search(page,page+n,input[i])){
continue;
}
total++;
memset(flag,0,sizeof(flag));
bool change = false;
for(int j = i; j < cnt;j++){
bool ischange = false;
for(int k = 0 ; k <= num ;k++){
if(page[k]==input[j]&&!ischange){
ischange = true;
flag[k] = 1;
}
}
int tmp = 0;
for(int k = 0; k <= num;k++){
if(!flag[k]) tmp++;
}
if(tmp==1){
for(int k = 0; k <= num;k++){
if(!flag[k]) {
page[k] = input[i];
change = true;
}
}
break;
}
}
if(!change){
for(int k = 0; k <= num;k++){
if(!flag[k]) {
page[k] = input[i];
break;
}
}
}
}
}
printf("OPT算法:页框数量为%d, 缺页总数为%d.\n",n,total);
}
void LRU(int n, int cnt)
{
int page[n+1];//建立页框
int num = -1;
int total = 0;
int count[n+1] = {0};//统计页面的使用次数
for(int i = 0; i < cnt;i++){
if(num != n-1){
bool judge = false;
for(int j = 0; j <= num ;j++){
if(input[i]==page[j]){
judge = true;
count[j] = 0;
for(int k = 0; k <= num ;k++){
if(k!=j){
count[k]++;
}
}
break;
}
}
if(!judge){
total++;
page[++num] = input[i];
for(int j = 0; j < num ; j++) count[j]++;
}
}else{
bool judge = false;
for(int j = 0; j <= num ;j++){
if(page[j]== input[i]){
judge = true;
count[j] = 0;
for(int k = 0; k <= num ;k++){
if(k!=j){
count[k]++;
}
}
break;
}
}
if(judge){
continue;
}
total++;
int max_num = -1,idx = -1;
for(int j = 0; j <= num ;j++){
if(count[j] > max_num){
max_num = count[j];
idx = j;
}
}
page[idx] = input[i];
count[idx] = 0;
for(int j = 0 ; j <= num ; j++){
if(j!=idx){
count[j]++;
}
}
}
}
printf("LRU算法:页框数量为%d, 缺页总数为%d.\n",n,total);
}
void FIFO(int n, int cnt)
{
queue<int> page;
int total = 0;
for(int i = 0 ; i < cnt; i++){
if(page.size()!=n){
bool flag = false;
int size = page.size();
for(int j = 0; j < size;j++){
if(page.front()==input[i]){//queue的遍历太笨了
flag = true;
}
page.push(page.front());
page.pop();
}
if(!flag){
total++;
page.push(input[i]);
}
}else{
bool flag = false;
int size = page.size();
for(int j = 0; j < size;j++){//复制粘贴有点憨
if(page.front()==input[i]){
flag = true;
}
page.push(page.front());
page.pop();
}
if(!flag){
page.push(input[i]);
page.pop();
total++;
}
}
}
printf("FIFO算法:页框数量为%d, 缺页总数为%d.\n",n,total);
}
int main()
{
int i = 0,cnt = 0;
while(input[i] != -1){
cnt++;
i++;
}
int n,chose;//chose是1,则为OPT算法,是2则为LRU算法,是3则为FIFO算法
while((scanf("%d%d",&n,&chose))!=-1){
if(chose == 1){
OPT(n,cnt);
}else if(chose == 2){
LRU(n,cnt);
}else if(chose == 3){
FIFO(n,cnt);
}else{
cout<<"Illgel input\n";
}
}
return 0;
}