-
Notifications
You must be signed in to change notification settings - Fork 0
/
NonPrePrio.java
184 lines (151 loc) · 6.3 KB
/
NonPrePrio.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import java.util.ArrayList;
public class NonPrePrio {
NonPrePrio(){}
private ArrayList<Integer> arrivalQueue = new ArrayList<>();
private int[][] inputArray;
private int[][] outputArray;
private int totalBurstTime;
private int[] finishTimeArray;
private int numberOfProcess;
private int startingTime;
private ArrayList<Integer> time = new ArrayList<>(); //store execution time
private ArrayList<String> ganntChart = new ArrayList<>(); //store process
/*inputArray[i][0] is process id
inputArray[i][1] is burst time
inputArray[i][2] is arrival time
inputArray[i][3] is priority
inputArray[i][4] is waiting time
inputArray[i][5] is turnaround time*/
NonPrePrio(int numberOfProcess,int[][] inputArray){
this.inputArray = inputArray;
this.numberOfProcess = numberOfProcess;
outputArray = new int[numberOfProcess][6];
finishTimeArray = new int[numberOfProcess];
setStartingTime();
totalBurstTime = calculateTotalBurst()+startingTime;
}
public void setStartingTime(){
int min=inputArray[0][2];
for(int i=1;i<3;i++){
if(inputArray[i][2]<min){
min = inputArray[i][2];
}
}
startingTime = min;
}
public int calculateTotalBurst(){
for (int i=0;i<numberOfProcess;i++){
totalBurstTime += inputArray[i][1];
}
return totalBurstTime;
}
public void nonPreemptivePriority(){
int executeTime = 0; //time counter for executing process
boolean executing = false; //if a process is executing
int processExecuting; //processID which is executing
for(int s=startingTime; s<totalBurstTime+1; s++){
//get new arrived processID
for(int p=0; p< numberOfProcess; p++){
if(inputArray[p][2] == s)
arrivalQueue.add(inputArray[p][0]);
}
//if executing, minus time counter for execution
if(executing){
if(executeTime!=0){
executeTime-=1;
if(executeTime == 0)
executing = false;
}
}
//not executing, start new process
else{
//if end of all process, break loop
if(s == totalBurstTime){
time.add(s);
break;
}else{
//get highest prio from arrivalQueueProcess
int highestPrio = getSmallest(arrivalQueue);
ArrayList<Integer> prioProcess = new ArrayList<Integer>();
//get processes with highestPrio
for(int i=0;i<arrivalQueue.size();i++)
for(int j=0; j< numberOfProcess;j++)
if(arrivalQueue.get(i) == inputArray[j][0] && inputArray[j][3] == highestPrio)
prioProcess.add(inputArray[j][0]);
//if there is process to be executed
if(prioProcess.size()>=1){
executing = true;
processExecuting = prioProcess.get(0);
//get row of the process
int row = getRow(processExecuting);
//burstTime - 1
executeTime = inputArray[row][1]-1;
//finishTime of the process
finishTimeArray[row] = s + inputArray[row][1];
//ganttChart
ganntChart.add("P"+processExecuting);
time.add(s);
//remove from waitingQueue
arrivalQueue.remove(Integer.valueOf(processExecuting));
if (executeTime == 0)
executing = false;
}
else{
time.add(s);
ganntChart.add("-");
}
}
}
}
}
public int getRow(int value){
for(int i=0; i<inputArray.length;i++){
if(inputArray[i][0]== value)
return i;
}
return -1;
}
public int getSmallest(ArrayList<Integer> arrivalQueue){ //boolean process if true return process id, else return smallest number of the column
int min = 100;
//int processID = 0;
for(int i=0;i<arrivalQueue.size();i++){
for(int j=0; j< numberOfProcess;j++){
if(arrivalQueue.get(i) == inputArray[j][0] && inputArray[j][3] < min)
min = inputArray[j][3];
}
}
return min;
}
public void updateOutputTable(){
for (int i = 0; i < numberOfProcess; i++) {
for(int j = 0; j < 4; j++){
outputArray[i][j]=inputArray[i][j]; //copy all data from input array to output array
}
outputArray[i][4] = finishTimeArray[i] - outputArray[i][2]; //turnaround time = finish time - arrival time
outputArray[i][5] = finishTimeArray[i] - outputArray[i][2] - outputArray[i][1]; //waiting time = finish time - arrival time - burst time
if (outputArray[i][5] < 0)
outputArray[i][5] = 0; //the lowest waiting time is 0
}
}
public int[][] getOutputArray(){
return outputArray;
}
public ArrayList<String> getGanttChart(){
return ganntChart;
}
public ArrayList<Integer> getTime(){
return time;
}
public void calculateTime(){
int totalTAT = 0;
int totalWT = 0;
for (int i = 0; i < numberOfProcess; i++) {
totalTAT += outputArray[i][4];
totalWT += outputArray[i][5];
}
double avgTAT = (double)totalTAT/numberOfProcess;
double avgWT = (double)totalWT/numberOfProcess;
System.out.println("\nAverage Turnaround Time: " + String.format("%.2f", avgTAT));
System.out.println("Average Waiting Time: " + String.format("%.2f", avgWT));
}
}