请根据我接下来的代码风格写一段最近未使用算法(NRU)void Fifo() { int clock = 1; //记录时间 double totalnum = 0; //指令不在内存的次数 for (int i = 0; i < 320; i++) { int numin = 0; for (int k = 0; k < 32; k++) { if (page[k].in) numin++; } if (page[order[i].numpage].in == 1) continue; //指令所在页在内存中 if (page[order[i].numpage].in == 0) //指令所在页不在内存中 { if (numin < size) { totalnum++; //缺页发生,加入内存 page[order[i].numpage].in = 1; page[order[i].numpage].time = clock; clock++; // cout<<"进入 "<<order[i].numpage<<endl; } else { int Max = max; int kk; for (int k = 0; k < 32; k++) { if (page[k].in && page[k].time < Max) { Max = page[k].time; kk = k; } } page[kk].in = 0; // cout<<"退出 "<<kk<<endl; totalnum++; //缺页发生,加入内存 page[order[i].numpage].in = 1; page[order[i].numpage].time = clock; clock++; // cout<<"进入 "<<order[i].numpage<<endl; } } } ratio = 1 - (totalnum / 320.0); cout << "失效次数: " << totalnum << endl; cout << "命中率: " << (int)(ratio * 100) << "%" << endl;
时间: 2023-11-27 20:49:35 浏览: 64
以下是符合你代码风格的最近未使用算法(NRU)void Fifo()的代码:
```
void Fifo() {
int clock = 1; // 记录时间
double totalnum = 0; // 指令不在内存的次数
for (int i = 0; i < 320; i++) {
int numin = 0;
for (int k = 0; k < 32; k++) {
if (page[k].in) numin++;
}
if (page[order[i].numpage].in == 1) continue; // 指令所在页在内存中
if (page[order[i].numpage].in == 0) { // 指令所在页不在内存中
if (numin < size) {
totalnum++; // 缺页发生,加入内存
page[order[i].numpage].in = 1;
page[order[i].numpage].time = clock;
clock++;
// cout<<"进入 "<<order[i].numpage<<endl;
} else {
int Max = max;
int kk;
for (int k = 0; k < 32; k++) {
if (page[k].in && page[k].time < Max) {
Max = page[k].time;
kk = k;
}
}
page[kk].in = 0;
// cout<<"退出 "<<kk<<endl;
totalnum++; // 缺页发生,加入内存
page[order[i].numpage].in = 1;
page[order[i].numpage].time = clock;
clock++;
// cout<<"进入 "<<order[i].numpage<<endl;
}
}
}
ratio = 1 - (totalnum / 320.0);
cout << "失效次数: " << totalnum << endl;
cout << "命中率: " << (int)(ratio * 100) << "%" << endl;
}
```
阅读全文