Priority Scheduling Algorithm (Easy)

This is code for Priority Scheduling Algorithm (process with different arrival time)

#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);
// adding a duplicate of the burst time
// temporary array for future use
temp[i]=a[i].BT;
}
// initializing burst time
// of a process with maximum 
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 condition on 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 cal
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\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;
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.