Preemptive Scheduling

About Preemptive scheduling

Preemptive scheduling is an essential concept in operating systems, and on this page, we will explore it through detailed explanations and illustrative examples. This scheduling method ensures that tasks with higher priority interrupt and replace currently running lower-priority tasks, enhancing system responsiveness and efficiency.

On this page, we will learn the concepts of preemptive scheduling with the help of detailed explanation along with examples. Preemptive scheduling is a type of scheduling process which helps to schedule tasks with higher priority first and then the tasks with comparatively lower priority.

Preemptive scheduling In OS

Overview

 In an operating system (OS), a process scheduler performs scheduling of process. It includes switching of processes between the ready queue and waiting queue and allocating them to the CPU. The operating system assigns priority to each process and maintains these queues. The scheduler selects the process from the queue and loads it into memory for execution. This process scheduling takes place in two types: preemptive scheduling and non-preemptive scheduling.

Preemptive Scheduling

In preemptive type of scheduling algorithm, a low priority process gets suspended from its execution, if a high priority process is waiting in the same queue for its execution. It is used when a process or task switches from running state to ready state or from waiting state to ready state. In this scheduling, the higher priority process is executed first.

At times, it is necessary to run a process that has a higher priority. If a high priority process arrives in the ready queue, it does not have to wait for the current process to complete its burst time. Instead, the running process is interrupted and is placed in the ready queue until the process with high priority is utilizing the CPU cycles. In this way, each process in the ready queue gets some time to run CPU.

Advantages of Preemptive Scheduling:

  1. Improved Response Time:
    Preemptive scheduling ensures that time-sensitive tasks (with high priority) get immediate access to CPU resources, improving overall system responsiveness.
  2. Fairer Process Handling:
    All processes in the ready queue are given a chance to run, ensuring fairness. Low-priority tasks will not be starved unless high priority tasks continuously arrive.
  3. Effective for Time sharing Systems:
    In systems where multiple users interact with the CPU, preemptive scheduling ensures that no single process monopolizes CPU time, keeping all users’ tasks running smoothly.

Disadvantages of Preemptive Scheduling:

  1. Context Switching Overhead:
    Frequent interruptions lead to increased context switching, which involves saving and restoring process states, consuming CPU time.
  2. Complexity in Management:
    Managing priority levels and determining the right moment for preemption adds complexity to the system scheduler.
  3. Starvation:
    Low priority processes may suffer from starvation if high priority tasks are constantly entering the queue.

Preemptive Scheduling  Example

P1 and P2 have different arrival times in the queue. Based on their priority or burst time, these processes are interrupted and allocated to the CPU. A process P1 arrives at time 0 and is allocated the CPU. Process P2 arrives at time 2, before P1 finishes execution. See the table below.

Preemptive Scheduling Example​
Preemptive Scheduling

Types of Preemptive Scheduling algorithms

Shortest Job First (SJF) Scheduling

It is also called the Greedy In Preemptive Shortest Job First Scheduling, the processes are put into the ready queue as per their arrival time. When a process with the shortest burst time arrives, the existing process is removed from execution, and the process with the shortest burst time is executed first.

Round-Robin (RR) Scheduling

It is also called time-slicing scheduling. It is primarily designed for a time sharing In Round Robin (RR) Scheduling, the process is allocated to the CPU for the specific time period (time slice). If the process completes its execution within this time period, then it is removed from the queue or else it has to wait for another time slice.

Closing remarks

Preemptive Scheduling plays a vital role in improving system responsiveness and fairness by allowing high priority processes to access the CPU without delay. It is particularly beneficial for time-sharing systems and ensures that all tasks get CPU time efficiently. Despite its advantages, such as improved response time and fairness, it also introduces challenges like context switching overhead and the risk of starvation for lower priority tasks. Understanding its types and functioning is essential for optimizing CPU scheduling in modern operating systems.

FAQs

Preemptive scheduling is preferred in real-time systems because it ensures immediate execution of high-priority tasks, which is crucial for time-sensitive operations.

It enhances multitasking by allowing multiple processes to share CPU time effectively, improving system responsiveness and user experience.

Yes, mobile OS like Android use preemptive scheduling to manage background and foreground tasks efficiently, maintaining smooth performance.

Priority can be determined by various factors such as process type, required CPU time, user input, or assigned priority levels by the system.