C/C++ Program for Shortest Job First Scheduling Algorithm

About Shortest Job First Scheduling

This is a non-preemptive scheduling algorithm and best approach to minimize waiting time.

Program to implement CPU scheduling for shortest job first.

You might also like: FCFS program




#include<iostream>
using namespace std; 
void main()
{
    int burst_time[20],process[20],waiting_time[20],tat[20],i,j,n,total=0,pos,temp;
    float wait_avg,TAT_avg;
  cout<<"Enter number of process: ";
    cin>>n;
 
   cout<<"\nEnter Burst time: \n";
    for(i=0;i<n;i++)
    {
       cout<<"Process["<<i+1<<"]: ";
       cin>>burst_time[i];
        process[i]=i+1;           //Process Number
    }
 
//Sorting
       for(i=0;i<n;i++)
    {
        pos=i;
        for(j=i+1;j<n;j++)
        {
            if(burst_time[j]<burst_time[pos])
                pos=j;
        }
 
        temp=burst_time[i];
        burst_time[i]=burst_time[pos];
        burst_time[pos]=temp;
 
        temp=process[i];
        process[i]=process[pos];
        process[pos]=temp;
    }
  //First process has 0 waiting time 
    waiting_time[0]=0;           
    //calculate waiting time
    for(i=1;i<n;i++)
    {
        waiting_time[i]=0;
        for(j=0;j<i;j++)
            waiting_time[i]+=burst_time[j];
 
        total+=waiting_time[i];
    }
     //Calculating Average waiting time 
    wait_avg=(float)total/n;
      total=0;
 
   cout<<"\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time";
    for(i=0;i<n;i++)
    {
        tat[i]=burst_time[i]+waiting_time[i]; //Calculating Turnaround Time     
        total+=tat[i];
        cout<<"\n"<<process[i]<<"\t\t"<<burst_time[i]<<"\t\t"<<waiting_time[i]<<"\t\t"<<tat[i];
    }
 
 //Calculation of Average Turnaround Time
    TAT_avg=(float)total/n;    
   cout<<"\n\nAverage Waiting Time: "<<wait_avg;
    cout<<"\nAverage Turnaround Time: "<<TAT_avg;
}
output
Output for SJF program

3 Replies to “C/C++ Program for Shortest Job First Scheduling Algorithm”

  1. Excellent for your program. Would appreciate if you could provide also the same program with different Arrival Time (as of now you are assuming that all Arrival Time is 0)

Leave a Reply

Your email address will not be published. Required fields are marked *