Friday, 7 April 2017

Implementation of Shortest Job First (SJF) Scheduling Algorithm in C

Shortest Job First (SJF) is process scheduling algorithm that selects the waiting process with the smallest execution time to execute first. SJN is a non-preemptive algorithm. This algorithm is also known as Shortest Job Next.This algorithm has minimum average waiting time among all the process scheduling algorithm. SJF is the best option in an environment where it is possible to estimate accurate execution time of the jobs.

Algorithm:

step 1:  Sort all the process according to their burst time in increasing order.
step 2:  Now, simply apply FCFS

C Program to implement Shortest Job First (SJF) scheduling algorithm:
#include <stdio.h>
 
int main()
 
{
 
         int bt[20],wt[20],p[20],tat[20];
         float avwt=0,avtat=0;
 
         int i,j,n,temp,key;
 
 
 
         printf("\nEnter the number of the processes: ");
 
         scanf("%d",&n);
 
 
 
         for(i=0;i<n;i++)
 
         {
 
                printf("\nEnter the burst time of the process P[%d]: ",i);
 
                scanf("%d",&bt[i]);
                p[i]=i;
 
         }

         for(i=0;i<n-1;i++)
         {
             for(j=0;j<n-i-1;j++)
             {
                  if(bt[j]>bt[j+1])
                  {
                       temp=bt[j];
                       bt[j]=bt[j+1];
                       bt[j+1]=temp;
                   
                       temp=p[j];
                       p[j]=p[j+1];
                       p[j+1]=temp;
                  }
            }
         }
 
 
         wt[0]=0;
 
         tat[0]=bt[0];
         
         avtat=tat[0];
 
 
         for(i=1;i<n;i++)
 
         {
 
               wt[i]=wt[i-1]+bt[i-1];
 
               tat[i]=tat[i-1]+bt[i];
 
               avwt+=wt[i];
               
               avtat+=tat[i];
 
         }
 
         avwt=avwt/n;
         avtat=avtat/n;
 
        printf("\n\nPROCESS\t\twaiting time\tburst time\tTurnaround time\n");
 
        printf("\n");
 
        for(i=0;i<n;i++)
 
        {
 
               printf("P[%d]\t\t%d\t\t%d\t\t%d\n",p[i],wt[i],bt[i],tat[i]);
 
        }
 
        printf("\n\nAverage waiting time: %.2f",avwt);
        printf("\n\nAverage Turn around time is: %.2f",avtat);
        printf("\n");
 
        return 0;
 
}                                                                                                                                                     

OUTPUT:

 

1 comments so far


EmoticonEmoticon