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!
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 ?
Code :
/* 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
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.