If you’re diving into operating systems or CPU scheduling algorithms, you’ve probably come across SRTF, also known as Shortest Remaining Time First. It’s a preemptive version of the Shortest Job First (SJF) algorithm and is super important when it comes to handling multitasking efficiently.
Let’s walk through what it is, how it works, and how you can write a SRTF scheduling program in C. No fluff, just plain easy explanation and code.
Table of Contents
What is SRTF Scheduling?
SRTF (Shortest Remaining Time First) is a preemptive CPU scheduling algorithm.
That means the CPU will check if any new process has arrived with a shorter remaining time than the currently running process. If it does, then the CPU switches to the new one.
Why use SRTF?
Because it reduces average waiting time and turnaround time compared to some other algorithms. It’s ideal for systems where response time matters.
Key Terms to Know
- Arrival Time: When a process enters the queue.
- Burst Time: How long the process needs to run.
- Completion Time: When the process finishes.
- Turnaround Time: Completion Time – Arrival Time.
- Waiting Time: Turnaround Time – Burst Time.
How SRTF Works (in simple words)
- Check which processes have arrived.
- Among them, choose the one with the shortest remaining burst time.
- Run it for one unit of time.
- Check again if any process has arrived with a shorter remaining time.
- Repeat until all processes are complete.
Yep, it can switch tasks often—but that’s how it saves time overall.
C Program for SRTF Scheduling
Here’s a simple and clear program to help you understand how it works:
#include <stdio.h>
int main() {
int n, i, j;
printf("Enter number of processes: ");
scanf("%d", &n);
int at[n], bt[n], rt[n], ct[n], tat[n], wt[n];
int completed = 0, time = 0, min_rt, shortest;
float avg_tat = 0, avg_wt = 0;
for(i = 0; i < n; i++) {
printf("Enter Arrival Time and Burst Time for P%d: ", i+1);
scanf("%d %d", &at[i], &bt[i]);
rt[i] = bt[i];
}
while(completed != n) {
shortest = -1;
min_rt = 9999;
for(i = 0; i < n; i++) {
if(at[i] <= time && rt[i] > 0 && rt[i] < min_rt) {
min_rt = rt[i];
shortest = i;
}
}
if(shortest == -1) {
time++;
continue;
}
rt[shortest]--;
time++;
if(rt[shortest] == 0) {
completed++;
ct[shortest] = time;
tat[shortest] = ct[shortest] - at[shortest];
wt[shortest] = tat[shortest] - bt[shortest];
}
}
printf("\nProcess\tAT\tBT\tCT\tTAT\tWT\n");
for(i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\t%d\t%d\n", i+1, at[i], bt[i], ct[i], tat[i], wt[i]);
avg_tat += tat[i];
avg_wt += wt[i];
}
printf("\nAverage Turnaround Time = %.2f", avg_tat/n);
printf("\nAverage Waiting Time = %.2f\n", avg_wt/n);
return 0;
}
Sample Output
Enter number of processes: 3
Enter Arrival Time and Burst Time for P1: 0 6
Enter Arrival Time and Burst Time for P2: 1 2
Enter Arrival Time and Burst Time for P3: 2 8
Process AT BT CT TAT WT
P1 0 6 10 10 4
P2 1 2 3 2 0
P3 2 8 18 16 8
Average Turnaround Time = 9.33
Average Waiting Time = 4.00
Final Thoughts
SRTF is one of the best CPU scheduling algorithms in terms of minimizing waiting time. But yeah, it’s a bit harder to implement due to its preemptive nature.
This program gives you a clear idea of how to simulate the process queue and calculate the necessary metrics. Once you’re good with this, you can try adding Gantt chart visualizations or maybe handle process ID sorting and more advanced input validations.
Let me know if you want that version too 😉







