Definition: Preemptive Scheduling is defined as the scheduling which is done when the process changes from running state to ready state or from waiting for the state to ready state. In this, the resources are allocated to execute the process for a certain period. After this, the process is taken away in the middle and is placed in the ready queue its bursts time is left and this process will stay in ready line until it gets its turn to execute.
Suppose if a process which has the highest priority arrives, then this process does not wait for the complete execution of the current process. Instead of it, the ongoing process is interrupted in between and is placed in the ready queue until the process which has the highest priority does its execution. Thus in this way, all the processes which are in the available line get some time to run.
Example of preemptive scheduling are: Round robin scheduling, priority scheduling, and shortest job first(SJF) scheduling
Explanation
Suppose there are four processes P1, P2, P3 and P4 whose arrival time and burst time are given in the table below:
Process | Arrival Time | CPU Burst Time |
P1 | 3 | 2 |
P2 | 2 | 4 |
P3 | 0 | 6 |
P4 | 1 | 4 |
Lets us understand this preemptive scheduling.
• Here, the process P3 arrives first i.e at time 0. As we know there is no process in the queue. So, the CPU is allocated to the process P3.
• When the process P3 was executing, the process P4 interrupts in-between as it arrives at time 1. Now the time left for the complete execution of the process P3 is 5 ms. This time is more than the time required by the process P4 for its implementation. So, process P4 is allocated to the CPU.
• When the process P4 was executing, the process P2 arrival time is reached. Now the time left for the execution of P4 is 3 ms. This time is less than the time required by process P2 and process P3. So, now P4 will continue.
• When P4 is ongoing with its execution, process P1 arrives. Now the time left for the implementation of the process P4 is 2ms. This time is equal to the time required by the process P1 which is 2ms. So, P4 will continue its execution.
• When the process P4 completes its execution, CPU will be allocated to the process P1 as its burst time is less than the other processes.
• After the completion of process P1, the process P2 will be executed, and in the end process, P3 will be executed.
Now we see how the execution takes place through GANTT chart.
We’ll be covering the following topics in this tutorial:
Difference between preemptive and non-preemptive scheduling
Preemptive Scheduling | Non-Preemptive Scheduling |
In preemptive scheduling, the processes are allocated for a short period. | In non-preemptive scheduling, the process is allocated to the CPU, and the resource will hold the process until it completes its execution or changes its state to waiting for the state from ready state. |
In this, we can interrupt the process in between of execution. | In this, we can not interrupt until the process completes its execution or switches its state. |
This type of schedule is flexible as each process in the ready queue gets some time to run CPU. | This type of scheduling is rigid. |
Preemptive scheduling has overheads of scheduling the processes. | Non-preemptive scheduling has not overheads of scheduling the processes. |
There is a cost associated with the preemptive scheduling. | There is no cost associated with non-preemptive scheduling. |
In preemptive scheduling, if a process which has high priority arrives in the ready queue, then the process which has low priority may starve. | In non-preemptive scheduling, if the process which has long burst time is executing, then the other method which has less burst time may starve. |
Advantages of Preemptive Scheduling
• Preemptive scheduling is more robust as one process can not utilize the CPU.
• In this, the usage of CPU is same, i.e., all, the running processes will make use of CPU equally. You can also say that preemptive scheduling is fair.
• It improves the average response time.
• Preemptive scheduling is beneficial when we use it for the multi-programming environment.
Disadvantages of preemptive scheduling
• The process which has low priority can wait for a long time if some high priority processes arrive continuously.