Program to stimulate the CPU scheduling algorithm Shortest job first (Non-Preemption)

Concept: To calculate the average waiting time in the shortest job first algorithm the sorting of the process based on their burst time in ascending order then calculate the waiting time of each process as the sum of the bursting times of all the process previous or before to that process.

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: Start the Ready Q according the shortest Burst time by sorting according to lowest to highest burst time.
Step 5: Set the waiting time of the first process as ‗0 and its turnaround time as its burst time.
Step 6: Sort the processes names based on their Burt time
Step 7: For each process in the ready queue, calculate
        a) Waiting time(n)= waiting time (n-1) + Burst time (n-1)
        b) Turnaround time (n)= waiting time(n)+Burst time(n)
Step 8: Calculate
        c) Average waiting time = Total waiting Time / Number of process
        d) Average Turnaround time = Total Turnaround Time / Number of
process

Step 9: Stop

				
					#include<stdio.h>
void main()
{
int serv[10][2],tempo[1][2];
int i,j,n,totoal;
float average_time,average_wait;
printf("shortest job first sjf scheduling program in c tn");
printf("enter number of processn");
scanf("%d",&n);
for(i=0;i
{
printf("nenter serv time of %d processn",i+1);
serv[i][0]=i;
scanf("%d",&serv[i][1]);
}
serv[0][1]=serv[0][1]-1;
for(i=0;i
{
for(j=0;j=serv[j+1][1])
{
tempo[0][1]=serv[j][1];
tempo[0][0]=serv[j][0];
serv[j][1]=serv[j+1][1];
serv[j][0]=serv[j+1][0];
serv[j+1][1]=tempo[0][1];
serv[j+1][0]=tempo[0][0];
}
}
}
totoal=1;
average_time=0;
for(i=0;i
{
totoal=totoal+serv[i][1];
average_time=average_time+(totoal-serv[i][0]);
}
average_wait=average_time/n;
printf("average waiting time for sjf is: %f",average_wait);
}
				
			
Output: 
enter number of process 4
enter serv time of 1 process 8
enter serv time of 2 process 4
enter serv time of 3 process 9
enter serv time of 4 process 5
average waiting time for sjf is: 13.00

Leave a Reply