Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Preemptive Scheduling?
3.
Advantages of Preemptive Scheduling
4.
Disadvantages of Preemptive Scheduling
5.
What is Non-Preemptive Scheduling?
6.
Advantages of Non-Preemptive Scheduling
7.
Disadvantages of Non-Preemptive Scheduling
8.
Key Differences Between Preemptive and Non-Preemptive Scheduling
9.
Preemptive and Non-Preemptive Comparison Chart
10.
Frequently Asked Questions
10.1.
Why is FCFS non-preemptive?
10.2.
Is scheduling under 1 and 4 non-preemptive?
10.3.
What is preemptive priority scheduling?
10.4.
In Preemptive scheduling, what happens when several high-priority processes arrive simultaneously?
11.
Conclusion
Last Updated: Mar 27, 2024
Medium

Difference Between Preemptive and Non-Preemptive Scheduling

Author Divyansh Jain
0 upvote
gp-icon
Operating system track
Free guided path
14 chapters
83+ problems
gp-badge
Earn badges and level up

Introduction

When the CPU is idle, it is the role of the CPU scheduler to assign a task to it. The CPU scheduler chooses a process from the ready queue and assigns it to the CPU. The short-term scheduler is in charge of selecting (or CPU scheduler) the process from the ready queue. The scheduler selects a process from the processes in memory that is ready to execute. And, Preemptive and Non-Preemptive Scheduling are the two broad categories of process scheduling algorithms. We'll go over both of them in depth. So, let’s get started.

 Preemptive and Non-Preemptive Scheduling

Types of Priority Based Scheduling:

  1. Preemptive Scheduling
  2. Non-Preemptive Scheduling 

What is Preemptive Scheduling?

Preemptive Scheduling is a scheduling technique in which tasks are assigned based on priority. Even though the lower priority activity is still running, it is sometimes necessary to run a higher priority task before a lower priority task.

The lower priority work is put on hold for a while and then continues when the higher priority process finishes its execution.

In Preemptive scheduling, the scheduler will execute a process for a specific amount of time before passing it on to the next process in line. It has to wait to be executed again by the scheduler. The process may go to the ready state from the running state or the waiting state to the ready state. If there is still any CPU burst time available, the resources are granted to the process for only a short period before being removed, and the process is moved to the ready queue. Some famous preemptive scheduling algorithms are SJF (preemptive), Round-robin, etc.

Example: 

Let’s look at an example of Preemptive scheduling, which will clarify the whole process. Here we have taken four processes, P0, P1, P2, and P3, to be executed using Preemptive scheduling as a priority scheduling algorithm (Lower the Number, Higher the priority).

Process

Arrival Time

Priority

CPU Burst time

P0

0

4

5

P1

1

1

2

P2

3

3

4

P3

4

2

3

Gantt chart:

Gantt chart
  • To begin, the process P0 appears at time 0. As a result, the CPU is assigned to P0.
     
  • Process P1 arrived at the time one unit, while process P0 was operating, and the Priority for P0 (4) is less than the priority of process P1 (1). As a result, P1 is allocated to the processor.
     
  • Now, At time 3ms, P1 completes its execution time, and the CPU has to choose a process from the ready queue. While at the ready queue, we have the P2 process arrive at time 3ms. 
     
  • Now, among P0 and P2, the CPU will be allocated to the process P2 due to higher priority. And the execution of the process P2 will be started.
     
  • The execution was in progress but at the same time, Process P3 arrived. The priority for P2 (3) is less than P3(2). So now, P3 started the execution till time equal to 7ms.
     
  • Now P2 again got the chance to complete its execution. So, P2 finishes the execution at time 10ms.
     
  • And finally, at last, process P0 completed its remaining execution till time 14ms.

 

To calculate Turn Around Time and Waiting time,

  • Turn around time = Completion time - Arrival time
  • Waiting time = Turn around time - Burst time

Process

Arrival Time

Priority

CPU Burst time

Completion Time

Turn Around Time

Waiting Time

P0

0

4

5

14

14

9

P1

1

1

2

3

2

0

P2

3

3

4

10

7

3

P3

4

2

3

