标签:换算 cout 页面 flag 结束 turn frame 引用 理论
关于页面置换算法的理论知识:
https://www.bilibili.com/video/BV1YE411D7nH?p=45
C++代码实现(未优化)
#include<iostream> #include<fstream> #include<cstring> using namespace std; int frame_length;//帧个数 int string_length; char *st;//串 int Max(int *st,int length) { int max = st[0]; int max_index=0; for(int i=1;i<length;i++) { if(max<st[i]) { max = st[i]; max_index = i; } } return max_index; } int Far(char *frame,char *st,int i,int string_length) { int len1 = strlen(frame); int len2 = string_length; int *distances; distances = new int[len1];//储存各帧到下一个相同字符的距离 for(int j=0;j<len1;j++) //初始将距离设置为无穷大 distances[j]=65535; for(int k=0;k<len1;k++) { for(int j=i+1;j<len2&&(distances[k]==65535);j++) { if(st[j]==frame[k]) distances[k]=j-i; } } return Max(distances,len1); } int Past(char *frame,char *st,int i,int string_length) { int len1 = strlen(frame); int len2 = string_length; int *distances; distances = new int[len1];//储存各帧到下一个相同字符的距离 for(int j=0;j<len1;j++) //初始将距离设置为无穷大 distances[j]=65535; for(int k=0;k<len1;k++) { for(int j=i-1;j>=0&&(distances[k]==65535);j--) { if(st[j]==frame[k]) distances[k]=i-j; } } return Max(distances,len1); } void FIFO(char *st,int frame_length,int string_length) { int len=string_length; char *frame; int *life; int misno = 0; //错误次数 frame = new char[frame_length+1];//页帧 life = new int[frame_length];//各帧的占用时间 for(int i=0;i<frame_length;i++) life[i] = 0; cout<<"FIFO:"<<endl; for(int i=0;i<frame_length;i++)//页帧未满时 { misno++; frame[i] = st[i]; cout<<st[i]<<‘,‘; for(int j=0;j<frame_length;j++) { if(j<=i) { cout<<frame[j]<<‘ ‘; life[j]++; } else cout<<‘*‘<<‘ ‘; } cout<<endl; } int flag; for(int i=frame_length;i<len;i++)//遍历引用串 { flag = 0;//flag判断引用字符是否在内存中 for(int j=0;j<frame_length&&!flag;j++) { if(st[i]==frame[j]) flag=1;//引用已经在内存中,结束循环 } if(!flag)//引用字符不在内存中 { int max = Max(life,frame_length);//选出最旧的帧 frame[max] = st[i]; life[max] = 0; for(int j=0;j<frame_length;j++) life[j]++; misno++; cout<<st[i]<<‘,‘; for(int j=0;j<frame_length;j++) cout<<frame[j]<<‘ ‘; cout<<endl; } else { for(int j=0;j<frame_length;j++) life[j]++; flag = 0; cout<<st[i]<<endl; } } cout<<"错误次数:"<<misno<<endl; } void OPT(char *st,int frame_length,int string_length) { int len=string_length; char *frame; int *life; int misno = 0; //错误次数 frame = new char[frame_length+1];//页帧 cout<<"OPT:"<<endl; for(int i=0;i<frame_length;i++)//页帧未满时 { misno++; frame[i] = st[i]; cout<<st[i]<<‘,‘; for(int j=0;j<frame_length;j++) { if(j<=i) { cout<<frame[j]<<‘ ‘; } else cout<<‘*‘<<‘ ‘; } cout<<endl; } int flag; for(int i=frame_length;i<len;i++)//遍历引用串 { flag = 0;//flag判断引用字符是否在内存中 for(int j=0;j<frame_length&&!flag;j++) { if(st[i]==frame[j]) flag=1;//引用已经在内存中,结束循环 } if(!flag)//引用字符不在内存中 { int max = Far(frame,st,i,string_length);//选出离得最远的帧 frame[max] = st[i]; misno++; cout<<st[i]<<‘,‘; for(int j=0;j<frame_length;j++) cout<<frame[j]<<‘ ‘; cout<<endl; } else { flag = 0; cout<<st[i]<<endl; } } cout<<"错误次数:"<<misno<<endl; } void LRU(char *st,int frame_length,int string_length) { int len=string_length; char *frame; int misno = 0; //错误次数 frame = new char[frame_length+1];//页帧 cout<<"LRU:"<<endl; for(int i=0;i<frame_length;i++)//页帧未满时 { misno++; frame[i] = st[i]; cout<<st[i]<<‘,‘; for(int j=0;j<frame_length;j++) { if(j<=i) { cout<<frame[j]<<‘ ‘; } else cout<<‘*‘<<‘ ‘; } cout<<endl; } int flag; for(int i=frame_length;i<len;i++)//遍历引用串 { flag = 0;//flag判断引用字符是否在内存中 for(int j=0;j<frame_length&&!flag;j++) { if(st[i]==frame[j]) flag=1;//引用已经在内存中,结束循环 } if(!flag)//引用字符不在内存中 { int max = Past(frame,st,i,string_length);//选出离得最远的帧 frame[max] = st[i]; misno++; cout<<st[i]<<‘,‘; for(int j=0;j<frame_length;j++) cout<<frame[j]<<‘ ‘; cout<<endl; } else { flag = 0; cout<<st[i]<<endl; } } cout<<"错误次数:"<<misno<<endl; } int main() { ifstream infile("page.txt"); infile>>frame_length; infile>>string_length; st = new char[string_length+1]; for(int i=0;i<string_length;i++) { infile>>st[i]; } cout<<endl; FIFO(st,frame_length,string_length); cout<<endl; LRU(st,frame_length,string_length); cout<<endl; OPT(st,frame_length,string_length); cout<<endl; return 0; }
操作系统概念 页面置换算法:分别使用FIFO、OPT、LRU三种置换算法来模拟页面置换的过程。
标签:换算 cout 页面 flag 结束 turn frame 引用 理论
原文地址:https://www.cnblogs.com/Yindehao/p/13034636.html