priority scheduling in java

Solutions on MaxInterview for priority scheduling in java by the best coders in the world

showing results for - "priority scheduling in java"
Euen
12 Mar 2019
1import java.util.Arrays;
2import java.util.Scanner;
3
4public class Priority {
5
6    public static void main(String[] args) {
7
8        System.out.println("*** Priority Scheduling ***");
9
10        System.out.print("Enter Number of Process: ");
11        Scanner sc = new Scanner(System.in);
12        int numberOfProcess = sc.nextInt();
13        String process[] = new String[numberOfProcess];
14
15        int p = 1;
16        for (int i = 0; i < numberOfProcess; i++) {
17            process[i] = "P" + p;
18            p++;
19        }
20
21        System.out.println(Arrays.toString(process));
22
23        System.out.print("Enter Burst Time for " + numberOfProcess + " process: ");
24
25        int burstTime[] = new int[numberOfProcess];
26        for (int i = 0; i < numberOfProcess; i++) {
27            burstTime[i] = sc.nextInt();
28        }
29
30        System.out.println(Arrays.toString(burstTime));
31
32        System.out.print("Enter Priority for " + numberOfProcess + " process: ");
33
34        int priority[] = new int[numberOfProcess];
35        for (int i = 0; i < numberOfProcess; i++) {
36            priority[i] = sc.nextInt();
37        }
38
39        System.out.println(Arrays.toString(priority));
40
41// Sorting process & burst time by priority
42int temp;
43String temp2;
44for (int i = 0; i < numberOfProcess - 1; i++) {
45      for (int j = 0; j < numberOfProcess - 1; j++) {
46            if (priority[j] > priority[j + 1]) {
47                   temp = priority[j];
48                    priority[j] = priority[j + 1];
49                    priority[j + 1] = temp;
50
51                    temp = burstTime[j];
52                    burstTime[j] = burstTime[j + 1];
53                    burstTime[j + 1] = temp;
54
55                    temp2 = process[j];
56                    process[j] = process[j + 1];
57                    process[j + 1] = temp2;
58
59                }
60            }
61        }
62
63        int TAT[] = new int[numberOfProcess + 1];
64        int waitingTime[] = new int[numberOfProcess + 1];
65
66// Calculating Waiting Time & Turn Around Time
67        for (int i = 0; i < numberOfProcess; i++) {
68            TAT[i] = burstTime[i] + waitingTime[i];
69            waitingTime[i + 1] = TAT[i];
70        }
71
72        int totalWT = 0;
73        int totalTAT = 0;
74        double avgWT;
75        double avgTAT;
76
77        System.out.println("Process     BT      WT        TAT");
78        for (int i = 0; i < numberOfProcess; i++) {
79
80            System.out.println(process[i] + "          " + burstTime[i] + "       " + waitingTime[i] + "         " + (TAT[i]));
81            totalTAT += (waitingTime[i] + burstTime[i]);
82            totalWT += waitingTime[i];
83
84        }
85
86        avgWT = totalWT / (double) numberOfProcess;
87        avgTAT = totalTAT / (double) numberOfProcess;
88
89        System.out.println("\n Average Wating Time: " + avgWT);
90        System.out.println(" Average Turn Around Time: " + avgTAT);
91
92    }
93
94}
95