Let us learn how to implement the preemptive shortest job first scheduling algorithm in C programming with its explanation, output, advantages, disadvantages and much more.
What is Preemptive Shortest Job Scheduling Algorithm?
According to the SJF algorithm, the jobs in the queue are compared with each other and the one with shortest burst time gets executed first.
The remaining processes are also executed in the order of their burst times. However, there may be scenarios where one or more processes have same execution time.
In such cases, the jobs are based on first come first serve basis or in other words, FIFO approach is used.
This is a preemptive algorithm which means that the CPU can leave a process while under execution, and can move to the next process in the queue.
Meanwhile, the current state of the process is saved by context switch and another job can be processed in the meantime.
Once the CPU scheduler comes back to the previous job which was incomplete, resumes it from where it was stopped.
The shortest job first algorithm is also popularly known by the following names:
- Shortest Remaining Time First algorithm
- Shortest Job Next algorithm
- Shortest Process Next algorithm
Note: This SJF preemptive scheduling program in c with output considers the arrival time of the processes entering the job queue.
Advantages
- The response time is much better as compared to FCFS algorithm.
- Minimum average waiting time is achieved.
- The throughput time is good as the burst time of the processes is less.
- Optimum turnaround time.
Disadvantages
- The execution time of all the jobs in the queue must be known in advance which is not possible in all the scenarios.
- The processes with larger burst time will have a high waiting time, and this may lead to starvation.
Note: This preemptive shortest job first scheduling program in c language is compiled with GNU GCC compiler using Linux terminal on Linux Ubuntu operating system.
C Program For Preemptive Shortest Job Scheduling Algorithm
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | #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 Processes\n", 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; } } burst_time[smallest]--; if(burst_time[smallest] == 0) { count++; 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; } |
Output

If you have any doubts about the implementation of the preemptive shortest job first scheduling program in c language, let us know about it in the comment section. Find more about it on Wikipedia.
Thank you so much. I was not able to find a working program for Preemptive SJF code anywhere.
Why do we have to use Arrival Time in Preemptive Algorithm Code?
What is the difference preeemptive and non preemptive sjf algorithm?
Let there are 3 processes-1000,1001,1002
Suppose at any point if two process have same burst time then it is broken by giving priority to the process with the lowest id no
What will be the program for this?
Nice you put a code in Comic Sans font. B-)
why is the “end,time and limit” used here???
hey i need some help anyone please ????