7

3

0

Now, Average Waiting time = [WT(P0) + WT(P1) + WT(P2) + WT(P3)] / 4

= [ 9 + 0 + 3 + 0 ] / 4

= 3 ms.

 

Note:

Turnaround Time = Completion Time - Arrival Time   

Waiting Time = Turnaround Time - Burst Time  

Average waiting time = 

(Sum of waiting time of all processes)/(Number of Processes)

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Advantages of Preemptive Scheduling

  • Each occurrence resulted in the disruption of ongoing tasks.
     
  • When used in a multiprogramming environment, preemptive scheduling is advantageous.
     
  • The average response time is also improved by using this scheduling strategy.
     
  • The Operating System makes sure that all processes use the same amount of CPU.
     
  • Preemptive scheduling is a more reliable approach that prevents one process from dominating the CPU.

Disadvantages of Preemptive Scheduling

  • If many high-priority processes arrive simultaneously, the low-priority process will have to wait longer.
     
  • The scheduler spends more time suspending the ongoing job, switching the context, and dispatching the new incoming task.
     
  • Scheduling requires a limited amount of computational resources.


Recommended Topic, FCFS Scheduling Algorithm, Multiprogramming vs Multitasking

What is Non-Preemptive Scheduling?

If a resource is allocated to a process under non-preemptive scheduling, that resource will not be released until the process is completed. Other tasks in the ready queue will have to wait their time, and it will not get the CPU forcefully. After a process has been assigned to it, it will hold the CPU until it has completed its execution or until an I/O action is required.

When a non-preemptive process with a long CPU burst time runs, the other process must wait for an extended period, increasing the average waiting time in the ready queue. Non-preemptive scheduling, on the other hand, contains no overhead when switching processes from the ready queue to the CPU. The execution process isn't even interrupted by a higher-priority task, implying that the scheduling is rigorous.

Example: 

Let's look at the above preemptive scheduling problem we have solved and see how we can handle it in a non-preemptive method. In contrast, the same four processes are P0, P1, P2, and P3, with the same arrival time and CPU burst time.

So, by using a Non-preemptive scheduler, the Gantt chart would look like-

Process

Arrival Time

CPU Burst time

P0

0

5

P1

1

2

P2

3

4

P3

4

3

Gantt chart:

 Gantt chart for Non-Preemptive Scheduling
  • The processor is allocated to process P0 since it arrives at 0 and takes five milliseconds to complete.
     
  • Meanwhile, all of the processes, P1, P2, and P3, arrive in the ready queue. All operations, however, must wait until Process P0 finishes their CPU burst period.
     
  • Process P0 will complete its execution at time = 5ms. The burst times are compared for the rest of the processes in the ready queue. Process P1 is executed because it has a shorter burst duration than other processes.
     
  • And similarly, following P1, P3, and then P2 will complete its execution.

 

So, let’s calculate the average waiting time for the non-preemptive method.

Process

Arrival Time

CPU Burst time

Completion Time

Turn Around Time

Waiting Time

P0

0

5

5

5

0

P1

1

2

7

6

4

P2

3

4

14

11

7

P3

4

3

10

6

3

Average waiting time = [WT(P0) + WT(P1) + WT(P2) + WT(P3)] / 4

= [ 0 + 4 + 7 + 3 ] / 4

= 3.5 ms.

So, the average waiting time for the preemptive method is less than the non-preemptive method which can further conclude that the preemptive method is more efficient in terms of time efficiency for the CPU.

Advantages of Non-Preemptive Scheduling

  • Low overhead in terms of scheduling is being offered.
     
  • It has a tendency to have a high throughput.
     
  • The approach is relatively easier to implement than other scheduling methods.
     
  • Very few computational resources are required in Non-preemptive scheduling.

Disadvantages of Non-Preemptive Scheduling

  • The response time is relatively much slower and inefficient.
     
  • It can make a priority and real-time scheduling challenging.
     
  • It can result in starvation, particularly for those real-time tasks.
     
  • The Bugs due to this scheduling can cause a system to become unresponsive.

