Wednesday 25 January 2017

First In First Out (FIFO) algorithm for demand paging in java


public class FIFO {
    private String input;
    private int frameSize;
    private int falutCount = 0;
    private int stackPointer = 0;
    private String[] frameStack; /** * * @param commaSepratedPages * Page String will be like '7,0,1,2,0,3,0' * @param frameSize * Frame Size that can hold Pages. */
    public FIFO(String commaSepratedPages, int frameSize) {
        this.frameSize = frameSize;
        frameStack = new String[frameSize];
        input = commaSepratedPages;
    } /** * Number of page faults with given Frame Size and Input. * @return */
    public int getPageFaults() {
        falutCount = 0;
        stackPointer = 0;
        String[] pages = input.split(",");
        System.out.println("Pages :\n " + input);
        System.out.println("Fault Pages ");
        for (String page: pages) {
            updateStack(page);
        }
        return falutCount;
    }
    private void updateStack(String page) {
        boolean isFaultOccured = true;
        for (int i = 0; i & lt; frameSize; i++) {
            if (page.equals(frameStack[i])) {
                isFaultOccured = false;
                break;
            }
        }
        if (isFaultOccured) {
            System.out.print(page + " ");
            falutCount++;
            frameStack[stackPointer] = page;
            updateStackPointer();
        }
    }
    private void updateStackPointer() {
        stackPointer++;
        if (stackPointer & gt; = frameSize) {
            stackPointer = 0;
        }
    }
}