C Program for Shortest Job First (SJF) Scheduling Algorithm

C Program for Shortest Job First (SJF) Scheduling Algorithm is today’s our topic.It’s was, one of the most demanding algorithm in my collage days (Still it is). So, this article is all about writing a C Program for Shortest Job First (SJF) Scheduling Algorithm

The main terms used in Shortest Job First (SJF) Scheduling Algorithm are :

CPU Utilization
Keep the CPU as busy as possible. It range from 0 to 100%. But, technically in practice, it range from 40 to 90%.

Throughput
Throughput is the rate at which processes are completed per unit of time.

Turnaround time
It is calculated as the time gap between the submission of a process and its completion.

Waiting time
The sum of the time periods spent in waiting in the ready queue.

Response time
Response time is the time it takes to start responding from submission time

Also, you can download the program from here : Shortest Job First

[wpdm_package id=’208′]

#include<stdio.h>
void main()
{
char process[10][5],temp[5];
int total_waiting_time=0,waiting_time[10],pt[10],i,j,n,temp1;
float avg=0;
printf("enter no of processes:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter process%d name:n",i+1);
scanf("%s",&process[i]);
printf("enter process time");
scanf("%d",&pt[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(pt[i]>pt[j])
{
temp1=pt[i];
pt[i]=pt[j];
pt[j]=temp1;
strcpy(temp,process[i]);
strcpy(process[i],process[j]);
strcpy(process[j],temp);
}
}
}
waiting_time[0]=0;
for(i=1;i<n;i++)
{
waiting_time[i]=waiting_time[i-1]+pt[i-1];
total_waiting_time=total_waiting_time+waiting_time[i];
}
avg=(float)total_waiting_time/n;
printf("p_namet P_timet w_timen");
for(i=0;i<n;i++)
printf("%st%dt%dn",process[i],pt[i],waiting_time[i]);
printf("total waiting time=%dn avg waiting time=%f",total_waiting_time,avg);
}