私は、指定されたページ参照文字列でFIFOページングアルゴリズムを実行しようとしています。プログラムはページフレーム1-4を通り、そのランスルーにいくつのページフォルトがあったかを述べる必要があります。これは次のようになります。どのようにして各繰り返しでヘッドをゼロから開始させることができますか?
"For x page frames:
FIFO had ### page faults.
LRU had ### page faults."
(私は次のLRUで作業する予定です)。
私は近づいていますが、fifo()メソッドの "head"は毎回リセットされません。私は「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");
}
}
(「ヘッド」print文は単なるテストのためです):
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