How does CPU Scheduling work?

Introduction

By allowing one process to use the CPU while another's execution is put on hold (or in a waiting state) due to the lack of a resource like I/O or another, CPU scheduling maximizes the CPU usage. CPU scheduling seeks to improve the system's effectiveness, speed, and fairness. The operating system must choose one of the processes in the ready queue to be performed whenever the CPU becomes idle. The short-term scheduler conducts the selection procedure (or CPU scheduler). One of the processes in memory that are ready to run is chosen by the scheduler, and that process is given the CPU.

Dispatcher scheduling on the CPU

The Dispatcher is a different element engaged in the CPU scheduling process. The module known as the Dispatcher is what grants the short-term scheduler's chosen process control of the CPU. This activity entails:

  • Switching context
  • Switching to user mode
  • Jumping to the appropriate spot within the user program to resume it from where it left off earlier.

The Dispatcher needs to be as quick as it can be since every time a process switch occurs, it is used. The dispatch latency is the time it takes the Dispatcher to halt one process and start another. The illustration below can be used to explain Dispatch Latency:


CPU scheduling types

The following four situations may result in CPU scheduling decisions:

1) When a process enters the waiting state (for I/O requests or the execution of wait for the termination of one of the child processes) and leaves the running form.

2) The transition of a process from the running state to the ready state (for example, when an interrupt occurs).

3) When a process transitions from the waiting state to the ready state, such as when I/O is finished.

4) When a procedure comes to an end.

It is impossible to choose a schedule in cases 1 and 4. If a new process is available, it must be selected to run from the ready queue. However, in cases 2 and 3, a decision must be made. Scheduling is considered non-preemptive when it only occurs in situations 1 and 4 and preemptive when it occurs in all other cases.

Non-Preemptive Scheduling

When a process is given non-preemptive Scheduling, it retains the CPU until it releases it, either by terminating or moving to the waiting state. The Apple Macintosh and Microsoft Windows 3.1 operating systems employ this scheduling technique. Because it does not need the specialized hardware (such as a timer) required for preemptive Scheduling, it is the sole method that can be utilized on some hardware platforms.

A process executing on a CPU is not interrupted mid-execution while using non-preemptive Scheduling. Instead, it waits until the process has finished using its CPU burst time before allocating the CPU to another method.

Shortest Job First (SJF, essentially non-preemptive) Scheduling and Priority (non-preemptive version) Scheduling are two non-preemptive scheduling algorithms.

Preemptive Scheduling

Priorities are typically allocated to the tasks in this style of Scheduling. Even when a study is already running, there are situations when it must be prioritized ahead of another study. As a result, the ongoing job is briefly halted before being restarted once the priority task is complete.

As a result, this scheduling method is typically utilized when a process transitions from a running state to a ready state or from a waiting state to a ready state. The resources (i.e., CPU cycles) are typically allotted to the process for a brief period before being removed. If the process still has CPU burst time left, the process is then put back in the ready queue. Until it is given a chance to run again, that process waits in the ready line.

Round Robin Scheduling (RR), Shortest Remaining Time First (SRTF), Priority (preemptive version) Scheduling, etc., are a few preemptive scheduling algorithms.

CPU Planning: Scheduling Standard

When evaluating the "best" scheduling algorithm, many distinct factors exist. They include:

1)Utilization of the CPU

The CPU should function most of the time (ideally 100%) to maximize its efficiency and prevent any CPU cycles from being wasted. CPU consumption should range from 40% (lightly loaded) to 90% when considering an actual machine (heavily loaded.)

2)Throughput

The total number of processes finished in a given time, or the total quantity of work accomplished in a given amount of time. Depending on the particular operations, this might be anywhere from 10/seconds to 1/hour.

3) Turnaround Time

It is the time required to complete a specific process or the period from when the procedure is submitted to when it is finished (Wall clock time).

4) Waiting Period

The total amount of time a process has been waiting to take control of the CPU in the ready queue is made up of all the waiting intervals.

5) Average Load

It represents the typical number of processes waiting in the ready queue to enter the CPU.

6) Response Time

The amount of time it takes after a request is made for a response to appear. Remember that the time until the first answer, not the conclusion of the process execution, is what matters (final response).

To optimize properly, other elements are typically minimized while CPU utilization and throughput are increased.