Implementing FIFO Page Replacement Algorithm in Java - Incorrect Output

196 Views Asked by At

I am implementing a page replacement algorithm for a computer operating system. Specifically, I am trying to implement the First-In-First-Out (FIFO) algorithm.

The algorithm works by maintaining a list of pages currently in memory, as well as a queue that keeps track of the order in which pages were added to memory. When a new page is requested, the algorithm checks if the page is already in memory. If it is not, and there is space in memory for the page, the page is added to both the list and the queue. If there is no space in memory, the oldest page in memory (i.e. the page that was added to memory first, according to the queue) is removed from memory, and the new page is added in its place.

I have written code to implement the algorithm, but it is not producing the correct output. Specifically, when I run the algorithm on the following input:

Page-Reference String: 2,6,9,2,4,2,1,7,3,0,5,2,1,2,9,5,7,3,8,5

I expect the output to be:

FIFO: 2, 2, 2, 1, 1, 1, 9, 9, 9, 0, 0, 5, 5, 5, 5, 7, 7, 7, 8, 8 Number of page faults: 13

However, my code is producing the following output:

FIFO: [18]

public static int FIFO(String referenceString) {
        int pageFaults = 0; // initialize the page fault count
        List<Integer> pages = new ArrayList<>(); // list to store pages in memory
        Queue<Integer> fifoQueue = new LinkedList<>(); // queue to implement FIFO

        for (String pageStr : referenceString.split(",")) {
            int page = Integer.parseInt(pageStr); // convert page from string to integer

            // If the page is not present in memory, verify if there is a vacant frame available or remove the page that was last accessed.
            if (!pages.contains(page)) {
                if (pages.size() < 3) { // check if there is an empty frame available (assuming 3 frames)
                    pages.add(page);
                    fifoQueue.add(page);
                } else { // if no empty frame is available, evict the page that was least recently used (FIFO)
                    int evictedPage = fifoQueue.poll(); // remove the least recently used page from the queue
                    pages.remove(Integer.valueOf(evictedPage)); // remove the least recently used page from memory
                    pages.add(page); // add the new page to memory
                    fifoQueue.add(page); // add the new page to the queue
                }
                pageFaults++; // increment the page fault count
            }
        }

        return pageFaults; // return the total number of page faults
    }


 public static void main(String[] args) {
        // list of reference strings
        String[] referenceStrings = { "2,6,9,2,4,2,1,7,3,0,5,2,1,2,9,5,7,3,8,5",
                "0,6,3,0,2,6,3,5,2,4,1,3,0,6,1,4,2,3,5,7",
                "3,1,4,2,5,4,1,3,5,2,0,1,1,0,2,3,4,5,0,1",
                "4,2,1,7,9,8,3,5,2,6,8,1,0,7,2,4,1,3,5,8",
                "0,1,2,3,4,4,3,2,1,0,0,1,2,3,4,4,3,2,1,0" };
        // iterate through each reference string
        for (String referenceString : referenceStrings) {
            // calculate page faults using different page replacement algorithms
            // int opt = OPT(referenceString);
            int fifo = FIFO(referenceString);
            // int lru = LRU(referenceString);
            // int sec = secondChance(referenceString);
            // print the page reference string and the number of page faults for each algorithm
            System.out.printf("Page-Reference String: [%s]\n", referenceString);
            System.out.printf("OPT: [%d]\tFIFO: [%d]\tLRU: [%d]\tSEC: [%d]\n\n", opt, fifo, lru,
                    sec);
        }
    }

I would appreciate any help in identifying and fixing the issue with my code.

0

There are 0 best solutions below