Java program to perform priority scheduling Algorithm

Priority scheduling

It is a method of scheduling processes based on priority. In this method, the scheduler chooses the tasks to work as per the priority.Priority scheduling involves priority assignment to every process, and processes with higher priorities are carried out first, whereas tasks with equal priorities are carried out on a First come First served (FCFS) or round robin basis.

This is a Non – Preemptive Algorithm for Priority Scheduling which means unless a single process gets completely executed, the CPU will not leave the current process and execute any other process. Once a process is completed, only then another process is taken for execution.

Drawback

When OS(operating system) gives a process very low priority, which make the process to wait for longer time i.e. CPU is not being provided. If processes with high priority keep on coming then it will never be dealt with CPU, this problem is known as Startvation or Infinite Blocking.

How to solve this problem?

A technique is used called aging in which the process with low priority gains priority over time.

Read Also: Program for FCFS , Program for SJF




Java program for priority scheduling


import java.util.Scanner;

public class TestClass {
       
    public static void main(String args[]) {
            Scanner s = new Scanner(System.in);

            int x,n,p[],pp[],bt[],w[],t[],awt,atat,i;

            p = new int[10];
            pp = new int[10];
            bt = new int[10];
            w = new int[10];
            t = new int[10];

   //n is number of process
   //p is process
   //pp is process priority
   //bt is process burst time
   //w is wait time
   // t is turnaround time
   //awt is average waiting time
   //atat is average turnaround time


   System.out.print("Enter the number of process : ");
   n = s.nextInt();
    System.out.print("\n\t Enter burst time : time priorities \n");

   for(i=0;i<n;i++)
    {
       System.out.print("\nProcess["+(i+1)+"]:");
      bt[i] = s.nextInt();
      pp[i] = s.nextInt();
      p[i]=i+1;
    }

//sorting on the basis of priority
  for(i=0;i<n-1;i++)
   {
     for(int j=i+1;j<n;j++)
     {
       if(pp[i]<pp[j])
       {
	 x=pp[i];
	 pp[i]=pp[j];
	 pp[j]=x;
	 x=bt[i];
	 bt[i]=bt[j];
	 bt[j]=x;
	 x=p[i];
	 p[i]=p[j];
	 p[j]=x;
      }
   }
}
w[0]=0;
awt=0;
t[0]=bt[0];
atat=t[0];
for(i=1;i<n;i++)
 {
   w[i]=t[i-1];
   awt+=w[i];
   t[i]=w[i]+bt[i];
   atat+=t[i];
 }

//Displaying the process

  System.out.print("\n\nProcess \t Burst Time \t Wait Time \t Turn Around Time   Priority \n");
for(i=0;i<n;i++)
  System.out.print("\n   "+p[i]+"\t\t   "+bt[i]+"\t\t     "+w[i]+"\t\t     "+t[i]+"\t\t     "+pp[i]+"\n");
awt/=n;
atat/=n;
  System.out.print("\n Average Wait Time : "+awt);
  System.out.print("\n Average Turn Around Time : "+atat);

        }
}

Output

Enter the number of process : 3

         Enter burst time : time priorities 

Process[1]:10 3

Process[2]:5 1

Process[3]:9 2


Process           Burst Time           Wait Time          Turn Around Time           Priority 

   1                   10                    0                     10                   3

   3                   9                    10                     19                   2

   2                   5                    19                     24                   1

 Average Wait Time : 9
 Average Turn Around Time : 17

Note: Here smaller number means smaller priority !

If you found any error or you have any better program please leave it in comment.

One Reply to “Java program to perform priority scheduling Algorithm”

Leave a Reply

Your email address will not be published. Required fields are marked *