forked from abhikriti/hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFairScheduling.java
More file actions
113 lines (90 loc) · 2.87 KB
/
Copy pathFairScheduling.java
File metadata and controls
113 lines (90 loc) · 2.87 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
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
import java.util.*;
public class FairScheduling {
class Process {
int bTime, pid, rTime, priority, VRT = 0;
}
private int nProcesses;
private ArrayList<Process> procList;
private int quantum, tSlice;
private Scanner input = new Scanner(System.in);
public void takeInput() {
System.out.println("Provide quantum size and number of processes:");
quantum = input.nextInt();
nProcesses = input.nextInt();
procList = new ArrayList<Process>();
System.out.println("Provide burst, priority of all processes:");
for (int i = 0; i < nProcesses; i++) {
Process newProcess = new Process();
newProcess.bTime = input.nextInt();
newProcess.priority = input.nextInt();
newProcess.rTime = newProcess.bTime;
newProcess.pid = i;
procList.add(newProcess);
}
}
public void removeUsed() {
boolean[] finished = new boolean[procList.size()];
for(int i = 0; i < procList.size(); i++) {
finished[i] = false;
}
for (int i = 0; i < procList.size(); i++) {
if (procList.get(i).rTime <= 0){
finished[i] = true;
}
}
for (int i = 0; i < procList.size(); i++) {
if (finished[i] == true){
procList.remove(i);
}
}
}
public void priortize() {
tSlice = quantum / procList.size();
for (int i = 0; i < procList.size(); i++) {
procList.get(i).VRT += tSlice * procList.get(i).priority;
}
/*
procList.sort(new Comparator<Process>() {
public int compare(Process p1, Process p2) {
return p1.VRT < p2.VRT ? 1:0;
}
});
*/
Collections.sort(procList, new Comparator<Process>() {
@Override
public int compare(Process p1, Process p2) {
return p1.VRT- p2.VRT;
}
});
}
public void printList() {
System.out.print("Present List:");
for (int i = 0; i < procList.size(); i++) {
System.out.print(procList.get(i).pid + 1 + " ");
}
System.out.println();
}
public void utilizeCPU() {
for (int i = 0; i < procList.size(); i++) {
System.out.println(procList.get(i).pid + 1);
procList.get(i).rTime -= tSlice;
}
}
public void run() {
priortize();
printList();
utilizeCPU();
removeUsed();
while(procList.size() > 0) {
priortize();
printList();
utilizeCPU();
removeUsed();
}
}
public static void main(String[] args) {
FairScheduling fairScheduling = new FairScheduling();
fairScheduling.takeInput();
fairScheduling.run();
}
}