SJF Scheduling Program in C

Here you will get the shortest job first scheduling program in c with an example.

In the shortest job first scheduling algorithm, the processor selects the waiting process with the smallest execution time to execute next.

Example:

Let’s take one example to understand how SFJ works.

ProcessBurst Time
P14
P22
P33
P41

Now according to the SJF algorithm, the execution sequence of the above processes will be as follow.

ProcessBurst TimeWaiting TimeTurnaround Time
P4101
P2213
P3336
P14610

 Below I have shared the C program for the implementation of the SJF algorithm.

#include<stdio.h>

void main()
{
    int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
    float avg_wt,avg_tat;
    printf("Enter number of process:");
    scanf("%d",&n);

    printf("\nEnter Burst Time:\n");
    for(i=0;i<n;i++)
    {
        printf("p%d:",i+1);
        scanf("%d",&bt[i]);
        p[i]=i+1;           //contains process number
    }

    //sorting burst time in ascending order using selection sort
    for(i=0;i<n;i++)
    {
        pos=i;
        for(j=i+1;j<n;j++)
        {
            if(bt[j]<bt[pos])
                pos=j;
        }

        temp=bt[i];
        bt[i]=bt[pos];
        bt[pos]=temp;

        temp=p[i];
        p[i]=p[pos];
        p[pos]=temp;
    }

    wt[0]=0;            //waiting time for first process will be zero

    //calculate waiting time
    for(i=1;i<n;i++)
    {
        wt[i]=0;
        for(j=0;j<i;j++)
            wt[i]+=bt[j];

        total+=wt[i];
    }

    avg_wt=(float)total/n;      //average waiting time
    total=0;

    printf("\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time");
    for(i=0;i<n;i++)
    {
        tat[i]=bt[i]+wt[i];     //calculate turnaround time
        total+=tat[i];
        printf("\np%d\t\t  %d\t\t    %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
    }

    avg_tat=(float)total/n;     //average turnaround time
    printf("\n\nAverage Waiting Time=%f",avg_wt);
    printf("\nAverage Turnaround Time=%f\n",avg_tat);
}

Output:

C Program for Shortest Job First (SJF) Scheduling Algorithm

Comment down below for any queries of suggestions related to the SJF algorithm in C.

50 thoughts on “SJF Scheduling Program in C”

  1. Hello,
    I need your help the coding of Identical Parallel Machine in Minimizing Makespan P||Cmax of Longest Processing Time (LPT), Shortest Processing Time (SPT) and First Come First Served (FCFS).

  2. This method does not mention the arrival time of the processes , which is the need of the hour! Please update the concept , otherwise it will be a loss for the whole coding fraternity!

    1. #include
      #include
      typedef struct
      {
      int pid;
      float at, wt, bt, ta, st;
      bool isComplete;
      }process;
      void procdetail(int i, process p[])
      {
      printf(“Process id: “);
      scanf(“%d”, &p[i].pid);
      printf(“Arrival Time: “);
      scanf(“%f”, &p[i].at);
      printf(“Burst Time: “);
      scanf(“%f”, &p[i].bt);
      p[i].isComplete = false;
      }//procdetail
      void sort(process p[], int i, int start)
      {
      int k = 0, j;
      process temp;
      for (k = start; k<i; k++)
      {
      for (j = k+1; j<i; j++)
      {
      if(p[k].bt < p[j].bt)
      continue;
      else
      {
      temp = p[k];
      p[k] = p[j];
      p[j] = temp;
      }
      }
      }
      }//sort
      void main()
      {
      int n, i, k = 0, j = 0;
      float avgwt = 0.0, avgta = 0.0, tst = 0.0;
      printf("Enter number of processes: ");
      scanf("%d",&n);
      process p[n];
      for (i = 0; i<n; i++)
      {
      printf("\nEnter process %d's details: ",i);
      procdetail(i,p);
      }
      for (i = 0; i<n; i++)
      {
      if (p[i].isComplete == true)
      continue;
      else
      {
      k = i;
      while (p[i].at<=tst && i<n)
      i++;
      sort (p,i,k);
      i = k;
      if(p[i].at<=tst)
      p[i].st = tst;
      else
      p[i].st = p[i].at;
      p[i].st = tst;
      p[i].isComplete = true;
      tst += p[i].bt;
      p[i].wt = p[i].st – p[i].at;
      p[i].ta = p[i].bt + p[i].wt;
      avgwt += p[i].wt;
      avgta += p[i].ta;
      }
      }
      avgwt /= n;
      avgta /= n;
      printf("Process Schedule Table: \n");
      printf("\tProcess ID\tArrival Time\tBurst Time\tWait Time\tTurnaround Time\n");
      for (i = 0; i<n; i++)
      printf("\t%d\t\t%f\t%f\t%f\t%f\n", p[i].pid,p[i].at, p[i].bt, p[i].wt, p[i].ta);
      printf("\nAverage wait time: %f", avgwt);
      printf("\nAverage turnaround time: %f\n", avgta);
      }//main

  3. Consider a scheduling approach which is non pre-emptive similar to shortest job next in nature. The priority of each job is dependent on its estimated run time, and also the amount of time it has spent waiting. Jobs gain higher priority the longer they wait, which prevents indefinite postponement. The jobs that have spent a long time waiting compete against those estimated to have short run times. The priority can be computed as :
    ???????? = 1 + ??????? ???? / ????????? ??? ????
    Using the data given below compute the waiting time and turnaround time for each process and average waiting time and average turnaround time.
    Process Arrival time Burst time
    P1 0 20
    P2 5 36
    P3 13 19
    P4 17 42

  4. C++ Program for Shortest Job First (SJF) Scheduling Algorithm

    #include
    #include
    using namespace std;

    int main()
    {
    int bt[20],p[20],wt[20],tat[20],i,j,n,total=0,pos,temp;
    float avg_wt,avg_tat;
    cout<>n;

    cout<<("\nEnter Burst Time:\n");
    for(i=0;i<n;i++)
    {
    cout<<"p"<<i+1<>bt[i];
    p[i]=i+1; //contains process number
    }

    //sorting burst time in ascending order using selection sort
    for(i=0;i<n;i++)
    {
    pos=i;
    for(j=i+1;j<n;j++)
    {
    if(bt[j]<bt[pos])
    pos=j;
    }

    temp=bt[i];
    bt[i]=bt[pos];
    bt[pos]=temp;

    temp=p[i];
    p[i]=p[pos];
    p[pos]=temp;
    }

    wt[0]=0; //waiting time for first process will be zero

    //calculate waiting time
    for(i=1;i<n;i++)
    {
    wt[i]=0;
    for(j=0;j<i;j++)
    wt[i]+=bt[j];

    total+=wt[i];
    }

    avg_wt=(float)total/n; //average waiting time
    total=0;

    cout<<"\nProcess\t Burst Time \tWaiting Time\tTurnaround Time";
    for(i=0;i<n;i++)
    {
    tat[i]=bt[i]+wt[i]; //calculate turnaround time
    total+=tat[i];
    cout<<"\nP["<<p[i]<<"]\t\t "<<bt[i]<<"\t\t "<<wt[i]<<"\t\t\t"<<tat[i];
    }

    avg_tat=(float)total/n; //average turnaround time
    cout<<"\n\nAverage Waiting Time= "<<avg_wt <<" milliseconds";
    cout<<"\nAverage Turnaround Time= "<<avg_tat <<" milliseconds"<<endl;

    system("pause");
    return 0;

    }

  5. #include
    using namespace std;
    int main()
    {
    int n,total,temp,temp1,total1;
    cout<>n;

    cout<<"\n";

    int p[n],b[n],w[n],t[n];

    for(int i=0;i<n;i++)
    {
    p[i]=i+1;
    cout<<" Burst time for process "<<i+1<>b[i];

    }

    for(int i=0;i<n;i++)
    {
    for(int j=0;jb[j+1])
    {
    temp=b[j];
    b[j]=b[j+1];
    b[j+1]=temp;

    temp1=p[j];
    p[j]=p[j+1];
    p[j+1]=temp1;

    }
    }
    }

    /* cout<<"\t Process id \t Burst time \n";
    for(int i=0;i<n;i++)
    {
    cout<<"\t "<<p[i]<<" \t\t "<<b[i]<<"\n";
    }
    */

    w[0]=0;

    for(int i=1;i<n;i++)
    {
    w[i]=w[i-1]+b[i-1];
    total=total+w[i];

    t[i-1]=w[i-1]+b[i-1];
    total1= total1+t[i-1];
    }

    t[n-1]=w[n-1]+b[n-1];

    float avg=(float)total/n;
    float avg1=(float)total1/n;

    cout<<"\n";
    cout<<"_____________________________________________________________________________\n";
    cout<<"|\t Process id \t Burst time \t Waiting time \t Turnaround time |\n";
    for(int i=0;i<n;i++)
    {
    cout<<"|\t P"<<p[i]<<" \t\t "<<b[i]<<" \t\t "<<w[i]<<" \t\t "<<t[i]<<"\t\t"<<" |"<<"\n";
    }

    cout<<"—————————————————————————–\n";

    cout<<"\n\t Average waiting time : "<<avg<<"\n";
    cout<<" \t Average turnaround time : "<<avg1<<"\n";
    }

  6. for(i=0;i<n;i++)
    {
    pos=i;
    for(j=i+1;j<n;j++)
    {
    if(bt[j]<bt[pos])
    pos=j;
    }

    temp=bt[i];
    bt[i]=bt[pos];
    bt[pos]=temp;

    temp=p[i];
    p[i]=p[pos];
    p[pos]=temp;
    }
    Sir in this loop the temp is used for two time,
    sir why we use this for two time

  7. Write a C programe using the pipe that perfrom following work:
    Parent: Ask two no from user and pass the number to child process.
    Child: add the numbers recvied from the parent and pass the result to parent.
    Parent: print the table of output received from child.

    how to do this program ,,i am not able to do . can anyone help me

Leave a Comment

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