Difference Between Preemptive and Non-Preemptive Scheduling

feature-image-of-difference-between.jpg

In today’s world, computers manage multiple tasks at the same time, deciding which process runs and when. Process scheduling ensures smooth system operation by managing CPU time based on process priorities. Among the various methods available, preemptive and non-preemptive scheduling are the two main types. Preemptive scheduling allows the CPU to switch between tasks based on priority, while non-preemptive scheduling lets a task finish before moving to the next. Each method uses a different approach and affects system performance in unique ways. In this blog, you will understand what preemptive and non-preemptive scheduling are, how they work, and their key differences in detail.

Table of Contents:

What is Preemptive Scheduling?

Preemptive scheduling is a CPU scheduling strategy in which the currently executing process may be interrupted and the CPU may be given to another process that is demanding attention. It is similar to your memory of a strict manager being able to come and take a worker off a task if something more urgent needs to be addressed. A preemption can happen for many reasons, such as a different, higher-priority process becoming available for execution or when the time slice for the currently running process has expired. The primary purpose of preemptive scheduling is simply to ensure that every process gets access to the CPU and high-priority processes do not have to wait indefinitely.

What is Non-Preemptive Scheduling?

Non-preemptive scheduling is an alternative to preemptive scheduling. In non-preemptive scheduling, once a process begins executing, it continues to execute until it finishes or the process releases the CPU. A non-preemptive scheduling example would be a worker who is given a task that they complete until they finish, or when they decide to take a break. There is no external force that can interrupt a running process in non-preemptive scheduling. The process must finish or give up the CPU on its own before another process can use it. It can’t be stopped or interrupted while it’s running.

Become a Job-Ready Software Engineer
Master coding, system design, and real-world projects with expert mentors. Get certified and land your dream tech job
quiz-icon

Differences Between Preemptive and Non-Preemptive Scheduling

Feature Preemptive Scheduling Non-Preemptive Scheduling
Interruption A running process can be interrupted to allow another process to execute A running process cannot be interrupted until it completes or blocks
Responsiveness More responsive and ideal for interactive or time-sensitive applications Less responsive and better suited for background or batch processing
CPU Utilization Improves CPU efficiency by sharing CPU time among processes May lead to lower CPU utilization if a process runs too long
Overhead Higher overhead due to frequent context switching Lower overhead as context switches occur less frequently
Priority Handling Supports priority-based execution and high-priority tasks can preempt others Priority is considered only when the CPU is free and no preemption occurs
Complexity More complex to design and manage due to interrupt handling and synchronization Simpler to implement and manage with fewer synchronization concerns
Starvation Possibility High-priority processes may cause starvation of low-priority ones Starvation is less likely as processes are completed in order
Real-Time Suitability Well-suited for real-time systems where quick response is needed Not ideal for real-time systems due to delayed execution

Key features of Preemptive Scheduling

1. Faster Response: Preemptive scheduling allows a system to respond faster to new or higher-priority events.

2. Emphasizes Fairness in CPU Usage: It is focused on fairer use of the CPU time slice, so that no single process can monopolize the CPU time.

3. Allows Task Interruption: The primary distinction of preemptive scheduling is that the operating system can suspend any task that is currently running.

Preemption Ensures Efficient CPU Usage: It prevents one process from holding on to the CPU for too long while also allowing other tasks to run in the system.

5. Allows Process Switching: This type of scheduling is intended for quick switching between processes. Preemptive scheduling is highly useful for time-sharing systems with multiple users, applications, and tasks actively running on the system at the same time.

Working of Preemptive Scheduling

The operating system uses a method, typically using a timer interrupt or priority queue, to know when to switch processes. When one process is executing, it is possible that a timer is set. If the timer expires while the process is executing, the operating system will suspend the process and then save the state of the running process, and choose another process to run. The operating system will also preempt the currently running lower-priority process if a new process arrives with a higher priority than the lower-priority process. Since preemptive scheduling means there is almost always a potential for interruption in any current task, the scheduling system must constantly monitor for process switching and interrupts.

Advantages and Disadvantages of Preemptive Scheduling

Preemptive scheduling has both advantages and disadvantages that affect system performance and complexity. Understanding these helps in selecting the best approach for different computing environments.

Advantages of Preemptive Scheduling

  • Increased responsiveness for interactive applications.
  • Better distribution of CPU time among processes.
  • Suitable for real-time systems where timely execution is required.
  • Prevents any single process from holding CPU control for too long.
  • Ensures critical tasks receive prompt attention.

