# Preemptive Shortest Job First Algorithm C Program

## C Program To Implement Preemptive Shortest Job First Algorithm

Learn How To Implement Preemptive Shortest Job First Algorithm in C Programming. This C Program For SJF Algorithm using Arrival Time makes use of Arrays. The SJF Scheduling Algorithm is a Disk Scheduling Algorithm for processing various jobs entering the CPU. The SJF Scheduling Algorithm is also known as **Shortest Process Next** Scheduling Algorithm.

#### What is Preemptive Shortest Job Scheduling Algorithm?

The Shortest Job First Scheduling Algorithm executes the processes having the least burst time or execution time from the Ready Queue. The processes are, therefore, compared with each other and arranged based on their execution times. The processes which have the largest execution time executes at the last.

Since this SJF Scheduling Algorithm is a Preemptive Scheduling algorithm, the CPU can, therefore, leave a process and switch to another process. This technique, therefore, reduces the overall Response Time of the processes and increases the system performance.

#### C Program For Preemptive Shortest Job Algorithm with Arrival Time

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 compilation errors or doubts in this C Program to Implement Preemptive Shortest Job First Scheduling Algorithm using Arrays and Arrival Time, let us know about in the Comment Section below. Find more about SJF Algorithm 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?