-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriorityNP.java
89 lines (78 loc) · 2.43 KB
/
PriorityNP.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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class PriorityNP implements CPUAlgos {
List<Process> list;
List<String> gantt;
boolean[] visited;
private double avgTat = 0;
private double avgWt = 0;
public PriorityNP(List<Process> list) {
this.list = list;
visited = new boolean[list.size()];
gantt = new ArrayList<>();
System.out.println("Priority Non Pre-emtive Algorithm.");
}
@Override
public void schedule() {
// Sorting according to the Arival Time.
sort(list);
for (Process p : list) {
System.out.println(p);
}
// SJF Logic
Process temp = list.get(0);
Process mp;
int exit = temp.bt + temp.at;
int max;
temp.tat = exit;
avgTat += temp.tat;
gantt.add(temp.at + " [ " + temp.name + " ] " + exit);
for (int i = 1; i < list.size(); i++) {
if (visited[i])
continue;
mp = list.get(i);
max = i;
if (exit < mp.at) {
exit = mp.at;
gantt.add(" [ IDEAL ] " + exit);
}
for (int j = 1; j < list.size(); j++) {
temp = list.get(j);
if (!visited[j] && exit >= temp.at
&& (mp.priority < temp.priority || (mp.priority == temp.priority && mp.at > temp.at))) {
mp = temp;
max = j;
}
}
visited[max] = true;
exit += mp.bt;
mp.tat = exit - mp.at;
avgTat += mp.tat;
mp.wt = mp.tat - mp.bt;
avgWt += mp.wt;
gantt.add(" [ " + mp.name + " ] " + exit);
if (max != i)
i--;
}
String str = "";
for (String s : gantt)
str += s;
Process.AvgTat = avgTat / list.size();
Process.AvgWt = avgWt / list.size();
Process.Gantt = str;
}
private void sort(List<Process> list) {
Comparator<Process> com = new Comparator<>() {
public int compare(Process a, Process b) {
int diff = a.at - b.at;
if (diff == 0)
return b.priority - a.priority;
else
return diff;
}
};
Collections.sort(list, com);
}
}