Today we will learn the Shortest Job First Program in C. So, before start learning, you should have a little bit knowledge about Shortest job first. So,

What is Shortest Job First?

This is an approach which considers the next CPU burst. Each process posses its next CPU burst. When CPU is available, the process having the smallest next CPU burst is allocated CPU.

Now it may happen that two or more processes have the same next CPU burst. Then which process to allocate will be decided as per FCFS scheduling.

shortest job first program in c


This algorithm is simple to implement.

Does not depend on any priority of the process. The smallest burst time is the higher priority consideration.

It provides good CPU utilization than FCFS (First Come First Search).

Waiting time and turn around time of each process is reduced, reducing the average waiting time and turn around the time of the system as compared to FCFS.


Waiting time of some processes still high due to the long burst time of the processes, in case of non-preemptive scheduling.

In the case of non-preemptive scheduling, it may act as a uni-processing operating system.

In the case of preemptive scheduling, context switch is required.

And in preemptive scheduling, turn around time may get increased.

There are two schemes with this type of scheduling:

Non-preemptive: Once the CPU is allocated to a process, it can not be preempted until it completes its CPU burst.

Preemptive: If a new process arrives with CPU burst length less than remaining time of current execution process, preempt the current process. This scheme is known as Shortest-Remaining-Time-First (SRTF).

Working of non-preemptive SJF:

Consider the following set of processes and the respective CPU burst times:

shortest job first program in c

Initially, at time 0, only one process P1 is present in the ready queue. So will get scheduled and start execution.

As it is non-preempted scheduling, it will finish its CPU burst and then only terminate. So it gets terminated 7-time units.

Now at this time, there are P2, P3 and P4 processes present in the ready queue. After the process is finished, the next process is to be scheduled.

If FCFS would have been used, Process P2 was the next one to be scheduled. But as it is SJF, the process having least CPU burst will get scheduled i.e process P3 will get scheduled.

After it gets finished, now there are 2 processes, P2 and P4, having the same next burst time. Now to break this tie, FCFS is used.

Process P2 has arrived time 2.0 and P4 has 5.0. So P2 has arrived first so will get scheduled first and then after its completion, P4 will get scheduled.

The process is summarized in the following Gantt chart:

sjf program in c

1. Shortest Job First Program in C (Non-preemptive)

 int 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:");
    printf("\nEnter Burst Time:\n");
   //sorting of burst times
    printf("\nProcess\t    Burst Time    \tWaiting Time\tTurnaround Time");
        printf("\np%d\t\t  %d\t\t    %d\t\t\t%d",p[i],bt[i],wt[i],tat[i]);
    printf("\n\nAverage Waiting Time=%f",avg_wt);
    printf("\nAverage Turnaround Time=%f\n",avg_tat);


sjf program in c

In the above program, we have calculated the average waiting and average turn around time. After executing this program the compiler asks the user to enter the number of processes and then store it in n. Then compiler asks the user to enter the burst time and stored it in the array bt.

After the burst time is arranged in the next section so the shortest one can be executed first. Here the selection sorting is used for sorting of burst array bt.

The waiting time of the first element is always zero. So, the remaining waiting time is calculated by using two for loops. So the inner for loop is controlled by another for loop and inside that loop, waiting time is calculated by adding burst time to waiting time.

2. Shortest Job First Program in C (Preemptive)

#include <stdio.h>
int main() 
      int arrival_time[10], burst_time[10], temp[10];
      int i, smallest, count = 0, time, limit;
      double wait_time = 0, turnaround_time = 0, end;
      float average_waiting_time, average_turnaround_time;
      printf("\nEnter the Total Number of Processes:\t");
      scanf("%d", &limit); 
      printf("\nEnter Details of %d Processesn", limit);
      for(i = 0; i < limit; i++)
            printf("\nEnter Arrival Time:\t");
            scanf("%d", &arrival_time[i]);
            printf("Enter Burst Time:\t");
            scanf("%d", &burst_time[i]); 
            temp[i] = burst_time[i];
      burst_time[9] = 9999;  
      for(time = 0; count != limit; time++)
            smallest = 9;
            for(i = 0; i < limit; i++)
                  if(arrival_time[i] <= time && burst_time[i] < burst_time[smallest] && burst_time[i] > 0)
                        smallest = i;
            if(burst_time[smallest] == 0)
                  end = time + 1;
                  wait_time = wait_time + end - arrival_time[smallest] - temp[smallest];
                  turnaround_time = turnaround_time + end - arrival_time[smallest];
      average_waiting_time = wait_time / limit; 
      average_turnaround_time = turnaround_time / limit;
      printf("\n\nAverage Waiting Time:\t%lf\n", average_waiting_time);
      printf("Average Turnaround Time:\t%lf\n", average_turnaround_time);
      return 0;


preemptive scheduling

The difference between preemptive and non-preemptive is that when the two burst times are same then the algorithm evaluates them on the basis of FCFS (First Come First Served).

Also Read: