-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLRU.java
More file actions
58 lines (52 loc) · 1.39 KB
/
LRU.java
File metadata and controls
58 lines (52 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
*
*/
import java.util.ArrayList;
import java.util.List;
import java.util.HashSet;
import java.util.Iterator;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;
/**
* This is an implementation of one of the paging algorithms
*
*
*/
public class LRU
{
/**
* @param args
*
* lru method that accepts a list and a resident set size as parameters.
* It makes a priority queue of the resident set.
* If a page is in the resident set, it checks if it's equal with the new page, and if it is, it will increment its useCount and set flag to true
* If page is not flagged, increment pages.if resident set is greater than the set size,take out the least recently used, create a new page and put it in the resident set
*/
public static int lru(shortProgram program, int setSize)
{
PriorityQueue<Page> resSet = new PriorityQueue<Page>();
int pageFaults=0;
boolean flag;
for(int i=0; i< program.size(); i++){
flag=false;
for(Page p : resSet){
if(p.equals(program.getIndex(i))) {
p.access();
flag=true;
break;
}
}
if(!flag){
pageFaults++;
if(resSet.size()>=setSize){
//take out the least recently used page and put a new one in
resSet.poll();
}
Page p = new Page(program.getIndex(i));
resSet.add(p);
}
}
return pageFaults;
}
}