Preemptive Scheduling
Overview :
Preemptive scheduling is the most commonly utilized scheduling method in real-time systems. Jobs are prioritized here, and the job with the greatest priority among all other tasks receives CPU time. If a task with a higher priority than the presently performing task becomes available, the kernel preserves the current process's context and changes to the higher priority task by loading its context. Typically, the highest priority job continues until it is completed or becomes non-computable, such as queuing for a resource to become accessible or invoking a function to wait/delay.
Introduction :
In real-time operating systems, the most often used scheduling algorithm is preemptive scheduling. So, what exactly is a real-time operating system? A real-time operating system(RTOS) is something that exists in the realm of microcontrollers. One machine can support real-time applications by using RTOS in microcontrollers. It will complete a task at the appointed time—not earlier, not later, but at the precise predetermined moment!. Time is the variable! Time is the only thing that worries us. This is a vital necessity for a real-time system on which our modern way of life may rely, such as an aircraft system control board or health devices.
Let's introduce the idea of a scheduler.
In any Case, What Exactly is a Scheduler?
The RTOS scheduler is an algorithm that the RTOS utilizes to schedule threads (tasks). The RTOS scheduler decides which processes should execute on each CPU in the system at any given time. Run queue — A queue of threads that are ready to be scheduled. Threads in the run queue are either executing or preparing to execute.
The scheduler implements tasks in two ways: preemptive and non-preemptive scheduling.
This essay will delve much deeper into the topic of preemptive scheduling.
What do We Mean by "Preemptive Scheduling"?
In real-time systems, the most often used scheduling algorithm is preemptive scheduling. In the preemptive priority scheduling method, if a higher priority process comes in the waiting queue, even if a low priority process is already willing or in the process of transmission, it is halted and placed on hold until the high priority process is processed. The CPU cycle is eventually transferred to the process with a higher priority. This is preemptive because a process that is currently running can be interrupted to allow a higher priority process to run, or in other words, the higher priority process will simply preempt the lower priority process.
Preemptive scheduling algorithms include Round Robin (RR), Shortest Remaining Time First (SRTF), Priority (preemptive variant), and others.
Example of preemptive scheduling
The following example will help you better grasp preemptive priority scheduling. All of the processes, as well as their arrival ( the time at which a process arrives in the ready queue) and burst timings (the time required by a process for CPU execution), are listed here. You haven't heard anything about it, have you? Please consider reading this first.
We must also take note of their priority numbers, which are essential for preemptive priority scheduling. P1, P2, P3, and P4 have the following priorities: 0, 2, 3, 1.
Sr No. | Process Id | Arrival Time | Burst time |
---|---|---|---|
1 | p1 | 0 | 1 |
2 | p2 | 1 | 7 |
3 | p3 | 2 | 3 |
4 | p4 | 3 | 6 |
- At Time = 0ms
- P1 enters the waiting queue at time=0ms.
- The CPU cycle is allotted to P1 since it is the lone process in the waiting queue until another process arrives.
- At Time = 1ms
- process P1 completes its execution since its burst time has expired, and process P2 enters the waiting queue.
- Because it is the sole process in the waiting line, P2 receives the CPU cycle until another process arrives.
- At Time = 2ms
- The process P3 enters the waiting queue at time=2ms.
- The priority of P2 and P3 are now being compared.
- Priorities 2 and 3 are assigned to the processes.
- As a result, P3 receives the CPU cycle until another process arrives in the waiting queue.
- P2 has been returned to the waiting line.
- At Time = 3ms
- P4 processes arrive in the waiting queue at time =3ms. The priority of P2, P3, and P4 are now compared.
- Their relative priorities are 2, 3, and 1.
- As a result, P3 remains allocated to the CPU cycle until another process arrives or the process quits.
- Processes P2 and P4 are held in the waiting line.
- At Time = 5ms
- P3 has completed its execution at time=5ms, as its burst time has expired.
- The priority is then compared between P2 and P4. Their relative priorities are 2 and 1.
- As a result, P2 receives the CPU cycle till another process comes or it ends.
- P4 has been added to the waiting line.
- At Time = 11ms
- The procedure P2 has completed its execution at time=11ms.
- P4 is assigned the CPU cycle since no other processes have arrived and none are left in the waiting queue.
- At Time = 17ms
- Process P4 is also executed at time=17ms.
Advantages of Preemptive Scheduling
- Preemptive scheduling achieves flexibility by allowing critical processes to access the CPU when they arrive in the ready queue, regardless of whatever task is presently running.
- In this case, the CPU consumption is the same, i.e., all running programs will use the CPU equally. Preemptive scheduling is also considered fair.
- It improves the average response time, as waiting time is less.
- When used in a multiprogramming environment, preemptive scheduling is advantageous.
The Drawbacks of Preemptive Scheduling
-
Processes with lesser priority may starve.
-
The scheduler devotes extra time to suspending the current work, adjusting the context, and dispatching the new incoming task.
-
Scheduling requires high computing resources.
Conclusion :
This article taught you:
- A scheduling discipline is preemptive if the CPU can take away a process after it has been granted.
- Round Robin and Shortest Remaining Time First are some examples of preemptive scheduling.
- The primary advantages of preemptive scheduling over other operating system scheduling approaches include being considered fair, flexibility by allowing critical processes to access the CPU when they arrive in the ready queue, regardless of whatever task is currently running, less waiting time, and the ability to be used in a multiprogramming environment.
- Due to the addition of a priority process in the queue, starvation may occur.