Tuesday, 29 May 2018

Priority Scheduling Algorithm in Java


//Priority Class File

package com.os.project;

import java.util.Scanner;

public class Priority {
 
 @SuppressWarnings("resource")
 public static void collectData()
 {
    int totalProcess, burstTime[], priority[];
    Scanner input, pburstTime, pPriority;
    System.out.println("Please Enter total Process : ");
    input = new Scanner(System.in);
    totalProcess = input.nextInt();
    burstTime = new int[totalProcess]; priority = new int[totalProcess];
    for(int i = 0; i < totalProcess; i++)
    {
        System.out.println("Please Enter burst time of process #"+(i+1));
        pburstTime = new Scanner(System.in);
        burstTime[i] = pburstTime.nextInt();
        System.out.println("Please Enter priority of process #"+(i+1));
        pPriority = new Scanner(System.in);
        priority[i] = pPriority.nextInt();
    }
    Priority.calculateResult(totalProcess, burstTime, priority);
 }
 
 
 public static void calculateResult(int totalProcess, int[] burstTime, int[] priority)
 {
    int[] turnAroundTime, waitingTime;
    turnAroundTime = new int[totalProcess];
    waitingTime    = new int[totalProcess];
    int averageWT = 0, averageTAT = 0;
    insertionSort(totalProcess, burstTime, priority);
    waitingTime[0] = 0;
    for(int m = 1; m < totalProcess; m++)
    {
       waitingTime[m] = burstTime[m - 1] + waitingTime[m - 1];
    }
  
    for(int i = 0; i < totalProcess; i++)
    {
        turnAroundTime[i] = waitingTime[i] + burstTime[i];
    }
  
    System.out.println("No of Process\t|\t" + "Burst Time\t\t|\t" + "Priority\t\t|\t" + "Waiting Time\t\t|\t\t" + "Turn Around Time");
    for(int i = 0; i < totalProcess; i++)
    {
        averageWT  += waitingTime[i];
        averageTAT += turnAroundTime[i];
        System.out.println((i+1)+ "\t\t|\t\t" + burstTime[i] + "\t\t|\t\t" + priority[i] + "\t\t|\t\t" + waitingTime[i] + "\t\t|\t\t" + turnAroundTime[i]);
    }
  
    System.out.println("The Average Waiting Time of All Process : "+(averageWT/totalProcess));
    System.out.println("The Average Turn Around Time of All Process : "+(averageTAT/totalProcess));
 }
 
 public static void insertionSort(int totalProcess, int[] burstTime, int[] priority)
 {
    int j, i, pTemp, pburst;
    for(i = 1; i < totalProcess; i++)
    { 
       pTemp  = priority[i];
       pburst = burstTime[i];
       j = i - 1;
       while(j >= 0 && priority[j] > pTemp)
       {
           priority[j+1]  = priority[j];
           burstTime[j+1] = burstTime[j];
           j--;
       } 
       priority[j+1]  = pTemp;
       burstTime[j+1] = pburst;
    }
 }
}

//Main Class File

package com.os.project;

import java.util.Scanner;

public class Main 
{ 
    public static void main(String args[])
    {
        Priority.collectData(); 
    }
}

No comments:

Post a Comment