c++实现最佳置换算法,FIFO,LRU,CLOCK置换算法代码,结果用表格输出,表格中包括时刻、页访问序列、物理块、缺页、置换、命中,算出置换率、缺页率、命中率并在表格下方显示
时间: 2023-07-10 22:13:30 浏览: 93
好的,下面是针对你的问题给出的 C++ 实现最佳置换算法、FIFO 算法、LRU 算法、CLOCK 算法的代码:
```c++
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <iomanip>
using namespace std;
// 最佳置换算法
int optimal(vector<int>& pages, int n, int m) {
int cnt = 0, pos = 0;
vector<int> frames(m, -1);
vector<int> nxt(m, -1);
for (int i = 0; i < m; ++i) {
frames[i] = pages[i];
++cnt;
for (int j = i + 1; j < n; ++j) {
if (find(frames.begin(), frames.end(), pages[j]) == frames.end()) {
nxt[i] = j;
break;
}
}
if (nxt[i] == -1) {
return 0;
}
}
int ans = 0;
for (int i = m; i < n; ++i) {
if (find(frames.begin(), frames.end(), pages[i]) == frames.end()) {
int idx = 0, farthest = nxt[0];
for (int j = 1; j < m; ++j) {
if (nxt[j] == -1) {
idx = j;
break;
}
if (nxt[j] > farthest) {
farthest = nxt[j];
idx = j;
}
}
frames[idx] = pages[i];
nxt[idx] = -1;
for (int j = i + 1; j < n; ++j) {
if (find(frames.begin(), frames.end(), pages[j]) == frames.end()) {
nxt[idx] = j;
break;
}
}
if (nxt[idx] == -1) {
return 0;
}
++ans;
}
}
return ans;
}
// FIFO 算法
int fifo(vector<int>& pages, int n, int m) {
int cnt = 0, pos = 0;
vector<int> frames(m, -1);
queue<int> q;
for (int i = 0; i < n; ++i) {
if (find(frames.begin(), frames.end(), pages[i]) == frames.end()) {
if (cnt < m) {
frames[cnt++] = pages[i];
q.push(pages[i]);
} else {
int t = q.front();
q.pop();
auto it = find(frames.begin(), frames.end(), t);
*it = pages[i];
q.push(pages[i]);
}
++pos;
}
}
return pos;
}
// LRU 算法
int lru(vector<int>& pages, int n, int m) {
int cnt = 0, pos = 0;
vector<int> frames(m, -1);
vector<int> recent(m, 0);
for (int i = 0; i < n; ++i) {
if (find(frames.begin(), frames.end(), pages[i]) == frames.end()) {
if (cnt < m) {
frames[cnt++] = pages[i];
} else {
int idx = 0, mn = recent[0];
for (int j = 1; j < m; ++j) {
if (recent[j] < mn) {
mn = recent[j];
idx = j;
}
}
frames[idx] = pages[i];
}
++pos;
}
for (int j = 0; j < m; ++j) {
if (frames[j] == pages[i]) {
recent[j] = i;
break;
}
}
}
return pos;
}
// CLOCK 算法
int clock(vector<int>& pages, int n, int m) {
int cnt = 0, pos = 0, hand = 0;
vector<int> frames(m, -1);
vector<int> flag(m, 0);
for (int i = 0; i < n; ++i) {
if (find(frames.begin(), frames.end(), pages[i]) == frames.end()) {
while (flag[hand]) {
flag[hand] = 0;
++hand;
if (hand == m) {
hand = 0;
}
}
frames[hand] = pages[i];
flag[hand] = 1;
++pos;
hand = (hand + 1) % m;
}
}
return pos;
}
// 输出表格
void printTable(int n, int m, vector<int>& pages, vector<int>& ans) {
double miss = (double)ans[0] / n, rate = 1 - miss;
cout << "+--------------+-----------------+-----------------+-----------------+-----------------+-----------------+" << endl;
cout << "| 时刻 | 页访问序列 | 物理块 | 缺页 | 置换 | 命中 |" << endl;
cout << "+--------------+-----------------+-----------------+-----------------+-----------------+-----------------+" << endl;
int idx = 0;
for (int i = 0; i < n; ++i) {
cout << "|" << setw(14) << i + 1 << "|";
cout << setw(17) << pages[i] << "|";
if (ans[idx] == 0) {
cout << setw(17) << "-" << "|";
} else {
for (int j = 0; j < m; ++j) {
if (ans[idx] == frames[j]) {
cout << setw(17) << ans[idx] << "|";
break;
}
}
}
if (ans[idx] == 0) {
cout << setw(17) << "Yes" << "|";
} else {
cout << setw(17) << "No" << "|";
}
if (ans[idx] != 0) {
cout << setw(17) << ans[idx] << "|";
} else {
cout << setw(17) << "-" << "|";
}
if (ans[idx] == 0) {
cout << setw(17) << "Yes" << "|" << endl;
} else {
cout << setw(17) << "No" << "|" << endl;
}
if (ans[idx] == 0) {
++idx;
}
}
cout << "+--------------+-----------------+-----------------+-----------------+-----------------+-----------------+" << endl;
cout << "缺页率: " << fixed << setprecision(2) << miss << ",命中率: " << fixed << setprecision(2) << rate << endl;
}
int main() {
vector<int> pages = {1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6};
int n = pages.size(), m = 3;
vector<int> ans(4, 0);
ans[0] = optimal(pages, n, m);
ans[1] = fifo(pages, n, m);
ans[2] = lru(pages, n, m);
ans[3] = clock(pages, n, m);
printTable(n, m, pages, ans);
return 0;
}
```
上述代码中,我们通过 `optimal` 函数实现了最佳置换算法,通过 `fifo` 函数实现了 FIFO 算法,通过 `lru` 函数实现了 LRU 算法,通过 `clock` 函数实现了 CLOCK 算法。我们将这些算法的结果保存在一个长度为 4 的 vector 中,最后用 `printTable` 函数输出表格。
在输出表格的时候,我们使用了 `setw()` 函数来控制表格中各列的宽度,用 `fixed` 和 `setprecision()` 函数来控制输出结果的精度。最后,我们在表格下方输出了缺页率和命中率。
阅读全文