-
Notifications
You must be signed in to change notification settings - Fork 0
/
pagereplacment.java
76 lines (49 loc) · 1.14 KB
/
pagereplacment.java
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Queue;
class Test
{
// Method to find page faults using FIFO
static int pageFaults(int pages[], int n, int capacity)
{
HashSet<Integer> s = new HashSet<>(capacity);
Queue<Integer> indexes = new LinkedList<>() ;
int page_faults = 0;
for (int i=0; i<n; i++)
{
if (s.size() < capacity)
{
if (!s.contains(pages[i]))
{
s.add(pages[i]);
page_faults++;
indexes.add(pages[i]);
}
}
else
{
if (!s.contains(pages[i]))
{
int val = indexes.peek();
indexes.poll();
// Remove the indexes page
s.remove(val);
// insert the current page
s.add(pages[i]);
indexes.add(pages[i]);
// Increment page faults
page_faults++;
}
}
}
return page_faults;
}
// Driver method
public static void main(String args[])
{
int pages[] = {7, 0, 1, 2, 0, 3, 0, 4,
2, 3, 0, 3, 2};
int capacity = 4;
System.out.println(pageFaults(pages, pages.length, capacity));
}
}