Header Ads Widget

C program of the priority scheduling (Preemptive ) algorithm 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:-

 logic:-. 

  1. First we copy the burst time of the process in a new array temp[] because in the further calculation we will be going to decrease the Burst time of the process but we will have to need the real burst time of the process in the calculation of the waiting time .(If you confused then don't worry you will be able understand after going through code)
  2. we initialize the priority of a process with the maximum (you can take any maximum value). and we will use 9th process because we assumed that there will not be more than 10 process but you can use any number.
  3. In this code we are going to use a loop which executed until all the processes are completed. for checking how many processes are completed we use count .Initially it's value is 0 (i.e no processes are completed yet).
  4. In each  cycle we will find the process which have highest priority(lowest priority number like 1 have high priority than 2) and arrived at time t and burst time of the process is not equal to zero.
  5. After doing this we will decrease the burst time of the process by 1 in each cycle of the time.
  6. And if the process will be complete (Burst time =0) then we will increase the value of the count by 1 (i.e one process is completed)
  7. For calculating the waiting time we will use a formula (WT= time- arrival-Burst time)        let's understand by  an example :- Lets say we have any work in the bank for 5 hours . and we go at the 2 pm and we will come at 9 pm from the bank then waiting time in the bank is:-
                       = (time spend in the bank ) - (Total work time)   

                     =  (9-2) - 5   = 2                                                                                                                                       So we have to wait for the 2 hours .

     8.For calculating turn around time we simply use TAT= completion time - arrival           time.


You can easily understand by Following code .

#include<stdio.h>
struct process
{
int WT,AT,BT,TAT,PT;
};

struct process a[10];

int main()
{
int n,temp[10],t,count=0,short_p;
float total_WT=0,total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the number of the process\n");
scanf("%d",&n);
printf("Enter the arrival time , burst time and priority of the process\n");
printf("AT BT PT\n");
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&a[i].AT,&a[i].BT,&a[i].PT);
// copying the burst time in
// a temp array fot futher use
temp[i]=a[i].BT;
}
// we initialize the burst time
// of a process with maximum
a[9].PT=10000;
for(t=0;count!=n;t++)
{
short_p=9;
for(int i=0;i<n;i++)
{
if(a[short_p].PT>a[i].PT && a[i].AT<=t && a[i].BT>0)
{
short_p=i;
}
}
a[short_p].BT=a[short_p].BT-1;
// if any process is completed
if(a[short_p].BT==0)
{
// one process is completed
// so count increases by 1
count++;
a[short_p].WT=t+1-a[short_p].AT-temp[short_p];
a[short_p].TAT=t+1-a[short_p].AT;
// total calculation
total_WT=total_WT+a[short_p].WT;
total_TAT=total_TAT+a[short_p].TAT;
}
}
Avg_WT=total_WT/n;
Avg_TAT=total_TAT/n;
// printing of the answer
printf("ID WT TAT\n");
for(int i=0;i<n;i++)
{
printf("%d %d\t%d\n",i+1,a[i].WT,a[i].TAT);
}
printf("Avg waiting time of the process is %f\n",Avg_WT);
printf("Avg turn around time of the process is %f\n",Avg_TAT);
return 0;
}




Output:-

Enter the number of the process
3
Enter the arrival time , burst time and priority of the process
AT BT PT
0 3 3
1 5 1
2 2 2
ID WT TAT
1 7 10
2 0 5
3 4 6
Avg waiting time of the process is 3.666667
Avg turn around time of the process is 7.000000

Recommended post:-  

codechef problems:-

Wipro :-

Infytq :-

Key Points;-

Hackerrank:-


C-tutorial:-

See more:-





 

Post a Comment

0 Comments