Key Differences Between Preemptive and Non-Preemptive Scheduling

  • The major difference between both the scheduling is that the CPU is allotted to the processes for a certain amount of time in preemptive scheduling. At the same time, the CPU is allotted to the process in non-preemptive scheduling until it quits or changes to the other state.
     
  • Whenever a higher priority task arrives, the preemptive scheduling execution is interrupted in the middle of its execution. Non-preemptive scheduling, on the other hand, does not interrupt the execution process in the middle of it and waits until it is finished.
     
  • Preemptive Scheduling has the overhead of transferring a process from the ready state to the running state and vice versa and managing the ready queue. In contrast, non-preemptive scheduling has no overhead associated with switching from a running to a ready state.
     
  • If a high-priority process emerges in the ready queue on a regular basis, the low-priority process will have to wait a long time and may have to starve. In non-preemptive scheduling, if the CPU is assigned to a process with a large burst time, processes with lesser burst periods may be forced to starve.
     
  • Preemptive scheduling offers flexibility by offering access to the CPU to critical processes as soon as they join the ready queue, regardless of what job is currently running. Non-preemptive scheduling is considered stiff since the CPU of a critical process is unaffected if it joins the ready queue.
     
  • Preemptive Scheduling is cost associative because it must preserve the integrity of shared data, which is not the case with Non-preemptive Scheduling.

Preemptive and Non-Preemptive Comparison Chart

Parameter PREEMPTIVE SCHEDULING NON-PREEMPTIVE SCHEDULING
Basic In preemptive, resources (CPU Cycle) are allotted to a process for a limited time. While in Non-preemptive, Once resources (CPU Cycles) are allotted to a process, it is held until the burst time is completed or the process transitions to the waiting state.
Interrupt The process can be interrupted at any time. The process cannot be interrupted until it has been completed or the timer has expired.
Starvation A low priority process may starve if a high priority process often appears in the ready queue. If a process with a long burst time consumes CPU, subsequent processes with shorter burst times may suffer.
Overhead It contains overheads associated with process scheduling. There are no overheads in Non-preemptive scheduling.
Cost There is a cost associated with preemptive scheduling. There is no cost associated with non-preemptive scheduling.
CPU Utilization CPU consumption is high in preemptive scheduling. CPU consumption is low in Non-preemptive scheduling.
Examples Some of the preemptive scheduling is Shortest Remaining Time First and Round Robin. Some non-preemptive scheduling is Shortest Job First and First Come First Serve.

 

Must Read Evolution of Operating System, Open Source Operating System.

Frequently Asked Questions

Why is FCFS non-preemptive?

FCFS is non-preemptive because once a process starts execution, it continues until completion without interruption, regardless of priority or time constraints.

Is scheduling under 1 and 4 non-preemptive?

Yes, Scheduling under 1 and 4 is non-preemptive. It means if the resources are allocated in a process, they will not get released until the process is finished. One way to release the process is voluntarily stop/block the system.

What is preemptive priority scheduling?

Preemptive priority scheduling prioritizes tasks based on priority levels and can interrupt lower-priority tasks for higher-priority ones.

In Preemptive scheduling, what happens when several high-priority processes arrive simultaneously?

When many high-priority processes arrive at the CPU ready queue, then the low-priority processes need to wait for a long time. It is one of the disadvantages of preemptive scheduling.

Conclusion

To summarize the article, we learned both the scheduling methods in detail, got a clear idea about the priority scheduling used in both scheduling methods, and discussed the key differences between preemptive and non-preemptive scheduling. And finally, by head-to-head comparison, we cleared the basic parametric differences followed by the conclusion. But the knowledge never stops, so to better understand the operating systems, you can go through many articles on our platform.

Recommended Readings: 


Check out some of the amazing Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingBasics of C, Basics of JavaOperating SystemsComputer Networks, etc., along with some Contests and Test Series only on Coding Ninjas StudioEnrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations. 

Happy Learning!!

Previous article
Non-Preemptive Priority Based Scheduling
Next article
Measure the Time Spent in Context Switch
Guided path
Free
gridgp-icon
Operating system track
14 chapters
83+ Problems
gp-badge
Earn badges and level up
Live masterclass