What is Priority Based scheduling
A priority based scheduling algorithm is an algorithm in which a priroity is associated with each process, and the CPU is allocated to the process with the highest priority (Equal-priority processes are scheduled in FCFS order).
Advantage of Priority based Scheduling
1. Simplicity.
2. Reasonable support for priority.
3. Suitable for applications with varying time and resource requirements.
Disadvantages of Priority based Scheduling
1. Indefinite blocking or starvation if high priority process keeps on coming.
2. A priority scheduling can leave some low priority waiting processes indefinitely for CPU.
3. If the system eventually crashes then all unfinished low priority processes gets lost.
To counter this most of the PBS based OS increase the priority of process in waiting queue after every certain interval.
C Program to show Priority Based Scheduling
#include<stdio.h>
void main()
{
int bt[20],p[20],wt[20],tat[20],pr[20],i,j,n,total=0,pos,temp;
int average_waiting_time,average_turnaround_time;
printf("Enter Total Number of Process:");
scanf("%d",&n);
printf("nEnter Burst Time and Priorityn");
for(i=0;i<n;i++)
{
printf("nP[%d]n",i+1);
printf("Burst Time:");
scanf("%d",&bt[i]);
printf("Priority:");
scanf("%d",&pr[i]);
p[i]=i+1; //P[i] contains process number
}
//sorting burst time, priority and process number in ascending order using selection sort
for(i=0;i<n;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(pr[j]<pr[pos])
pos=j;
}
temp=pr[i];
pr[i]=pr[pos];
pr[pos]=temp;
temp=bt[i];
bt[i]=bt[pos];
bt[pos]=temp;
temp=p[i];
p[i]=p[pos];
p[pos]=temp;
}
wt[0]=0; //waiting time for first process is zero
//calculate waiting time
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
average_waiting_time=total/n;
total=0;
printf("nProcesst Burst Time tWaiting TimetTurnaround Time");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i]; //calculate turnaround time
total+=tat[i];
printf("nP[%d]tt %dtt %dttt%d",p[i],bt[i],wt[i],tat[i]);
}
average_turnaround_time=total/n;
printf("nnAverage Waiting Time=%d", average_waiting_time);
printf("nAverage Turnaround Time=%dn", average_turnaround_time);
}