Priority Based Scheduling Algorithm

It is a scheduling algorithm of an operating system.

Priority Based Scheduling Algorithm

  • Priority is assigned for each process.
  • It is an integer number.
  • It can be Preemptive or Non-Preemptive.
  • A process with the highest priority is executed first and so on.
  • Same Priority Can be broken with FCFS Algorithm.
  • No Importance of Arrival Time and Burst Time.
Process Arrival Time Burst Time Priority
P0 0 5 1
P1 1 3 2
P2 2 8 1
P3 3 6 3

Preemptive

  • Only one process is arriving at 0 time P0. so, we have to do the execution of process P0 for 1 unit time.
  • After that First process is getting the executes the process p1 has arrived and the priority of P1 is higher than P0. So, process P1 getting the execution.
  • After that, process P2 arrives. but the priority of process P2 is less than the P1. so P1 again gets an execution.
  • Similarly, All the processes execute likewise.

Gantt Chart:

priority preemptive

Process Arrival Time Burst Time Priority Completion Time Turn around Time Waiting Time
P0 0 5 1 14 14 9
P1 1 3 2 10 9 6
P2 2 8 1 22 10 2
P3 3 6 3 9 6 0

Non-Preemptive

  • Only one process is arriving at 0 time P0.so, we have to do execution of process P0.
  • After the completion of process P0. All the processes have arrived.
  • but the priority of Process is P3. so that, process P3 gets the execution first.
  • Likewise, all the processes get the execution.

Gantt Chart:

priority non preemptive

Process Arrival Time Burst Time Priority Completion Time Turn around Time Waiting Time
P0 0 5 1 5 5 0
P1 1 3 2 14 13 10
P2 2 8 1 12 20 12
P3 3 6 3 11 8 2

Problem:

  • Starvation – low priority processes may never execute.

Solution:

  • Aging – increasing the priority of those process that has been waiting long because of low priority after some specified amount of time.

Implementation of Priority algorithm in C language:

#include<stdio.h>

int main()
{
int p[30],pr[30],key,loc,bt[30],temp,max,wt[30],ta[30],sum=0,i,j,n;
wt[0]=0;
printf("enter the number of processes=");
scanf("%d",&n);
printf("enter the burst time for each process");
for(i=0;i<n;i++)
{
p[i]=i+1;
printf("\np%d=",i+1);
scanf("%d",&bt[i]);
printf("priority of p%d=",i+1);
scanf("%d",&pr[i]);
}
for(i=0;i<n;i++)
{ max=i;
for(j=i+1;j<n;j++)
{
if(pr[j] <pr[max])
max=j;
}
temp=pr[max];
pr[max]=pr[i];
pr[i]=temp;
temp=bt[max];
bt[max]=bt[i];
bt[i]=temp;
temp=p[max];
p[max]=p[i];
p[i]=temp;
}
for(i=0;i<n;i++)
{ wt[i+1]=bt[i]+wt[i];
ta[i]=bt[i]+wt[i];
sum+=ta[i];
}
for(i=0;i<n;i++)
{
printf("\n waiting time for p[%d]=%d",p[i],wt[i]);
printf("\t turn around time for p[%d]=%d",p[i],ta[i]);
}
printf("\n\n average turn around=%d",sum/n);
return 1;
}

Comment for any query.
To learn more concept about Priority go to 
https://www.geeksforgeeks.org/program-priority-scheduling-set-1/

For Android projects goto the ANDROID

Stay Connect with our app: – 

https://play.google.com/store/apps/details?id=com.edu.easyengineer

For Assignment Questions and Explanation of a theory topic visit:

http://mycandal.com/home/elementor-179/

About easyengineering 32 Articles
Easyengineering.in provides you to all subject and exam related materials online like GPSC, UPSC, IES, GATE, etc. As well as we provide daily job notification, some life-related books and other online courses which are useful too in your study.

2 Comments

  1. I’m really enjoying the design and layout of your website.
    It’s a very easy on the eyes which makes it much more pleasant for me to come here and visit more often. Did you hire
    out a developer to create your theme? Outstanding work!

Leave a Reply

Your email address will not be published.


*