我正在嘗試使用給定的頁面引用字符串執行FIFO分頁算法。該程序應該通過第1-4幀頁面,並說明有多少頁面故障用於該貫穿。它應該看起來像這樣:如何在每次迭代中再次開始零點?
"For x page frames:
FIFO had ### page faults.
LRU had ### page faults."
(我將繼續研究LRU)。
我很接近,但「團長」中的FIFO()方法不會重置每次都通過了。我已經嘗試了幾個我說「head = 0」的地方沒有任何工作。我想我需要另一雙眼睛。有沒有人看到我怎樣才能使每個迭代再次從零開始?預先感謝您的幫助!
import java.util.concurrent.ThreadLocalRandom;
public class Testfifo3 {
public static void main (String[] args){
Testfifo3 tf3 = new Testfifo3();
int[] array2 = {7,0,1,2,0,3,0}; //,4,2,3,0,3,2,1,2,0,1,7,0,1}; (testing with a shorter sample)
int[] frames = {1, 2, 3, 4}; //, 5, 6, 7}; (testing with a shorter sample)
//run the algorithms with array2:
//print out the array you'll be using.
System.out.println("Your string of pages is: ");
for (int i: array2){
System.out.print(i + " ");
}
System.out.println();
for(int fr: frames){
System.out.println("frames: " + fr);
tf3.fifo(array2, fr);
tf3.lru(array2);
}
}
public void fifo(int[] arr, int fr){
int faults = 0; //count faults
int[] frames = new int [fr]; //array to hold page frames
//start all the frames out at -1 (because "null" gives us a null pointer reference)
for (int c = 0; c<frames.length; c++){
frames[c] = -1;
}
int head = 0; //points to frame we need to add to
//nested loop for comparing frame contents with pages
//for each item in the page ref array, you'll loop through all the pages to compare
for(int i = 0; i < arr.length; i++){
for(int j= 0; j < frames.length; j++){
if (frames[j] == arr[i]){ //if the value in frames[j] equals value of page ref
frames[j] = arr[i]; //don't really change anything
break;
}
if (arr[i] != frames[j]){
frames[head] = arr[i]; //put this page ref value into head frame
if (head == frames.length -1){ //if head gets to the last page
head = 0; //reset head back at first frame
}
else{
head++; //increment head
}
faults++; //increment page faults
break; //go to next number in reference string
}
}
System.out.println("Head: " +head);
}
System.out.println("\n\t"+"FIFO has "+ faults +" page faults\n");
}
}
這原樣打印(「頭」打印語句只是用於測試):
Your string of pages is:
7 0 1 2 0 3 0
frames: 1
Head: 0
Head: 0
Head: 0
Head: 0
Head: 0
Head: 0
Head: 0
FIFO has 7 page faults
LRU has xxxxx page faults
frames: 2
Head: 1
Head: 0
Head: 1
Head: 0
Head: 1
Head: 0
Head: 0
FIFO has 6 page faults
LRU has xxxxx page faults
frames: 3
Head: 1
Head: 2
Head: 0
Head: 1
Head: 2
Head: 0
Head: 1
FIFO has 7 page faults
LRU has xxxxx page faults
frames: 4
Head: 1
Head: 2
Head: 3
Head: 0
Head: 1
Head: 2
Head: 2
FIFO has 6 page faults
LRU has xxxxx page faults
頭應始終在零
通過手做,我認爲3個幀,頭部應該是:0,1,2,0,1,1,2 – Ang