-
Notifications
You must be signed in to change notification settings - Fork 1
/
PriorityScheduling.java
71 lines (57 loc) · 2.59 KB
/
PriorityScheduling.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
package priorityscheduling;
import java.util.Scanner;
public class PriorityScheduling {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arrivalTime, burstTime, priority, waitingTime, turnaroundTime, completionTime;
int[] x;
int smallest, count = 0, time, n;
double avgWaitingTime = 0, avgTurnaroundTime = 0, end;
System.out.print("Enter the number of processes: ");
n = sc.nextInt();
arrivalTime = new int[n];
burstTime = new int[n];
priority = new int[n];
waitingTime = new int[n];
turnaroundTime = new int[n];
completionTime = new int[n];
x = new int[n];
for (int i = 0; i < n; i++) {
System.out.print("Enter arrival time of process " + (i + 1) + ": ");
arrivalTime[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
System.out.print("Enter burst time of process " + (i + 1) + ": ");
burstTime[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
System.out.print("Enter priority of process " + (i + 1) + ": ");
priority[i] = sc.nextInt();
}
System.arraycopy(burstTime, 0, x, 0, n);
priority[n - 1] = -1;
for (time = 0; count != n; time++) {
smallest = n - 1;
for (int i = 0; i < n; i++) {
if (arrivalTime[i] <= time && priority[i] > priority[smallest] && burstTime[i] > 0)
smallest = i;
}
time += burstTime[smallest] - 1;
burstTime[smallest] = -1;
count++;
end = time + 1;
completionTime[smallest] = (int) end;
waitingTime[smallest] = (int) (end - arrivalTime[smallest] - x[smallest]);
turnaroundTime[smallest] = (int) (end - arrivalTime[smallest]);
}
System.out.println("Process\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time\tCompletion Time\tPriority");
for (int i = 0; i < n; i++) {
System.out.println("P" + (i + 1) + "\t\t" + x[i] + "\t\t" + arrivalTime[i] + "\t\t" + waitingTime[i] +
"\t\t" + turnaroundTime[i] + "\t\t" + completionTime[i] + "\t\t" + priority[i]);
avgWaitingTime += waitingTime[i];
avgTurnaroundTime += turnaroundTime[i];
}
System.out.println("\nAverage waiting time = " + (avgWaitingTime / n));
System.out.println("Average turnaround time = " + (avgTurnaroundTime / n));
}
}