-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPre_Priority_Scheduling.java
More file actions
97 lines (70 loc) · 2.37 KB
/
Pre_Priority_Scheduling.java
File metadata and controls
97 lines (70 loc) · 2.37 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
package java_algo;
import java.util.Arrays;
import java.util.Scanner;
public class Pre_Priority_Scheduling {
public static void main(String[] args) {
System.out.println("*** Priority Scheduling ***");
System.out.print("Enter Number of Process: ");
Scanner sc = new Scanner(System.in);
int numberOfProcess = sc.nextInt();
String process[] = new String[numberOfProcess];
int p = 1;
for (int i = 0; i < numberOfProcess; i++) {
process[i] = "P" + p;
p++;
}
System.out.println(Arrays.toString(process));
System.out.print("Enter Burst Time for " + numberOfProcess + " process: ");
int burstTime[] = new int[numberOfProcess];
for (int i = 0; i < numberOfProcess; i++) {
burstTime[i] = sc.nextInt();
}
System.out.println(Arrays.toString(burstTime));
System.out.print("Enter Priority for " + numberOfProcess + " process: ");
int priority[] = new int[numberOfProcess];
for (int i = 0; i < numberOfProcess; i++) {
priority[i] = sc.nextInt();
}
System.out.println(Arrays.toString(priority));
// Sorting process & burst time by priority
int temp;
String temp2;
for (int i = 0; i < numberOfProcess - 1; i++) {
for (int j = 0; j < numberOfProcess - 1; j++) {
if (priority[j] > priority[j + 1]) {
temp = priority[j];
priority[j] = priority[j + 1];
priority[j + 1] = temp;
temp = burstTime[j];
burstTime[j] = burstTime[j + 1];
burstTime[j + 1] = temp;
temp2 = process[j];
process[j] = process[j + 1];
process[j + 1] = temp2;
}
}
}
int TAT[] = new int[numberOfProcess + 1];
int waitingTime[] = new int[numberOfProcess + 1];
// Calculating Waiting Time & Turn Around Time
for (int i = 0; i < numberOfProcess; i++) {
TAT[i] = burstTime[i] + waitingTime[i];
waitingTime[i + 1] = TAT[i];
}
int totalWT = 0;
int totalTAT = 0;
double avgWT;
double avgTAT;
System.out.println("Process BT WT TAT");
for (int i = 0; i < numberOfProcess; i++) {
System.out.println(
process[i] + " " + burstTime[i] + " " + waitingTime[i] + " " + (TAT[i]));
totalTAT += (waitingTime[i] + burstTime[i]);
totalWT += waitingTime[i];
}
avgWT = totalWT / (double) numberOfProcess;
avgTAT = totalTAT / (double) numberOfProcess;
System.out.println("\n Average Wating Time: " + avgWT);
System.out.println(" Average Turn Around Time: " + avgTAT);
}
}