C Program for Shortest Job First (SJF) Scheduling Algorithm

Here you will get C program for shortest job first (sjf) scheduling algorithm.
 
In shortest job first scheduling algorithm, the processor selects the waiting process with the smallest execution time to execute next.

 
Below I have shared the C program for this algorithm.

 

Output

C Program for Shortest Job First (SJF) Scheduling Algorithm

28 thoughts on “C Program for Shortest Job First (SJF) Scheduling Algorithm

  1. Mandy

    The last line of the output displays the execution time.
    Please provide a code for that.
    Revert asap.
    Thank you 🙂

    Reply
  2. deepak

    Plz remove code from the text box.
    It’s very annoying and difficult to read from that.

    Reply
    1. Puru Goyal

      Hi! What help you need with online saloon management system!
      Please contact

      Reply
  3. revathi

    i need a program for (preemtive)sjf scheduling algorithm.
    pls rply fast

    Reply
  4. Nur

    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).

    Reply
  5. kalyani

    what if the process arrives at different time ex
    process arrival time service time
    1 0 3
    2 2 3
    3 3 5
    4 4 2
    5 8 3

    Reply
  6. Aayush Goyal

    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!

    Reply
    1. Arnab Saha

      #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

      Reply
  7. shankar

    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

    Reply
  8. Granit Nebiu

    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;

    }

    Reply
  9. Sheethal

    Are you ignoring arrival time? i dont see how you kept track of arrival time?

    Reply
  10. divayn mendoza

    hello. i need a combination program for FCFS and SJF with arrival time.

    Reply
  11. Harsh Saglani

    Thank you very much for the code. It helped me in the college practical

    Reply
  12. swathy

    this program doesnt contain gantt chart and table. we are not satisfied.

    Reply

Leave a Reply

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