Write a Program Using C to Implement the Priority Scheduling

Aim:

To Write a Program Using C to Implement the Priority Scheduling.

Algorithm:

  • STEP 1- START
  • STEP 2- Declare the value of bt, p, wt, tat, pr, I, j, n, total=0, os, average waiting time and average turnaround time.
  • STEP 3- Print the total number of process in the n
  • STEP 4- Sorting burst time, priority and process number in ascending order using selection sort
  • STEP 5- Print the i+1, Burst time and Priority time in the P[i]=i+1
  • STEP 6- if pr[i] less than pr[pos], pos=i and create the temporary file and equal to the bt[i], bt[i] equal to bt[pos], bt[pos]equal to temporary, temporary equal to p[i], so p[i] equal to p[pos] and p[pos] equal their temporary file
  • STEP 7- Waiting time for first process is zero, calculate waiting time, average waiting time
  • STEP 8- Print the value process burst time waiting time and turnaround time.STEP 9- tat[i] equal to bt[i] add the wt[i], tot add or equal to tat[i],and print the value p[i],bt[i],wt[i] andtat[i].
  • STEP 10- In float average turn around time equal to total divided be n value, and print the average waiting time and average turn around time.
  • STEP 11- STOP.

Description:

Suppose the ready list contains the processes as shown in the table.

i         T(Pi)         Priority

0         24                2

1          3                 1

2          3                 3

Gantt Chart:

P1P0P2

0                            3                             27                         30

Turn Around Time:

TRND(P0)=27

TRND(P1)=3

TRND(P2)=30

Average Turn-Around Time:

TRND =(30+3+27)/3=20

Waiting Time:

W(P0)=3

W(P1)=0

W(P2)=27

Average Waiting Time:

W=(3+0+27)/3=10

Program:

#include<stdio.h>
#define max 10
int main()
{
int i,j,n,bt[max],p[max],wt[max],tat[max],pr[max],total=0,pos,temp;
float avg_wt,avg_tat;
printf("Enter Total Number of Process:");
scanf("%d",&n); 
printf("\nEnter Burst Time and Priority For ");
for(i=0;i<n;i++)
{
printf("\nEnter Process %d: ",i+1);
scanf("%d",&bt[i]);
scanf("%d",&pr[i]);
p[i]=i+1;           
}
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;
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
avg_wt=total/n;      
total=0;
printf("\n\nProcess\t\tBT\t\tWT\t\tTAT");
for(i=0;i<n;i++)
{
tat[i]=bt[i]+wt[i];     
total+=tat[i];
printf("\n P%d\t\t%d\t\t%d\t\t%d",p[i],bt[i],wt[i],tat[i]);
    } 
avg_tat=total/n;     
printf("\n\nAverage Waiting Time = %.2f",avg_wt);
printf("\nAvg Turn Around Time = %.2f\n",avg_tat);
return 0;
}

Execution:

Input
4 3 2 5 1 7 5 9 4

Output
Enter Total Number of Process:
Enter Burst Time and Priority For 
Enter Process 1: 
Enter Process 2: 
Enter Process 3: 
Enter Process 4: 

Process		BT		WT		TAT
 P2		5		0		5
 P1		3		5		8
 P4		9		8		17
 P3		7		17		24

Average Waiting Time = 7.00
Avg Turn Around Time = 13.00

Result:

Thus priority scheduling program was executed Successfully.

Leave a Comment