This repository was archived by the owner on Jun 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriority_NPE.java
More file actions
68 lines (58 loc) · 2.8 KB
/
Copy pathPriority_NPE.java
File metadata and controls
68 lines (58 loc) · 2.8 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
import java.util.*;
//lower priority value means higher priority
public class Priority_NPE {
public static void main(String[] args) {
System.out.println("Enter the number of processes : ");
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
List<Process> processes = new ArrayList<>(size);
for(int i = 0 ; i < size ; i++){
System.out.println("Enter the arrival time, burst time and priority of process " + (i+1) + " : ");
int arrivalTime = sc.nextInt();
int burstTime = sc.nextInt();
int priority = sc.nextInt();
processes.add(new Process(i+1, arrivalTime, burstTime, priority));
}
sc.close();
float arrivalTime = 0,finishTime= 0;
float turnAroundTime = 0,waitingTime = 0;
float avgTurnAroundTime = 0,avgWaitingTime = 0;
System.out.println("-------------------------------------------------------------------------");
System.out.println("|PID\t|Arrival|Burst\t|Priority|Finish|Turn Around\t|Waiting\t|");
System.out.println("-------------------------------------------------------------------------");
processes.sort(Comparator.comparingInt((Process p) -> p.arrivalTime).thenComparingInt(p -> p.priority));
while(!processes.isEmpty()){
Process current = processes.remove(0);
if(current.arrivalTime > arrivalTime){
processes.add(current);
processes.sort(Comparator.comparingInt((Process p) -> p.arrivalTime));
arrivalTime = processes.get(0).arrivalTime;
}
else {
finishTime = arrivalTime + current.burstTime;
turnAroundTime = finishTime - current.arrivalTime;
waitingTime = turnAroundTime - current.burstTime;
System.out.println("|"+current.pid + "\t|" + current.arrivalTime + "\t|" + current.burstTime + "\t|"+ current.priority + "\t |" + finishTime + "\t|" + turnAroundTime + "\t\t|" + waitingTime + "\t\t|");
avgTurnAroundTime += turnAroundTime;
avgWaitingTime += waitingTime;
arrivalTime = finishTime;
processes.sort(Comparator.comparingInt((Process p) -> p.priority));
}
}
avgTurnAroundTime /= size;
avgWaitingTime /= size;
System.out.println("Average Turn Around Time : " + avgTurnAroundTime + "\nAverage Waiting Time : " + avgWaitingTime);
}
}
class Process{
int pid;
int arrivalTime;
int burstTime;
int priority;
public Process(int pid , int arrivalTime , int burstTime, int priority){
this.pid = pid;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.priority = priority;
}
}