Repository navigation
Expand file tree
/
Copy pathShortestRemainingTime.java
More file actions
196 lines (163 loc) · 7.18 KB
/
Copy pathShortestRemainingTime.java
File metadata and controls
196 lines (163 loc) · 7.18 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
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
185
186
187
188
189
190
191
192
193
194
195
196
import java.util.*;
class Process{
int processID;
int burstTime;
int arrivalTime;
int completionTime;
public Process(int pid, int bt, int art){
processID = pid;
burstTime = bt;
arrivalTime = art;
}
public void setCompletionTime(int cT){
completionTime = cT;
}
}
public class ShortestRemainingTime {
// Method to find the sequence for all processes
static void findSequence(Process proc[], int n) {
int totalTime = 0;
ArrayList<Integer> burstTimeList = new ArrayList<Integer>();
ArrayList<Integer> processIDList = new ArrayList<Integer>();
ArrayList<Integer> startTimeList = new ArrayList<Integer>();
// Copy the burst time into burstTimeList
for (int i = 0; i < n; i++) {
burstTimeList.add(proc[i].burstTime);
totalTime = totalTime + proc[i].burstTime;
}
int complete = 0, t = 0, minRemainTime = Integer.MAX_VALUE;
int shortest = 0;
boolean check = false;
int currentProcessID = 0;
System.out.print(
"\n\n--------------------------------------");
System.out.printf("\n%-15s%-15s%-15s\n", "Customer", "Start Time", "End Time");
System.out.print(
"--------------------------------------\n");
// Process until all processes gets completed
while (complete != n) {
for (int j = 0; j < n; j++) {
if ((proc[j].arrivalTime <= t) && (burstTimeList.get(j) < minRemainTime) && burstTimeList.get(j) > 0) {
minRemainTime = burstTimeList.get(j);
shortest = j;
check = true;
}
}
// System.out.println("proc[shortest].arrivalTime: " +
// proc[shortest].arrivalTime);
// System.out.println("totalTime: " + totalTime);
// System.out.println("proc[shortest].processID: " + proc[shortest].processID);
// System.out.println("t: " + t);
if (proc[shortest].arrivalTime > t) {
totalTime++;
} else {
if (proc[shortest].processID != currentProcessID) {
processIDList.add(proc[shortest].processID);
startTimeList.add(t);
currentProcessID = proc[shortest].processID;
}
}
if (check == false) {
t++;
continue;
}
// Decrease remaining time by one
burstTimeList.set(shortest, burstTimeList.get(shortest) - 1);
// Update minimum
minRemainTime = burstTimeList.get(shortest);
if (minRemainTime == 0)
minRemainTime = Integer.MAX_VALUE;
// If a process completely executed
if (burstTimeList.get(shortest) == 0) {
complete++;
check = false;
}
// Increment time
t++;
}
for (int i = 0; i < processIDList.size(); i++) {
if (i == processIDList.size() - 1) {
System.out.printf("%-15s%-15s%-15s\n", processIDList.get(i), startTimeList.get(i), totalTime);
break;
} else {
System.out.printf("%-15s%-15s%-15s\n", processIDList.get(i), startTimeList.get(i),
startTimeList.get(i + 1));
}
}
}
static void findWaitingTime(Process proc[], int n, int wt[]) {
ArrayList<Integer> burstTimeList = new ArrayList<Integer>();
ArrayList<Integer> completionTimeList = new ArrayList<Integer>();
// Copy the burst time into burstTimeList
for (int i = 0; i < n; i++) {
burstTimeList.add(proc[i].burstTime);
}
int complete = 0, t = 0, minm = Integer.MAX_VALUE;
int shortest = 0, finish_time = 0;
boolean check = false;
// Process until all processes completed
while (complete != n) {
for (int j = 0; j < n; j++) {
if ((proc[j].arrivalTime <= t) && (burstTimeList.get(j) < minm) && burstTimeList.get(j) > 0) {
minm = burstTimeList.get(j);
shortest = j;
check = true;
}
}
if (check == false) {
t++;
continue;
}
// Decrease remaining time by one
burstTimeList.set(shortest, burstTimeList.get(shortest) - 1);
// Update minimum
minm = burstTimeList.get(shortest);
if (minm == 0)
minm = Integer.MAX_VALUE;
// If a process completely executed
if (burstTimeList.get(shortest) == 0) {
complete++;
check = false;
// Find finish time of current process
finish_time = t + 1;
// Calculate waiting time
wt[shortest] = finish_time - proc[shortest].burstTime - proc[shortest].arrivalTime;
completionTimeList.add(finish_time);
proc[shortest].setCompletionTime(finish_time);
if (wt[shortest] < 0)
wt[shortest] = 0;
}
t++;
}
}
// Method to calculate turn around time
static void findTurnAroundTime(Process proc[], int n, int wt[], int tat[]) {
// calculating turnaround time by adding
for (int i = 0; i < n; i++)
tat[i] = proc[i].burstTime + wt[i];
}
// Method to calculate average time
static void findavgTime(Process proc[], int n) {
int wt[] = new int[n], tat[] = new int[n];
int total_wt = 0, total_tat = 0;
// Function to find waiting time of all processes
findWaitingTime(proc, n, wt);
// Function to find turn around time for all processes
findTurnAroundTime(proc, n, wt, tat);
// Display processes along with all details
System.out.print(
"\n\n----------------------------------------------------------------------------------------------------------------");
System.out.printf("\n%-20s%-20s%-20s%-20s%-20s%-20s", "Processes", "Arrival time", "Burst time", "Completion time", "Turnaround time", "Waiting time");
System.out.print(
"\n----------------------------------------------------------------------------------------------------------------");
for (int i = 0; i < n; i++) {
total_wt = total_wt + wt[i];
total_tat = total_tat + tat[i];
System.out.printf("\n%-20s%-20s%-20s%-20s%-20s%-20s", proc[i].processID, proc[i].arrivalTime,proc[i].burstTime, proc[i].completionTime,tat[i], wt[i]);
}
// Calculate average waiting time and total turnaround time
System.out.println("\n\n\nAverage waiting time = " + (float) total_wt / (float) n);
System.out.println("Average turn around time = " + (float) total_tat / (float) n);
findSequence(proc, n);
}
}