如何在每次迭代时将头部重新开始为零?

时间:2017-06-23 02:00:38

标签: java loops

我正在尝试使用给定的页面引用字符串执行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");   
    }           
}   

这个原样打印(“Head”打印语句仅用于测试):

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 个答案:

没有答案