Prerequisites: Make sure you are aware with scheduling and types of schedulers. If not then click on the link below.
Scheduling and types of schedulers
Before starting with Preemptive Scheduling and Non-Preemptive Scheduling lets look at some important terms which will help us understand concept easily.
- Arrival Time : It is the time at which a process arrives in ready queue.
- Completion Time : It is the time at which a process completes its execution.
- Burst Time : It is the time taken by CPU to execute a process.
- Turn-around Time : It is the difference between completion and arrival time.
- Waiting Time : It is the difference between turn-around time and burst time.
Preemptive Scheduling
In this type of scheduling a process can be preempt by another process. A process can switch from running to ready or waiting to ready state using preemptive scheduling.
The process is executed for some time and then it is placed back into the ready queue with its remaining burst time.
For example: In the picture above we have 4 processes. The process with least arrival time is taken first for execution. So, P2 is executed with arrival time 0.
But meanwhile P2 was executing another process P3 comes at arrival time 1 and now the remaining burst time for process P2 is 5 ms, so it will be moved into ready queue again with its remaining time and P3 will start its execution.
Similarly when P3 was executing another process P1 arrives at time 2. So, P3 will be moved back into the ready queue with its remaining burst time(3ms) and the process continues.
In this way preemptive scheduling works.
Non-Preemptive Scheduling
In this type of scheduling a process cannot be preempted while it is executing.
In this once CPU is given to a process then it gets terminated when it reaches its waiting state.
A process is not paused or moved back into ready queue until it completes its execution.
For example: The arrival time is the least for P2 so it will be given CPU first and it will execute for complete 6 ms. Meanwhile, P0, P1, and P3 will be added to ready queue in the order of their arrival time. When P2 will complete its execution then P3 will start with its execution and it will also execute for complete 4ms and in this way the process will continue.
So,this was non-preemptive scheduling.
Differences between Preemptive Scheduling and Non-Preemptive Scheduling
Preemptive Scheduling Non-Preemptive Scheduling
1. CPU is given to a process for some time If a process gets CPU then it execute for
and then if another process comes in between the given burst time and no process can
execution then it gets the CPU and this process preempt it.
moves to ready queue.
2. Starvation may be caused due to insertion Starvation may occur if a process with
of priority process in the queue. large burst time comes into CPU.
What is Starvation?
Starvation is a condition when a process does not get CPU indefinitely because each time a new process with higher priority comes and it gets executed.