<pre name="code" class="cpp">LRU页面置换算法,将帧内最近不常使用的置换出
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
using namespace std;
int returnMaxValueInVec(vector<int>str)
{
int postion = 0;
int length = str.size();
if (str.size() == 0)
{
return -1;
}
else
{
for (int i = 0; i < length; i++)
{
if (str[i] == 0)
{
return i;
}
}
int max = str[0];
for (int i = 1; i < length; i++)
{
if (max < str[i])
{
max = str[i];
postion = i;
}
}
}
return postion;
}
int main(int argc, char argv[])
{
int length = 0;
while (true)
{
cout << "输入分配帧长度:";
cin >> length;
if (length == 0)
{
break;
}
vector<int>vec(length, -1);
vector<int>str;
ifstream inFile("test.txt");
int number;
bool threadInVec = false;
int num = 0;
int count = 0;
int indexInStr = 0;
int allCount = 0;
vector<int>timeOfVec(length);
while (!inFile.eof())
{
inFile >> number;
str.push_back(number);
indexInStr++;
for (auto iter = vec.begin(); iter != vec.end(); iter++)
{
if (number == *iter)
{
threadInVec = true;
break;
}
}
if (threadInVec == true)
{
threadInVec = false;
//do nothing
}
else
{
if (num < length)
{
vec[num++] = number;
}
else
{
int j = 0;
for (auto iter = vec.begin(); iter != vec.end(); iter++)
{
int k = 0;
bool Flag = true;
for (int i = indexInStr - 2; i > indexInStr - 2 - length; i--)
{
k++;
if (*iter == str[i])
{
timeOfVec[j] = k;
Flag = false;
break;
}
}
if (Flag == true)
{
timeOfVec[j] = 0;
}
j++;
}
int pos = returnMaxValueInVec(timeOfVec);
vec[pos] = number;
}
count++;
}
for (int i = 0; i < length; i++)
{
if (vec[i] >= 0 && vec[i] <= 9)
{
cout << vec[i] << " ";
}
else
{
cout << "$" << " ";
}
}
cout << endl;
allCount++;
}
cout << "分配帧数为:" << length << "的缺页率为:" << count / (double)allCount << endl;
}
return 0;
}
<pre name="code" class="cpp">//最优页面置换算法,该算法的核心是,考虑当前帧内进程在将来最长时间不会被使用的页将会被替换
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
int returnPosMaxValueInVec(vector<int>str)
{
int postion = 0;
int length = str.size();
if (str.size() == 0)
{
return -1;
}
else
{
for (int i = 0; i < length; i++)
{
if (str[i] == 0)
{
return i;
}
}
int max = str[0];
for (int i = 1; i < length; i++)
{
if (max < str[i])
{
max = str[i];
postion = i;
}
}
}
return postion;
}
int main(int argc, char argv[])
{
int posInStr;
int count;
bool flag = true;
ifstream inFile("test.txt");
vector<int>str;
int number;
while (!inFile.eof())
{
inFile >> number;
str.push_back(number);
}
int Length = str.size();
while (flag)
{
count = 0;
int length = 0;
cout << "输入分配帧长度:";
cin >> length;
bool threadInVec = false;
vector<int>vec(length,-1);
vector<int>timeForUse(length);
if (length == 0)
{
break;
}
else
{
int posInVec = 0;
posInStr = 0;
while (posInStr < Length)
{
for (int i = 0; i < length; i++)
{
if (str[posInStr] == vec[i])
{
threadInVec = true;
break;
}
}
if (threadInVec == true)
{
threadInVec = false;
}
else
{
if (posInVec < length)
{
vec[posInVec] = str[posInStr];
posInVec++;
}
else
{
//找出在进程中,剩余中最后使用的进程
int j = 0;
for (vector<int>::iterator iter = vec.begin(); iter != vec.end(); iter++)
{
int num = 0;
for (int i = posInStr + 1; i < Length; i++)
{
num++;
if (*iter == str[i])
{
timeForUse[j] = num;
break;
}
else
{
timeForUse[j] = 0;
}
}
j++;
}
//找出在数组中最大的数值,并返回该数值的位置
vec[returnPosMaxValueInVec(timeForUse)] = str[posInStr];
count++;
}
}
posInStr++;
for (int i = 0; i < length; i++)
{
if (vec[i] >= 0 && vec[i] <= 9)
{
cout << vec[i] << " ";
}
else
{
cout << "$" << " ";
}
}
cout << endl;
}
}
cout << "当前缺页率:" << count / double(str.size()) << endl;
}
}
<pre name="code" class="cpp">//页面置换算法
//1:FIFO
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
int main(int argc, char argv[])
{
//FIFO的主要思想是:如在内存中必须置换出一页时
//应该选择将最旧的一页置换出去
//变量count计算缺页的数量,来计算缺页率,用allCount来计算总共有多少线程需要进来
int count = 0;
int allCount = 0;
bool flag = true;
//定义一个程序分几页
int length = 0;
//该bool变量的用处是,表示当前帧内是否有要进入的进程
bool inThread = false;
//该变量表示要置换出去的是那一页
int outNumber = 0;
while (flag)
{
cout << "输入分配帧数:";
cin >> length;
vector<int>vec(length,-1);
ifstream inFile("test.txt");
count = 0;
allCount = 0;
outNumber = 0;
inThread = false;
if (length == 0)
{
flag = false;
}
if (flag == true)
{
int number;
while (!inFile.eof())
{
inFile >> number;
for (int i = 0; i < length; i++)
{
if (number == vec[i])
{
inThread = true;
break;
}
}
if (inThread == true)
{
//该情况下证明此时要进入的帧已经存在当前帧内了
inThread = false;
}
else
{
//否则,将该进程加载到当前帧内,并使缺页数加一
if (allCount < length)
{
//当数组为满是,持续放入数组
vec[allCount] = number;
}
else
{
//当数组满时,开始页面置换
vec[outNumber++] = number;
if (outNumber >= length)
{
outNumber = 0;
}
}
count++;
}
//打印出当前帧内的情况
for (int i = 0; i < length; i++)
{
if (vec[i] >= 0 && vec[i] <= 9)
{
cout << vec[i] << " ";
}
else
{
cout << "$" << " ";
}
}
allCount++;
cout << endl;
}
}
cout << "当前帧数为 " << length << " 的缺页率为:" << count / (double)allCount << endl;
}
}