Disadvantages of Preemptive Scheduling

  • Frequent context switching may lead to thrashing, reducing performance.
  • Frequent context switching results in higher overhead.
  • It can reduce overall system throughput.
  • Adds complexity to the design of the operating system.
  • More difficult to implement properly.

Get 100% Hike!

Master Most in Demand Skills Now!

Key features of Non-Preemptive Scheduling

1. Simple to Implement: Non-preemptive scheduling is, in general, easier to design or implement than preemptive scheduling since it does not have to implement complex interruption designs.

2. Less Context Switching Overhead: Once a process begins, it runs until it finishes or voluntarily gives up the CPU, which reduces the overhead of the operating system, saving and restoring the process state.

3. Task Efficiency: This model enables a task to run on the CPU uninterrupted, so for those long-running, CPU-bound type jobs, this works very efficiently.

4. Predictable Task Completion: As the tasks run without interruptions or preemptions, their completion times are more predictable due to the absence of external interference.

5. Appropriate for Batch System: This method works more effectively in environments where it is a batch processing way to execute a queue of jobs, where the jobs will run sequentially one after the other, and the disruptive element of immediate user interaction is not a high priority in real time.

Working of Non-Preemptive Scheduling

The operation of a non-preemptive scheduling model is straightforward. Once a process is selected to run, it retains full control of the CPU and continues execution until it either completes or performs an I/O operation. (For example, during a read from disk) the process willingly gives up the CPU when it finishes or needs input or output. In non-preemptive scheduling, the scheduler can assign the CPU to another process only when the current one finishes or voluntarily yields. It operates on the principle of “run to completion or yield.”

Advantages and Disadvantages of Non-Preemptive Scheduling

Non-preemptive scheduling has both advantages and disadvantages that impact system efficiency and simplicity. Knowing these helps in choosing the right method for various system requirements.

Advantages of Non-Preemptive Scheduling

  1. Minimal context switching, which means low overhead.
  2. More CPU time is available to run the actual important tasks.
  3. Better for systems with tasks that require consistent and uninterrupted execution.
  4. Easier to implement and maintain.
  5. No chance of race conditions in critical sections since processes run to completion.

Disadvantages of Non-Preemptive Scheduling

  1. Not suitable for real-time or time-sharing systems.
  2. No responsiveness, especially for interactive work.
  3. Long-running processes can block higher-priority tasks from running.
  4. Increased wait times for other processes.
  5. Can lead to a poor user experience.

Common mistakes for Preemptive and Non-Preemptive Scheduling

1. Improper Preemption Interval (Preemptive): If the interval is too small, it leads to excessive context switching. If the interval is too large, it reduces responsiveness and the system starts behaving like a non-preemptive one.

2. Lack of Shared Resource Coordination (Preemptive): Interrupting a process using shared resources may cause race conditions, data corruption, or system crashes.

3. Poor Algorithm Fit for Interactive Workloads (Non-Preemptive): Long-running tasks can block the CPU and delay time-sensitive or high-priority operations.

4. Uneven Task Execution Time (Non-Preemptive): Assuming all tasks have similar durations can lead to CPU monopolization by longer processes while others remain idle.

5. No Recovery from Stuck Processes (Non-Preemptive): If a process enters an infinite loop or gets stuck in a deadlock, it can freeze the system because there is no way to interrupt it.

Best Practices for Preemptive and Non-Preemptive Scheduling

1. Maximize Responsiveness (Preemptive): Implement effective priority management and optimize time slices to ensure high-priority and interactive tasks receive timely CPU access and maintain system responsiveness.

2. Ensure Data Consistency (Preemptive): Use synchronization mechanisms like mutexes and semaphores to protect shared resources and prevent race conditions and data corruption during context switches.

3. Match Scheduling to Workload Type (Non-Preemptive): Apply non-preemptive scheduling to systems with predictable, long-running tasks such as batch jobs or embedded systems that rely on precise timing and minimal context switching.

4. Minimize Starvation and Delays (Non-Preemptive): Break long tasks into shorter ones or have them yield the CPU periodically. Use watchdog timers to detect and recover from stuck or unresponsive processes.

5. Balance Throughput and Latency (Both): Select a scheduling strategy that aligns with system goals by weighing task completion rate (throughput) against the responsiveness required for real-time or interactive applications.

Real-World Example for Preemptive and Non-Preemptive Scheduling

Let’s now see how the preemptive and non-preemptive scheduling works with the help of an example.

