Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Page Replacement Algorithms

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Page Replacement Algorithms

    Hello I'm new to the forum and have had about a year of java programming but am still not very fluent,

    My current problem is that I need to demonstrate 2 page replacement algorithms such as FIFO and LRU. There are 3 inputs that the user should put into a command prompt to set the parameters:

    1. the size of the process in pages between 10-100
    2. the size of memory in frames 1-50
    3. how long the process will run in terms of page references between 10-10,000.

    3 data structures are necessary:

    1. a page table, with one entry for each page in the process
    2. a list to hold the page references
    3. a list to represent memory so you will know which frames are used and which are available.

    each entry in the page table will contain an indicator as to whether that page is in memory and if so, what frame it is in.

    Each entry in the memory list will contain a flag telling you whether that frame is empty or in use.

    The program should genereate a random list of page references. Page numbers start at 0 and go up to one less than the process size. So if your process has 50 pages they will be numbered 0 - 49.

    write two output text files:
    1. a summary file with counts of page faults vs. page references for each algorithm
    2. a detail file with a list of page references for each algorithm, whether it was a page fault or not, where in memory it was placed, and what page (if any ) it replaced.

    I have no code so far and am kind of at a loss where to start. If anyone could point me to an example or give me an example or give me some steps in the right direction i'd be most greatful!

    Thank you in advance!


  2. #2
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Page Replacement Algorithms

    here is the code i've assembled so far... I am having some errors compiling it but I believe it is executing the FIFO method. Any suggestions to why my code isn't working ?

    /* explaining how to capture user input via the console
    At the top of the program "import java.util.Scanner;"
     
    in the main thread (public static void main())
     
    you can do something like ->
     
    	Scanner in = new Scanner(System.in);
    	System.out.println("Input pages :");
    	int pages = in.nextInt();
     
    	System.out.println("Input frames :");
    	int frames = in.nextInt();
     
    	System.out.println("Input process run time :");
    	int runtime = in.nextInt();
     
    	in.close(); 	//to avoid extra memory and so on ;)
     
     
    Now you got the 3 variables needed instead of hardcoding them later on //you can make checks on each of the inputs to make sure they meet the standard (ie, they're in the correct range) before committing it to a variable
    */
     
     
    ---------------------------------------->
     
     
     
    class Page {
    private boolean isInMemory = false;
    private int pageNumber;
     
    	public Page(int number) {pageNumber = number;}
     
    	public void setActive() {isInMemory = true; }
    	public boolean getActive() {return isInMemory; }
    }
     
     
    ---------------------------------------->
     
    class Memory {
    private Page page = null;
     
    	public Memory(Page p) {page = p;}
     
    	public Page getPage() {return page; }
    }
     
     
    ---------------------------------------->
     
     
    public static void main(String[] args) {
     
    	Scanner in = new Scanner(System.in);
    	System.out.println("Input pages :");
    	int pagesIn = in.nextInt();
     
    	System.out.println("Input frames :");
    	int framesIn = in.nextInt();
     
    	System.out.println("Input process run time :");
    	int runtimeIn = in.nextInt();
     
    	in.close(); 	//to avoid extra memory and so on ;)
     
     
     
    	int pages = pages; ;
    	int memory = framesIn;
    	int pageReferences = runtimeIn;		//process
     
    	Page[] pageTable = new page[pages];
     
    	for(int i = 0; i<pages; i++) {
    		pageTable[i] = new Page(int i);
    	}	
     
    	ArrayList<Memory> memList = new ArrayList<Memory>();
    	ArrayList<Page> pageList = new ArrayList<Page>();
     
    	for(int i = 0; i<pageReferences; i++) {
    		int pageIndex = Math.rand()*pages;	//make a random number between 1 and 20 (the number of pages)
    		Page p = pageTable[pageIndex];
    		pageList.add(p);
    	}
     
     
    ---------------------------------------->
     
    	int pageFaultCount = 0;
    	for(Page p : pageList) {I'
    			if(!p.getActive) {
    				p.setActive();
    			}
     
    			if(memList.size() < memory) {
    				memList.add(p);
    			} else {
    				memList.remove(0);
    				memList.add(0,p);	//add 'p' to the list at the first position in the list (index = 0)
    				pageFaultCount++;
    			}
    		}
    	}
    }
     
    	//After this you can write out the pageFaultCount for statistics
    	//Since this is just one "algorithm" I've made out from the document, there should be 3 algorithms to implement. And write the stats for those 3
     
    	//(FIFO – First In First Out, LRU – Least Recently Used, and the Clock).
    	//The one I made was a FIFO, LRU should be almost exactly the same as FIFO, just instead of adding at index = 0, then add it at the highest index
    	//I don't know what the Clock method is
    Last edited by helloworld922; December 7th, 2009 at 10:32 PM. Reason: Code tags should be [code], not <code>

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Page Replacement Algorithms

    FIFO is best implemented as a Queue (and indirectly, a linked list). For the Least Recently Used, I think you might be able to implement that as a Priority Queue (through a heap), just keep track of when the page was last used.

    Wikipedia: Queue Data Structure
    Wikipedia: Heap Data Structure

    If you can, just use Java's Queue and Priority Queue classes.

Similar Threads

  1. Sorting Algorithms
    By Dalisra in forum Java Programming Tutorials
    Replies: 1
    Last Post: November 10th, 2009, 09:24 PM
  2. Replies: 1
    Last Post: June 21st, 2009, 12:05 PM
  3. Logic to implement a program to display decimal place of a number
    By chronoz13 in forum Algorithms & Recursion
    Replies: 2
    Last Post: June 11th, 2009, 04:53 AM
  4. Java algorithm for bank program to connect database
    By araujo3rd in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 10th, 2008, 01:34 PM
  5. Saving .jsp page as .pdf file while generating report for struts based web application
    By ravindra_kumar_tiwari in forum JavaServer Pages: JSP & JSTL
    Replies: 3
    Last Post: August 12th, 2008, 10:32 AM