-
Notifications
You must be signed in to change notification settings - Fork 368
/
priority_scheduling.java
96 lines (80 loc) · 3.63 KB
/
priority_scheduling.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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class priority_scheduling {
static class PriorityScheduling {
public String name;
public int completionTime, arrivalTime, burstTime, turnAroundTime, responseTime, waitingTime;
public int priority;
}
static class PriorityComparator implements Comparator<PriorityScheduling> {
private int curr;
public PriorityComparator(int curr) {
this.curr = curr;
}
@Override
public int compare(PriorityScheduling o1, PriorityScheduling o2) {
if (curr >= o2.arrivalTime && curr >= o1.arrivalTime) {
return Integer.compare(o1.priority, o2.priority);
}
return 0;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Number of Processes: ");
int n = scanner.nextInt();
List<PriorityScheduling> process = new ArrayList<>();
System.out.print("Enter Name of Processes: ");
for (int i = 0; i < n; i++) {
PriorityScheduling ps = new PriorityScheduling();
ps.name = scanner.next();
process.add(ps);
}
System.out.print("Enter Arrival Time of Processes: ");
for (int i = 0; i < n; i++) {
process.get(i).arrivalTime = scanner.nextInt();
}
System.out.print("Enter Priority of Processes: ");
for (int i = 0; i < n; i++) {
process.get(i).priority = scanner.nextInt();
}
System.out.print("Enter Burst Time of Processes: ");
for (int i = 0; i < n; i++) {
process.get(i).burstTime = scanner.nextInt();
}
int curr = 0;
double avgTurnAroundTime = 0, avgWaitingTime = 0, avgResponseTime = 0;
for (int i = 0; i < n; i++) {
Collections.sort(process, new PriorityComparator(curr));
process.get(i).responseTime = curr - process.get(i).arrivalTime;
process.get(i).completionTime = curr + process.get(i).burstTime;
curr = process.get(i).completionTime;
process.get(i).turnAroundTime = process.get(i).completionTime - process.get(i).arrivalTime;
process.get(i).waitingTime = process.get(i).turnAroundTime - process.get(i).burstTime;
avgTurnAroundTime += process.get(i).turnAroundTime;
avgWaitingTime += process.get(i).waitingTime;
avgResponseTime += process.get(i).responseTime;
}
System.out.println(" AT\tP\tBT\tCT\tTAT\tWT\tRT");
for (int i = 0; i < n; i++) {
PriorityScheduling ps = process.get(i);
System.out.printf("%s: %d\t%d\t%d\t%d\t%d\t%d\t%d\n", ps.name, ps.arrivalTime, ps.priority, ps.burstTime,
ps.completionTime, ps.turnAroundTime, ps.waitingTime, ps.responseTime);
}
System.out.println();
System.out.printf("Average TurnAroundTime: %.2f\n", avgTurnAroundTime / n);
System.out.printf("Average WaitingTime: %.2f\n", avgWaitingTime / n);
System.out.printf("Average ResponseTime: %.2f\n", avgResponseTime / n);
System.out.println();
System.out.println("Gantt Chart");
System.out.print(process.get(0).arrivalTime + " |" + process.get(0).name + "| ");
for (int i = 0; i < n - 1; i++) {
System.out.print(process.get(i).completionTime + " |" + process.get(i + 1).name + "| ");
}
System.out.print(process.get(n - 1).completionTime);
System.out.println();
}
}