📄 fifo.java
字号:
package 操作系统.各个置换算法;
import javax.swing.*;
public class Fifo {
public static final int PROGRAMLEN = 12;
public static final int PROGRAMNUM = 5;
public static final int PAGELEN = 3;
public static void main(String[] args) {
int[] program = new int[PROGRAMLEN]; // program为要装入的程序
for (int i = 0; i < PROGRAMLEN; i++) {
program[i] = (int) (Math.random() * PROGRAMNUM + 1);
}
// int[] program = { 5, 5, 5, 5, 1, 5, 2, 3, 1, 3, 3, 4 };//------5
// int[] program = { 2, 3, 2, 1, 5, 2, 4, 5, 3, 2, 5, 2 };// ------9
// fifo
// int[] program = { 4, 3, 5, 5, 1, 5, 3, 4, 1, 1, 4, 2 };// ------6
// int[] program = { 3, 1, 3, 2, 5, 2, 3, 4, 1, 4, 3, 5 };// ------8
// int[] program = {7,0,1,2,0,3,0,4,2,3,0,3,2,1,2,0,1,7,0,1};
for (int pro : program)
System.out.print(" " + pro);
System.out.println("");
int[] page = new int[PAGELEN];
for (int i = 0; i < PAGELEN; i++)
page[i] = 0;
int[] index = new int[PAGELEN];
for (int i = 0; i < PAGELEN; i++)
index[i] = 0;
int lostPage = 0; // 统计缺页次数
for (int i = 0; i < PROGRAMLEN; i++) {
int j = 0;
for (j = 0; j < PAGELEN; j++) {
if (program[i] == page[j])
break;
}
if (j < PAGELEN)
continue;
lostPage++;
for (j = 0; j < PAGELEN; j++)
if (page[j] == 0) {
page[j] = program[i];
break;
}
if (j < PAGELEN)
continue;
for (j = 0; j < PAGELEN - 1; j++)
page[j] = page[j + 1];
page[PAGELEN - 1] = program[i];
}
JOptionPane.showMessageDialog(null, "先进先出置换算法(FIFO)缺页 " + lostPage
+ " 次");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -