页面置换算法java实现
时间: 2023-12-14 15:33:59 浏览: 94
以下是Java实现五种页面置换算法的示例代码:
1. Optimal算法
```java
public class Optimal {
public static void main(String[] args) {
int[] pages = {1, 3, 0, 3, 5, 6};
int[] frames = new int[3];
int faults = 0;
boolean full = false;
int farthest = 0;
int replace = 0;
for (int i = 0; i < pages.length; i++) {
int page = pages[i];
int j;
for (j = 0; j < frames.length; j++) {
if (frames[j] == page) {
break;
}
if (frames[j] == 0) {
frames[j] = page;
faults++;
full = false;
break;
}
if (j == frames.length - 1) {
full = true;
}
}
if (full) {
int[] temp = new int[frames.length];
boolean[] used = new boolean[frames.length];
for (int k = i + 1; k < pages.length; k++) {
for (int l = 0; l < frames.length; l++) {
if (pages[k] == frames[l] && !used[l]) {
temp[l] = k;
used[l] = true;
break;
}
if (l == frames.length - 1) {
temp[l] = Integer.MAX_VALUE;
}
}
}
farthest = 0;
replace = 0;
for (int k = 0; k < temp.length; k++) {
if (temp[k] > farthest) {
farthest = temp[k];
replace = k;
}
}
frames[replace] = page;
faults++;
}
System.out.print("Page " + page + ": ");
for (int k = 0; k < frames.length; k++) {
System.out.print(frames[k] + " ");
}
System.out.println();
}
System.out.println("Page faults: " + faults);
}
}
```
2. FIFO算法
```java
public class FIFO {
public static void main(String[] args) {
int[] pages = {1, 3, 0, 3, 5, 6};
int[] frames = new int[3];
int faults = 0;
int pointer = 0;
boolean full = false;
for (int i = 0; i < pages.length; i++) {
int page = pages[i];
int j;
for (j = 0; j < frames.length; j++) {
if (frames[j] == page) {
break;
}
if (frames[j] == 0) {
frames[j] = page;
faults++;
full = false;
break;
}
if (j == frames.length - 1) {
full = true;
}
}
if (full) {
frames[pointer] = page;
faults++;
pointer++;
if (pointer == frames.length) {
pointer = 0;
}
}
System.out.print("Page " + page + ": ");
for (int k = 0; k < frames.length; k++) {
System.out.print(frames[k] + " ");
}
System.out.println();
}
System.out.println("Page faults: " + faults);
}
}
```
3. LRU算法
```java
public class LRU {
public static void main(String[] args) {
int[] pages = {1, 3, 0, 3, 5, 6};
int[] frames = new int[3];
int faults = 0;
boolean full = false;
int replace = 0;
for (int i = 0; i < pages.length; i++) {
int page = pages[i];
int j;
for (j = 0; j < frames.length; j++) {
if (frames[j] == page) {
break;
}
if (frames[j] == 0) {
frames[j] = page;
faults++;
full = false;
break;
}
if (j == frames.length - 1) {
full = true;
}
}
if (full) {
int[] temp = new int[frames.length];
boolean[] used = new boolean[frames.length];
for (int k = i - 1; k >= 0; k--) {
for (int l = 0; l < frames.length; l++) {
if (pages[k] == frames[l] && !used[l]) {
temp[l] = k;
used[l] = true;
break;
}
if (l == frames.length - 1) {
temp[l] = Integer.MIN_VALUE;
}
}
}
replace = 0;
int min = temp[0];
for (int k = 1; k < temp.length; k++) {
if (temp[k] < min) {
min = temp[k];
replace = k;
}
}
frames[replace] = page;
faults++;
}
System.out.print("Page " + page + ": ");
for (int k = 0; k < frames.length; k++) {
System.out.print(frames[k] + " ");
}
System.out.println();
}
System.out.println("Page faults: " + faults);
}
}
```
4. LFU算法
```java
public class LFU {
public static void main(String[] args) {
int[] pages = {1, 3, 0, 3, 5, 6};
int[] frames = new int[3];
int[] counts = new int[3];
int faults = 0;
boolean full = false;
int replace = 0;
for (int i = 0; i < pages.length; i++) {
int page = pages[i];
int j;
for (j = 0; j < frames.length; j++) {
if (frames[j] == page) {
counts[j]++;
break;
}
if (frames[j] == 0) {
frames[j] = page;
counts[j]++;
faults++;
full = false;
break;
}
if (j == frames.length - 1) {
full = true;
}
}
if (full) {
replace = 0;
int min = counts[0];
for (int k = 1; k < counts.length; k++) {
if (counts[k] < min) {
min = counts[k];
replace = k;
}
}
frames[replace] = page;
counts[replace] = 1;
faults++;
}
System.out.print("Page " + page + ": ");
for (int k = 0; k < frames.length; k++) {
System.out.print(frames[k] + " ");
}
System.out.println();
}
System.out.println("Page faults: " + faults);
}
}
```
5. 改进型Clock算法
```java
public class Clock {
public static void main(String[] args) {
int[] pages = {1, 3, 0, 3, 5, 6};
int[] frames = new int[3];
boolean[] ref_bits = new boolean[3];
int faults = 0;
int pointer = 0;
for (int i = 0; i < pages.length; i++) {
int page = pages[i];
int j;
for (j = 0; j < frames.length; j++) {
if (frames[j] == page) {
ref_bits[j] = true;
break;
}
if (frames[j] == 0) {
frames[j] = page;
ref_bits[j] = true;
faults++;
break;
}
}
if (j == frames.length) {
while (ref_bits[pointer]) {
ref_bits[pointer] = false;
pointer++;
if (pointer == frames.length) {
pointer = 0;
}
}
frames[pointer] = page;
ref_bits[pointer] = true;
pointer++;
if (pointer == frames.length) {
pointer = 0;
}
faults++;
}
System.out.print("Page " + page + ": ");
for (int k = 0; k < frames.length; k++) {
System.out.print(frames[k] + " ");
}
System.out.println();
}
System.out.println("Page faults: " + faults);
}
}
```
阅读全文