2017-06-23 39 views
0

我正在嘗試使用給定的頁面引用字符串執行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 

頭應始終在零

+0

通過手做,我認爲3個幀,頭部應該是:0,1,2,0,1,1,2 – Ang

回答

0

開始誰也不能保證,這個代碼返回head=0只:

  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 
      } 

如果條件滿足,head得到增加。 break僅結束if聲明,並且可以刪除。

+0

到那個「休息」,我覺得他想「繼續」代替。無論如何,因爲循環遇到它,就像你說的,可以被刪除。 –

+0

當我把break語句時,該頁面引用循環多次框架...所以1幀= 7 0 1 2 0 3 0,2幀= 7 7 0 0 1 1 2 2 0 0 3 3 0 0,3幀循環:7 7 7 0 0 0 1 1 1 2 2 2 0 0 0 3 3 3 0 0 0 ...等等.... – Ang

+0

我不是說你的代碼是正確的,我只是確定它沒有意義。在這個代碼中我還沒有看到任何FIFO(對不起)。 –