页面置换算法FIFO(Java)

package FIFO;

import java.util.ArrayList;

import java.util.List;

import java.util.Scanner;

class Page {

int value;

int time;

public Page(int value, int time) {

this.value = value;

this.time = time;

}

}

public class Test2 {

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

int n;

int base;//

int target = 0;// 命中

n = in.nextInt();

List<Page> list = new ArrayList<>();

for (int a = 0; a < n; a++) {

int x = in.nextInt();

Page page = new Page(x, 0);// 初始化

list.add(page);// 加入集合中

}

base = in.nextInt();

List<Page> targetlist = new ArrayList<>();// 定义淘汰的集合

List<Page> list_base1 = new ArrayList<>();// 定义容器集合

for (int a = 0; a < list.size(); a++) {

int flag = 0;// 默认不存在

// 判断新进来的进程的页面再内存中是否存在

for (int b = 0; b < list_base1.size(); b++) {

if (list_base1.get(b).value == list.get(a).value) {

target++;

flag = 1;

break;

}

}

//如果内存中没有

if (flag == 0) {

if (list_base1.size()<base) {

list_base1.add(list.get(a));// 将新的加入

} else {

int maxtime = -1;

int locat = 0;

// 在内存中找出存在时间最久的

for (int b = 0; b < list_base1.size(); b++) {

if (maxtime < list_base1.get(b).time) {

maxtime = list_base1.get(b).time;

locat = b;

}

}

targetlist.add(list_base1.get(locat));// 加入淘汰的

list_base1.set(locat, list.get(a));// 删除时间最久的并加入最新的;

}

}

//时间增加

for (int b = 0; b < list_base1.size(); b++) {

list_base1.get(b).time=list_base1.get(b).time+1;

}

}

System.out.println("淘汰页号:");

for (int a = 0; a < targetlist.size(); a++) {

System.out.print(targetlist.get(a).value + " ");

}

System.out.println("命中率:" + target + "/" + n);

}

}

猜你喜欢

转载自blog.csdn.net/weixin_45823684/article/details/129315924