Processes:

Characteristics Preemptive Priority Non-Preemptive SJF
Interruption Yes, interruption occurs. For example, P1 is paused when P2 arrives. No, processes run to completion. P2 waits for P1 even if it arrives earlier.
Average Waiting Time P1: 3, P2: 0, P3: 5 → Average = 2.67 P1: 0, P2: 4, P3: 5 → Average = 3.0
Common Use Case Suitable for real-time systems where priority handling is crucial. Used in batch systems where simpler, uninterrupted execution is preferred.

Goal to reach: To schedule the process based on priority.

1. Preemptive Priority Scheduling

Rules to follow: CPU in preemptive priority scheduling will always allocate the task that is on a high-priority process immediately on arrival. This will be working on high priority even if there is another process running on the CPU.

Steps:

  1. Time 0: P1 arrives and immediately starts executing, as there are no other processes in the system.
  2. Time 2: P2 arrives with a higher priority (1) than P1. Therefore, P2 will preempt P1, pausing its execution, and P2 will start running.
  3. Time 5: P2 will complete its process. Then again, P1 will resume its work. At this point, P1 still has 4 units of burst time remaining.
  4. Time 6: Now, P3 arrives with priority = 3, which is lower than P1’s priority = 2, so P1 continues executing.
  5. Time 9: P1 completes. Now, P3 will start running.
  6. Time 13: At the end, P3 completes its processing.

Gantt Chart:

preemptive gantt chart

Note: Here, the P1 was interrupted at t=2, when P2 arrived.

2. Non-Preemptive uses Shortest Job First (SJF)

Rules to follow: This method follows the Shortest Job First (SJF) principle by selecting the task with the shortest burst time among the available ones. It does not reconsider shorter jobs that arrive later.

Steps:

  1. Time 0: P1 arrived and started running the process.
  2. Time 2: Then, P2 arrived with a burst time of 3, which is less than P1’s, so the remaining time will be 4, but P1 continues in the non-preemptive method.
  3. Time 6: Then P1 has completed the task.
  4. Queue: Now, P2, which has a burst time of 3, and P3 has a burst time of 4. This makes the P2 run first, as P2 is the shortest.
  5. Time 9: Now, P2 has completed its task. Then P3 starts to run.
  6. Time 13: In the end, P3 completes its process.

Gantt Chart:

non-preemptive gantt chart

Note: Here, P2 arrived earlier but had to wait for P1 to finish its task.

Characteristics Preemptive Priority Non-Preemptive SJF
Interruption Yes, interruption happens. P1 is stopped when a higher-priority P2 arrives No interruption. P2 waits until P1 finishes, even if it arrives first
Average Waiting Time P1 is 3, P2 is 0, P3 is 5. So, average is 2.67 units P1 is 0, P2 is 4, P3 is 5. So, average is 3 units
Common Use Cases Used in real-time systems where priority decides which task runs first Used in batch systems where tasks are done in order of arrival or duration

Conclusion

The core difference between preemptive scheduling and non-preemptive scheduling in operating systems is responsiveness and efficiency. Preemptive scheduling focuses on quickly switching tasks to ensure fairness and meet real-time needs. Non-preemptive scheduling allows tasks to finish without interruption, making it easier to implement and better suited for batch processing. Knowing this key difference helps system designers pick the right scheduling method based on their system’s needs.

Take your skills to the next level by enrolling in the Software Engineering Course today and gain hands-on experience. Also, prepare for job interviews with Software Engineering Interview Questions prepared by industry experts.

Difference Between Preemptive and Non-Preemptive Scheduling – FAQs

Q1. What is the main difference between preemptive and non-preemptive scheduling?

Preemptive scheduling allows interruption of a running task for a higher-priority one, while non-preemptive scheduling lets a task run until it finishes or waits.

Q2. What is the difference between a process and a thread?

A process is an independent program in execution with its own memory space, while a thread is a smaller unit within a process that shares the same memory but runs independently.

Q3. What are preemptable and non preemptable resources?

Preemptable resources, such as the CPU, can be reassigned without issues, while non-preemptable resources, like printers, must be released by the process using them.

Q4. What is an example of preemptive scheduling?

Round Robin and Shortest Remaining Time First (SRTF) are examples of preemptive scheduling.

Q5. Is round robin non-preemptive?

No, Round Robin is preemptive, as it uses time slices to switch between processes.

About the Author

Senior Consultant Analytics & Data Science, Eli Lilly and Company

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

fullstack