C program to simulate the cpu scheduling priority algorithm.

Concept: To calculate the average waiting time in the priority algorithm, sort the burst times according to their priorities and then calculate the average waiting time of the processes. The waiting time of each process is obtained by summing up the burst times of all the previous processes.

ALGORITHM:
Step 1: Start 
Step 2: Accept the number of processes in the ready Queue
Step 3: For each process in the ready Q, assign the process id and accept the CPU burst time
Step 4: Sort the ready queue according to the priority number.
Step 5: Set the waiting of the first process as ‗0‘ and its burst time as its turnaround time
Step 6: Arrange the processes based on process priority
Step 7: For each process in the Ready Q calculate
Step 8: for each process in the Ready Q calculate
         a) Waiting time(n)= waiting time (n-1) + Burst time (n-1)
         b) Turnaround time (n)= waiting time(n)+Burst time(n)
Step 9: Calculate
         c) Average waiting time = Total waiting Time / Number of process
        d) Average Turnaround time = Total Turnaround Time / Number of process
Print the results in an order.
Step 10: Stop 

				
					#include<stdio.h> 
#include<conio.h>
void main()
{
char p[10][5],temp[5];
int i,j,pt[10],wt[10],totwt=0,pr[10],temp1,n;
float avgwt;
clrscr();
printf("enter no of processes:");
scanf("%d",&n);
for(i=0;i
{
printf("enter process%d name:",i+1);
scanf("%s",&p[i]);
printf("enter process time:");
scanf("%d",&pt[i]);
printf("enter priority:");
scanf("%d",&pr[i]);
}
for(i=0;i
{
for(j=i+1;j
{
if(pr[i]>pr[j])
{
temp1=pr[i];
pr[i]=pr[j];
pr[j]=temp1;
temp1=pt[i];
pt[i]=pt[j];
pt[j]=temp1;
strcpy(temp,p[i]);
strcpy(p[i],p[j]);
strcpy(p[j],temp);
}
}
}
wt[0]=0;
for(i=1;i
{
wt[i]=wt[i-1]+et[i-1];
totwt=totwt+wt[i];

}
avgwt=(float)totwt/n;
printf("p_name\t p_time\t priority\t w_time\n");
for(i=0;i
{
printf(" %s\t %d\t %d\t %d\n" ,p[i],pt[i],pr[i],wt[i]);
}
printf("total waiting time=%d\n avg waiting time=%f",tot,avg);
getch();
}
				
			
OUTPUT:
enter no of processes: 5
enter process1 name: aaa
enter process time: 4
enter priority:5
enter process2 name: bbb
enter process time: 3
enter priority:4
enter process3 name: ccc
enter process time: 2
enter priority:3
enter process4 name: ddd
enter process time: 5
enter priority:2
enter process5 name: eee
enter process time: 1
enter priority:1
p_name P_time priority w_time
eee     1       1      0
ddd     5       2      1
ccc     2       3      6
bbb     3       4      8
aaa     4       5      11
total waiting time=26
avg waiting time=5.20

Leave a Reply