Preemptive
CPU scheduler takes the process from ready queue and allocates the process to CPU for a limited time. Scheduler will allocate the process based on the priority, so low priority process may starve.This type of scheduling is flexible as each process in the ready queue gets some time to run CPU.
What is preemption ?
Act of temporarily interrupting a task being carried out by a computer. If a task is preempted, it is being disturbed/acquired by an another task.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 |
• 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.
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.
GANTT chart |
Advantages
Pre-emptive scheduling is more robust as one process can't utilize the CPU.
The usage of CPU is same because all the process will make use of CPU.
It is beneficial when it is used in multi-programming environment.
It improves the average response time.
Disadvantages
The process which has a low priority can wait for a long time if some high priority process continuously.
Examples of Preemptive scheduling - Round Robin Scheduling, Priority Scheduling, Shortest Job First Algorithm.
Non-preemptive
CPU scheduler takes the process from ready queue and allocates the process to CPU till the burst time completes or switches to waiting state. Scheduler will allocate the process till it completes. It will not be interrupted by another process. This type of scheduling is rigid.
Read about Race condition
Comments
Post a Comment