作業系統頁面置換演算法之先進先出(FIFO)演算法
阿新 • • 發佈:2019-01-29
import java.util.LinkedList;
import java.util.List;
public class FIFO {
public static void main(String[] args) {
int framesize = 5; //幀數量
int[] s = { 1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6 };//引用串
int[] is = new int[s.length];
for (int i = 0; i < s.length; i++)
is[i] = 0;
int aa[][] = new int[s.length][framesize];
List<Integer> q = new LinkedList<Integer>();
int count = 0;
for (int i = 0; i < framesize; i++)
q.add(0);
for (int i = 0; i < s.length; i++) {
if (!q.contains(new Integer(s[i]))) {
q.remove(0);
q.add(new Integer(s[i]));
count++;
is[i] = 1;
}
int j = 0;
for (Integer v : q)
aa[i][j++] = v;
}
for (int i = 0; i < framesize; i++) {
for (int j = 0; j < s.length; j++) {
System.out.print(aa[j][i] + " ");
}
System.out.println();
}
System.out.println();
for (int i = 0; i < s.length; i++) {
System.out.print(is[i] + " ");
}
System.out.println();
System.out.println("total : " + count);
}
}
import java.util.List;
public class FIFO {
public static void main(String[] args) {
int framesize = 5; //幀數量
int[] s = { 1, 2, 3, 4, 2, 1, 5, 6, 2, 1, 2, 3, 7, 6, 3, 2, 1, 2, 3, 6 };//引用串
int[] is = new int[s.length];
for (int i = 0; i < s.length; i++)
is[i] = 0;
int aa[][] = new int[s.length][framesize];
List<Integer> q = new LinkedList<Integer>();
int count = 0;
for (int i = 0; i < framesize; i++)
q.add(0);
for (int i = 0; i < s.length; i++) {
if (!q.contains(new Integer(s[i]))) {
q.remove(0);
q.add(new Integer(s[i]));
count++;
is[i] = 1;
}
int j = 0;
for (Integer v : q)
aa[i][j++] = v;
}
for (int i = 0; i < framesize; i++) {
for (int j = 0; j < s.length; j++) {
System.out.print(aa[j][i] + " ");
}
System.out.println();
}
System.out.println();
for (int i = 0; i < s.length; i++) {
System.out.print(is[i] + " ");
}
System.out.println();
System.out.println("total : " + count);
}
}