SRTF Scheduling In OS

Shortest Remaining Time First (SRTF) scheduling is a preemptive CPU scheduling algorithm where any process which contains shortest remaining burst time is always executed first. If a new process arrives which contains shorter remaining execution time than the currently running process, then the CPU switches to the new incoming process immediately.

SRTF is designed to reduce the average waiting time and average turnaround time. It may cause starvation for long processes; therefore, it requires continuous monitoring of the ready queue.

How SRTF Scheduling Works

The working steps of SRTF scheduling are given below:

  • Processes enter the ready queue according to their arrival time.
  • The CPU selects the process with the shortest remaining burst time.
  • The selected process starts executing.
  • If a new process arrives with a shorter remaining burst time, the CPU switches to the new process; the current process is interrupted and returned to the ready queue.
  • The CPU starts executing the new process.
  • These steps continue until all processes finish execution.

Example of SRTF Scheduling

Consider the following processes.

Process Arrival Time (AT) Burst Time (BT)
P1 0 8
P2 1 4
P3 2 2
P4 3 1

Ready Queue Behavior

  • Time 0: Only P1 is available, so P1 starts executing.
  • Time 1: P2 arrives. Remaining time of P1 is 7, while P2 needs only 4 units, so P2 preempts P1.
  • Time 2: P3 arrives. Remaining time of P2 is 3, while P3 needs only 2 units, so P3 preempts P2.
  • Time 3: P4 arrives. Remaining time of P3 is 1, and P4 also needs 1 unit. Since P3 is already running, it continues and finishes.
  • Time 4: P4 executes and completes.
  • Time 5: P2 resumes and finishes.
  • Time 8: P1 resumes and completes.

Gantt Chart

0      1      2      4      5      8             15
| P1 | P2 | P3 | P4 | P2 |     P1      |

Completion Time (CT)

Process CT
P1 15
P2 8
P3 4
P4 5

Turnaround Time (TAT)

Formula:

TAT = Completion Time − Arrival Time

Process TAT
P1 15
P2 7
P3 2
P4 2

Waiting Time (WT)

Formula:

WT = Turnaround Time − Burst Time

Process WT
P1 7
P2 3
P3 0
P4 1

Average Waiting Time (AWT)

[
AWT=\frac{7+3+0+1}{4}=2.75
]

Average Turnaround Time (ATAT)

[
ATAT=\frac{15+7+2+2}{4}=6.5
]

Advantages of SRTF Scheduling

The advantages of SRTF scheduling are listed below.

  • Reduces average waiting time.
  • Improves average turnaround time.
  • Gives faster response to short processes.
  • Better CPU utilization than FCFS.
  • Suitable for interactive systems.
  • Produces good overall system performance.

Disadvantages of SRTF Scheduling

The disadvantages of SRTF scheduling are given below.

  • Long processes may suffer from starvation.
  • Requires accurate estimation of CPU burst time.
  • Frequent context switching increases overhead.
  • More complex than FCFS and SJF.
  • Not suitable when burst times are unknown.

Starvation in SRTF Scheduling

Starvation occurs when long processes continuously wait because new short processes keep arriving. Since SRTF always gives priority to the process with the shortest remaining time, a long process may be delayed for a very long time.

Example:

  • P1 has a burst time of 30 ms.
  • Small processes of 2 ms keep arriving.
  • P1 is repeatedly interrupted.
  • As a result, P1 waits much longer before completing.

Context Switching in SRTF

Context switching happens whenever the CPU stops executing one process and starts another.

In SRTF, context switching occurs frequently because every newly arrived shorter process can interrupt the currently running process. Although this improves response time, excessive context switching increases CPU overhead.

Performance Measures of SRTF Scheduling

The performance of SRTF scheduling is evaluated using the following measures.

  • Gantt Chart
  • Completion Time (CT)
  • Turnaround Time (TAT)
  • Waiting Time (WT)
  • Average Waiting Time (AWT)
  • Average Turnaround Time (ATAT)
  • CPU Utilization
  • Throughput
  • Response Time

Applications of SRTF Scheduling

SRTF scheduling is commonly used in the following situations.

  • Interactive operating systems
  • Time-sharing systems
  • Real-time environments with short tasks
  • Process scheduling requiring quick response
  • CPU scheduling where short jobs should finish earlier

Difference Between SJF and SRTF

Feature SJF SRTF
Scheduling Type Non-preemptive Preemptive
CPU Interruption Not Allowed Allowed
Decision Basis Shortest burst time Shortest remaining burst time
Response Time Higher Lower
Context Switching Less More
Average Waiting Time Low Usually lower than SJF

When to Use SRTF Scheduling

SRTF scheduling is suitable when:

  • Short response time is required.
  • Burst times can be estimated.
  • Interactive systems are used.
  • Reducing average waiting time is the main objective.
  • The system can handle frequent context switching.

FAQs

Is SRTF preemptive or non-preemptive?

SRTF is a preemptive CPU scheduling algorithm because a running process can be interrupted when another process with a shorter remaining burst time arrives.

What is the main goal of SRTF scheduling?

The main goal is to minimize the average waiting time and turnaround time by always executing the process with the shortest remaining execution time.

Can starvation occur in SRTF?

Yes. Long processes may experience starvation if short processes continue arriving.

What is the difference between SJF and SRTF?

SJF is non-preemptive and does not interrupt the running process, while SRTF is preemptive and can switch the CPU to a newly arrived process with a shorter remaining time.

Which scheduling algorithm performs better, SJF or SRTF?

SRTF generally provides lower average waiting time and better response time than SJF, but it also causes more context switching.