页面置换算法(FIFO,LRU)
作者:互联网
目录
FLFO(内存块3)
FLFO(内存块4)
算法实现(java实现)(复制时注意添加主函数)
static void first_in_first_out(int amount, int[] page, int memory_block) {
int[][] memory = new int[memory_block][amount];
String[] page_missing = new String[amount];
int i1 = memory_block;
for (int i = 0; i < memory_block; i++) {
for (int j = 0; j < memory_block; j++) {
if (j >= i) {
memory[i][j] = page[i];
} else {
memory[i][j] = -1;
}
}
page_missing[i] = "缺页";
}
for (int i = memory_block; i < amount; i++) {
int num = i1 % memory_block;
int count = 0;
for (int j = 0; j < memory_block; j++) {
if (memory[j][i - 1] == page[i]) {
count = 1;
break;
}
if (memory[j][i - 1] == -1 && i > memory_block) {
int control = i - 1;
while (memory[j][control] == -1) {
control--;
}
if (memory[j][control] == page[i]) {
count = 1;
break;
}
}
}
if (count == 0) {
for (int k = 0; k < memory_block; k++) {
if (memory[k][i - 1] != -1) {
memory[k][i] = memory[k][i - 1];
} else {
for (int j = i - 1; j > 0; j--) {
if (memory[k][j] != -1) {
memory[k][i] = memory[k][j];
break;
}
}
}
}
memory[num][i] = page[i];
i1++;
page_missing[i] = "缺页";
}
if (count == 1) {
for (int j = 0; j < memory_block; j++) {
memory[j][i] = -1;
}
page_missing[i] = "不缺页";
}
}
System.out.print("访问页面" + "\t");
for (int k = 0; k < amount; k++) {
System.out.print(page[k] + " ");
}
System.out.println(" ");
for (int i = 0; i < memory_block; i++) {
for (int k = 0; k < amount; k++) {
System.out.print(memory[i][k] + " ");
}
System.out.println("");
}
System.out.print("是否缺页" + "\t");
for (int k = 0; k < amount; k++) {
System.out.print(page_missing[k] + " ");
}
}
运行结果(内存块3)(-1表示null)
先进先出置换算法
访问页面 3 2 1 0 3 2 4 3 2 1 0 4
3 3 3 0 0 0 4 -1 -1 4 4 -1
-1 2 2 2 3 3 3 -1 -1 1 1 -1
-1 -1 1 1 1 2 2 -1 -1 2 0 -1
是否缺页 缺页 缺页 缺页 缺页 缺页 缺页 缺页 不缺页 不缺页 缺页 缺页 不缺页
Process finished with exit code 0
运行结果(内存块4)(-1表示null)
先进先出置换算法
访问页面 3 2 1 0 3 2 4 3 2 1 0 4
3 3 3 3 -1 -1 4 4 4 4 0 0
-1 2 2 2 -1 -1 2 3 3 3 3 4
-1 -1 1 1 -1 -1 1 1 2 2 2 2
-1 -1 -1 0 -1 -1 0 0 0 1 1 1
是否缺页 缺页 缺页 缺页 缺页 不缺页 不缺页 缺页 缺页 缺页 缺页 缺页 缺页
Process finished with exit code 0
LRU(内存块4)
算法实现(java实现)(复制时注意添加主函数)
static void last_unused(int amount, int[] page, int memory_block) {
int[][] memory = new int[memory_block][amount];
String[] page_missing = new String[amount];
int[] list = new int[memory_block];
int[] list1 = new int[memory_block];
memory[0][0] = page[0];
list[0] = page[0];
page_missing[0] = "缺页";
int count = 0;
int count1 = 1;
int number = 0;
for (int i = 1; i < memory_block; i++) {
memory[i][0] = -1;
list[i] = -1;
}
for (int i = 1; i < amount; i++) {
count = 0;
for (int j = 0; j < memory_block; j++) {
if(page[i] == list[j]){
count = 1;
}
}
if (count == 1){
for (int j = 0; j < memory_block; j++) {
memory[j][i] = -1;
}
page_missing[i] = "不缺页";
}
if(count == 0){
list[count1] = page[i];
count1++;
for (int j = 0; j < memory_block; j++) {
memory[j][i] = list[j];
}
page_missing[i] = "缺页";
}
if (list[memory_block-1] != -1){
number = i;
break;
}
}
for (int i = number+1; i < amount; i++) {
count = 0;
number = 0;
for (int j = 0; j < memory_block; j++) {
list1[j] = list[j];
}
for (int j = 0; j < memory_block; j++) {
if(list[j] == page[i]){
count = 1;
break;
}
}
if (count == 1){
for (int j = 0; j < memory_block; j++) {
memory[j][i] = -1;
}
page_missing[i] = "不缺页";
}
if(count == 0){
for (int j = i-1; j >= 0 ; j--) {
for (int k = 0; k < memory_block; k++) {
if (list1[k] == page[j]){
list1[k] = -1;
number++;
}
if (number == memory_block){
list[k] = page[i];
break;
}
}
if (number == memory_block){
break;
}
}
for (int j = 0; j < memory_block; j++) {
memory[j][i] = list[j];
}
page_missing[i] = "缺页";
}
}
System.out.print("访问页面" + "\t");
for (int k = 0; k < amount; k++) {
System.out.print(page[k] + " ");
}
System.out.println(" ");
for (int i = 0; i < memory_block; i++) {
for (int k = 0; k < amount; k++) {
System.out.print(memory[i][k] + " ");
}
System.out.println("");
}
System.out.print("是否缺页" + "\t");
for (int k = 0; k < amount; k++) {
System.out.print(page_missing[k] + " ");
}
}
运行结果(内存块4)(-1表示null)
最近最久未用置换算法
访问页面 1 8 1 7 8 2 7 2 1 8 3 8 2 1 3 1 7 1 3 7
1 1 -1 1 -1 1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1 1 -1 -1 -1
-1 8 -1 8 -1 8 -1 -1 -1 -1 8 -1 -1 -1 -1 -1 7 -1 -1 -1
-1 -1 -1 7 -1 7 -1 -1 -1 -1 3 -1 -1 -1 -1 -1 3 -1 -1 -1
-1 -1 -1 -1 -1 2 -1 -1 -1 -1 2 -1 -1 -1 -1 -1 2 -1 -1 -1
是否缺页 缺页 缺页 不缺页 缺页 不缺页 缺页 不缺页 不缺页 不缺页 不缺页 缺页 不缺页 不缺页 不缺页 不缺页 不缺页 缺页 不缺页 不缺页 不缺页
Process finished with exit code 0
主函数
注释部分为自由输入(也可以选择非注释部分代码块里面输入)
import java.util.Scanner;
//置换算法
public class replacement {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
/* System.out.println("请输入待访问页面数量");
int amount = scanner.nextInt();
System.out.println("请输入需要访问的访问页面");
int[] page = new int[amount];
for (int i = 0; i < amount; i++) {
page[i] = scanner.nextInt();
}
System.out.println("请输入内存块大小");
int memory_block = scanner.nextInt();*/
int amount = 20;
int[] page = new int[]{1,8,1,7,8,2,7,2,1,8,3,8,2,1,3,1,7,1,3,7};
int memory_block = 4;
System.out.println("先进先出置换算法");
first_in_first_out(amount, page, memory_block);
System.out.println("\n最近最久未用置换算法");
last_unused(amount, page, memory_block);
}
}
补充:图片来源于王道考研-操作系统
标签:int,FIFO,++,LRU,memory,缺页,page,block,页面 来源: https://blog.csdn.net/m0_46708851/article/details/121552244