先进先出置换算法(FIFO)
程序员文章站
2022-03-14 15:49:08
...
#include<iostream>
#include<cstdio>
using namespace std;
bool find(int number);
void print(int * array, int length);
int page[25] = {0, 7, 0, 1, 2, 0, 3, 0, 4, 2, 3, 0, 3,
2, 1, 2, 0, 1, 7, 0, 1};//第一个元素不算
const int memory_len = 4, page_len = 20;//内存大小, 页的大小
int memory[memory_len+1];
int main() {
int memory_pos = 1, page_pos = memory_len+1;//内存位置和页的位置
printf("页面置换过程:\n");
for (int i = 1; i <= memory_len; i++) {
if (find(page[i])) memory[i] = page[i];
print(memory, i);
}
while (page_pos <= page_len) {
if (find(page[page_pos])) {
if (memory_pos > memory_len) memory_pos = 1;
memory[memory_pos++] = page[page_pos];
page_pos++;
print(memory, memory_len);
}else {
page_pos++;
}
}
return 0;
}
void print(int * array, int length) {
for (int i = 1; i <= length; i++) {
printf("%d ", array[i]);
}
printf("\n");
}
bool find(int number) {
for (int i = 1; i <= memory_len; i++) {
if (memory[i] == number) return false;
}
return true;
}
上一篇: excel工作簿插入删除行列
下一篇: Excel2007批注如何删除,编辑