Priority Scheduling Program in C With Arrival Time
One of the most used scheduling techniques in batch systems is priority scheduling. Priority is given to every process. The process that has the highest importance should be carried out initially, and so on. Priority processes are carried out in order of arrival to the processing queue. Any resource demand, including memory and time, can be used to determine priority. The ratio of average I/O to average CPU burst time can also be used to determine priority.
How is the order of priorities determined?
Numerous factors, including a process's burst time and minimal memory requirements, must be considered when determining its priority.
When it comes to process scheduling, there are a few terms you should know:
- Arrival Time: The amount of time that passes between a process's arrival in the ready queue
- Waiting Time: The amount of time a procedure must wait to be executed in the queue
- Burst Time: The amount of time needed for the procedure to finish.
- Turnaround Time: The total of the burst and queue waiting times.
If we consider Arrival time also, then the first arrival time (less arrival time process first) is allocated to each process. When two processes arrive at the same time, compare them based on priority, with the highest process coming first. Additionally, compare the process numbers of the two processes if their priorities are the same (less process number first). While every step is carried out, this one is repeated.
Steps to implement priority scheduling with arrival time:
1. Enter the processes first, along with their priority, arrival time, and burst time.
2. The process with the lowest arrival time will be scheduled first; if two or more processes have the lowest arrival times, the process with the highest priority will be scheduled first.
3. From this point on, the arrival time and process priority will determine the scheduling of subsequent procedures. (In this case, it is assumed that a lower priority number corresponds to a higher priority). Sort by process number if the priorities of the two processes are the same.
Note: The question will clearly indicate which number will be given a higher priority and which one will be a lesser priority.
4. After all of the processes arrive, we can arrange them according to priority.
Example:
![Priority Scheduling Program in C With Arrival Time](https://static.tutorialandexample.com/c/priority-scheduling-program-in-c-with-arrival-time1.png)
Gantt chart for the above example:
![Priority Scheduling Program in C With Arrival Time](https://static.tutorialandexample.com/c/priority-scheduling-program-in-c-with-arrival-time2.png)
Here are some code steps to perform different priority scheduling with different arrival time
1. First, we must copy the process's burst time into a new array named temp[] since we will decrease it in subsequent computations. We need it to determine the waiting time.
2. We begin a procedure with the maximum priority setting (you are free to choose any maximum number). Moreover, we'll choose the ninth process – you can use any number-because we think there won't be more than ten.
3. This code will make use of a loop that will continue to execute until each procedure is completed. To find out how many processes have been completed, we use count. It starts with a value of 0. (That is, progress has yet to be finished).
4. We will determine which activity has the highest priority at time t, has a burst time that is not equal to zero, and arrives at time t in each cycle (lowest priority numbers, like 1 having a greater priority than 2).
5. Following this, we will cut the burst time of the procedure by one for each cycle.
6. To calculate the waiting time, we shall apply the formula WT= time-arrival-burst time. If the process is complete (burst time = 0), the count will be increased by 1 (i.e., one process will be completed).
Program:
#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,i;
float total_WT=0,total_TAT=0,Avg_WT,Avg_TAT;
printf("Enter the process\n");
scanf("%d",&n);
printf("Input the arrival time , burst time and priority of the process\n");
for(i=0;i<n;i++)
{
scanf("%d%d%d",&a[i].AT,&a[i].BT,&a[i].PT);
temp[i]=a[i].BT;
}
a[9].PT=10000;
for(t=0;count!=n;t++)
{
short_p=9;
for(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(a[short_p].BT==0)
{
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_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;
printf("ID\tWT\tTAT\n");
for(i=0;i<n;i++)
{
printf("%d\t%d\t%d\n",i+1,a[i].WT,a[i].TAT);
}
printf("Avg waiting time is %f\n",Avg_WT);
printf("Avg turn around time is %f\n",Avg_TAT);
return 0;
}
Output:
![Priority Scheduling Program in C With Arrival Time](https://static.tutorialandexample.com/c/priority-scheduling-program-in-c-with-arrival-time3.png)