-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion2.java
122 lines (104 loc) · 3.6 KB
/
Question2.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import java.util.LinkedList;
import java.util.Queue;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
public class Question2 {
// FIFO Algorithm
public int faultsFIFO (int allPages[], int page_frames) {
int faults = 0;
Queue<Integer> currentFrames = new LinkedList<>();
for (int i = 0; i<allPages.length; i++){
boolean checker = false;
// Checking if the elem is in the array
for (int element : currentFrames) {
if (element == allPages[i]) {
checker = true;
break;
}
}
if(checker != true){
faults ++;
if(page_frames == currentFrames.size()){
int x = currentFrames.remove();
currentFrames.add(allPages[i]);
} else {
currentFrames.add(allPages[i]);
}
}
}
return faults;
}
// LRU Algorithm
public int faultsLRU (int allPages[], int page_frames){
int faults = 0;
HashMap<Integer, Integer> index = new HashMap<>();
HashSet<Integer> p = new HashSet<>(page_frames);
for (int i=0; i < allPages.length; i++) {
if (p.size() < page_frames) {
// Checking if the page is in the set
if (!p.contains(allPages[i])) {
faults++;
p.add(allPages[i]);
}
index.put(allPages[i], i);
}
// When page set is full
else {
// Checking if the page is in the set
if (!p.contains(allPages[i])) {
int max = Integer.MAX_VALUE;
int min = Integer.MIN_VALUE;
Iterator<Integer> itr = p.iterator();
while (itr.hasNext()) {
int tmp = itr.next();
if (index.get(tmp) < max) {
max = index.get(tmp);
min = tmp;
}
}
faults++;
index.remove(min);
p.remove(min);
p.add(allPages[i]);
}
index.put(allPages[i], i);
}
}
return faults;
}
public static void main(String[] args) {
// Generating the number of page frames.
Random rand = new Random();
int page_frames = rand.nextInt(7 - 1) + 1;
System.out.println("----------------------------------------------------");
System.out.println("Number of Frames: " + Integer.toString(page_frames));
// Creation of page numbers.
int[] allPages = new int[13];
for (int i = 0; i < allPages.length; i++) {
allPages[i] = rand.nextInt(10);
}
// Print the array of selected pages.
System.out.println("----------------------------------------------------");
System.out.print("List of pages: ");
for (int element: allPages) {
System.out.print(Integer.toString(element) + " " );
}
System.out.println("\n----------------------------------------------------");
// FIFO
System.out.print("\n----------------------- FIFO -----------------------");
System.out.print("\nNumber of Page Faults: ");
Question2 elem = new Question2();
int faultsFIFO = elem.faultsFIFO(allPages, page_frames);
System.out.println(faultsFIFO);
System.out.println("----------------------------------------------------");
// LRU
System.out.print("\n----------------------- LRU ------------------------");
System.out.print("\nNumber of Page Faults: ");
Question2 elem2 = new Question2();
int faultsLRU = elem2.faultsLRU(allPages, page_frames);
System.out.println(faultsLRU);
System.out.println("----------------------------------------------------");
}
}