C program on the priority scheduling algorithm (Non preemptive) in operating system (os)

In this post I am going to explain you all things related to the priority scheduling . What is priority scheduling , what is the characteristics of the priority scheduling , what is the drawbacks of the this algorithm . all that points.

So let’s start…….

What is the priority scheduling:-

  As it is clear with the name that this scheduling is based on the priority of the processes. The process which have the higher priority will get the CPU first. whereas jobs with equal priorities are carried out on a round-robin or FCFS basis. 

This scheduling is of two types:-

1. Non preemptive

2. Preemptive

characteristics:-

  1. It schedules the process based on the priority of the processes.
  2. Lower the number higher the priority.
  3. If the two or more processes have the same priority then we schedules on the basis of FCFS.
  4. Major problem with priority scheduling is problem of starvation.
  5. Solution of the problem of the starvation is aging ,where aging is a technique of gradually increasing the priority of the processes that wait in the system from long time.

Drawbacks:-

  1. Major problem with priority scheduling is problem of starvation.
  2. Solution of the problem of the starvation is aging ,where aging is a technique of gradually increasing the priority of the processes that wait in the system from long time.

Code:-

 1.After taking the input first we sort the input on the basis of Arrival time (i.e the process which have less Arrival time will come first). 

2.Here one more things we will do . we will check that the arrival time of all the processes are different or not. (Example:-  if all the processes comes at the same time then we don’t need to sort the array on the basis of arrival time ) . for checking this we will use check_ar.

3.After doing this if processes are arrived at the different time then we can easily calculate the waiting and turn around time for the first process. and for other process first we will find the highest priority (less priority number)  of the process which are arrived at cmp_time  (completion time of the previous process)  .

You can easily understand by Following code .

#include<stdio.h>
struct process
{
int id,WT,AT,BT,TAT,PR;
};
struct process a[10];

// function for swapping
void swap(int *b,int *c)
{
int tem;
tem=*c;
*c=*b;
*b=tem;
}

//Driver function
int main()
{
int n,check_ar=0;
int Cmp_time=0;
float Total_WT=0,Total_TAT=0,Avg_WT,Avg_TAT;
printf(“Enter the number of process n”);
scanf(“%d”,&n);
printf(“Enter the Arrival time , Burst time and priority of the processn”);
printf(“AT BT PRn”);
for(int i=0;i<n;i++)
{
scanf(“%d%d%d”,&a[i].AT,&a[i].BT,&a[i].PR);
a[i].id=i+1;
// here we are checking that arrival time
// of the process are same or different
if(i==0)
check_ar=a[i].AT;
if(check_ar!=a[i].AT )
check_ar=1;
}
// if process are arrived at the different time
// then sort the process on the basis of AT
if(check_ar!=0)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<ni1;j++)
{
if(a[j].AT>a[j+1].AT)
{
swap(&a[j].id,&a[j+1].id);
swap(&a[j].AT,&a[j+1].AT);
swap(&a[j].BT,&a[j+1].BT);
swap(&a[j].PR,&a[j+1].PR);
}
}
}
}
// logic of Priority scheduling ( non preemptive) algo
// if all the process are arrived at different time
if(check_ar!=0)
{
a[0].WT=a[0].AT;
a[0].TAT=a[0].BTa[0].AT;
// cmp_time for completion time
Cmp_time=a[0].TAT;
Total_WT=Total_WT+a[0].WT;
Total_TAT=Total_TAT+a[0].TAT;
for(int i=1;i<n;i++)
{
int min=a[i].PR;
for(int j=i+1;j<n;j++)
{
if(min>a[j].PR && a[j].AT<=Cmp_time)
{
min=a[j].PR;
swap(&a[i].id,&a[j].id);
swap(&a[i].AT,&a[j].AT);
swap(&a[i].BT,&a[j].BT);
swap(&a[i].PR,&a[j].PR);
}
}
a[i].WT=Cmp_timea[i].AT;
Total_WT=Total_WT+a[i].WT;
// completion time of the process
Cmp_time=Cmp_time+a[i].BT;
// Turn Around Time of the process
// compl-Arival
a[i].TAT=Cmp_timea[i].AT;
Total_TAT=Total_TAT+a[i].TAT;
}
}
// if all the process are arrived at same time
else
{
for(int i=0;i<n;i++)
{
int min=a[i].PR;
for(int j=i+1;j<n;j++)
{
if(min>a[j].PR && a[j].AT<=Cmp_time)
{
min=a[j].PR;
swap(&a[i].id,&a[j].id);
swap(&a[i].AT,&a[j].AT);
swap(&a[i].BT,&a[j].BT);
swap(&a[i].PR,&a[j].PR);
}
}
a[i].WT=Cmp_timea[i].AT;
// completion time of the process
Cmp_time=Cmp_time+a[i].BT;
// Turn Around Time of the process
// compl-Arrival
a[i].TAT=Cmp_timea[i].AT;
Total_WT=Total_WT+a[i].WT;
Total_TAT=Total_TAT+a[i].TAT;
}
}
Avg_WT=Total_WT/n;
Avg_TAT=Total_TAT/n;

// Printing of the results
printf(“The process aren”);
printf(“ID WT TATn”);
for(int i=0;i<n;i++)
{
printf(“%dt%dt%dn”,a[i].id,a[i].WT,a[i].TAT);
}
printf(“Avg waiting time is: %fn”,Avg_WT);
printf(“Avg turn around time is: %f”,Avg_TAT);
return 0;
}

Output:-

Enter the number of process
4
Enter the Arrival time , Burst time and priority of the process
AT BT PR
0 5 3
1 2 4
2 2 1
3 6 2
The process are
ID WT TAT
1   0   5
3   3   5
4   4   10
2   12  14
Avg waiting time is: 4.750000
Avg turn around time is: 8.500000

Recommended post:-  

Hackerearth Problems:-

Data structure:-

Key points:-


Leave a Reply

Your email address will not be published.