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.



C Program for Shortest Job First (SJF) Scheduling Algorithm

18 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 πŸ™‚

  2. deepak

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

    1. Puru Goyal

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

  3. Nur

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

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

  5. 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!

    1. Arnab Saha

      typedef struct
      int pid;
      float at, wt, bt, ta, st;
      bool isComplete;
      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;
      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)
      temp = p[k];
      p[k] = p[j];
      p[j] = temp;
      void main()
      int n, i, k = 0, j = 0;
      float avgwt = 0.0, avgta = 0.0, tst = 0.0;
      printf("Enter number of processes: ");
      process p[n];
      for (i = 0; i<n; i++)
      printf("\nEnter process %d's details: ",i);
      for (i = 0; i<n; i++)
      if (p[i].isComplete == true)
      k = i;
      while (p[i].at<=tst && i<n)
      sort (p,i,k);
      i = k;
      p[i].st = tst;
      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);

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


Leave a Reply

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