This repository was archived by the owner on Aug 9, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathpriority.java
More file actions
77 lines (62 loc) · 2.8 KB
/
Copy pathpriority.java
File metadata and controls
77 lines (62 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
69
70
71
72
73
74
75
76
77
import java.util.Arrays;
import java.util.Scanner;
class Process {
int id;
int arrivalTime;
int burstTime;
int priority;
Process(int id, int arrivalTime, int burstTime, int priority) {
this.id = id;
this.arrivalTime = arrivalTime;
this.burstTime = burstTime;
this.priority = priority;
}
}
public class PriorityScheduler {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Get the number of processes from the user
System.out.print("Enter the number of processes: ");
int n = scanner.nextInt();
Process[] processes = new Process[n];
// Get details for each process from the user
for (int i = 0; i < n; i++) {
System.out.println("Enter details for Process " + (i + 1) + ":");
System.out.print(" Arrival Time: ");
int arrivalTime = scanner.nextInt();
System.out.print(" Burst Time: ");
int burstTime = scanner.nextInt();
System.out.print(" Priority: ");
int priority = scanner.nextInt();
processes[i] = new Process(i + 1, arrivalTime, burstTime, priority);
}
scanner.close();
// Call the Priority scheduling algorithm
priorityScheduling(processes);
}
// Implementation of Priority Scheduling algorithm
public static void priorityScheduling(Process[] processes) {
int n = processes.length;
Arrays.sort(processes, (a, b) -> Integer.compare(a.priority, b.priority));
int[] waitingTime = new int[n];
int[] turnaroundTime = new int[n];
int[] completionTime = new int[n];
int currentTime = 0;
for (int i = 0; i < n; i++) {
Process currentProcess = processes[i];
completionTime[currentProcess.id - 1] = currentTime + currentProcess.burstTime;
turnaroundTime[currentProcess.id - 1] = completionTime[currentProcess.id - 1] - currentProcess.arrivalTime;
waitingTime[currentProcess.id - 1] = turnaroundTime[currentProcess.id - 1] - currentProcess.burstTime;
currentTime = completionTime[currentProcess.id - 1];
}
// Print results
System.out.println("Process\t Arrival Time\t Burst Time\t Priority\t Completion Time\t Waiting Time\t Turnaround Time");
for (int i = 0; i < n; i++) {
Process currentProcess = processes[i];
System.out.println(currentProcess.id + "\t\t" + currentProcess.arrivalTime + "\t\t\t" +
currentProcess.burstTime + "\t\t\t" + currentProcess.priority + "\t\t\t" +
completionTime[currentProcess.id - 1] + "\t\t\t" + waitingTime[currentProcess.id - 1] +
"\t\t\t" + turnaroundTime[currentProcess.id - 1]);
}
}
}