Process scheduling organizes the processes to maximize CPU utilization. Process scheduling supports multiprogramming. Multiple processes are loaded to the main memory of the CPU, and the processes are switched so frequently that the CPU is busy executing one process at a time while the other is in a waiting for the state.
Process scheduling helps in determining which process should be allotted to the CPU and which should be put on hold. It takes care that no process is starving and no process is engaging the CPU for a long time, keeping the other process on hold.
Table of Contents
Process Scheduling Queues
Every process in the main memory is organized in the queue, which helps to classify which processes are ready for execution which are in waiting state. Whenever a process gets created, it is placed in the job queue. The job queue is one where all the processes in the system stay till they get ready for execution.
When a process is ready for execution, it is brought to the main memory and placed in the ready queue. During the time the CPU gets free, the first process in the ready queue is allotted to the CPU for execution.
When a process executing in CPU quits for the reasons like waiting for the occurrence of an event (I/O request to complete). Processes waiting for sharing of I/O devices are placed in the device queue.
Process scheduling would be easier to understand with a standard representation of queuing diagram. You can observe two scheduling queues in the diagram ready queue and the device queue.
Resources serving the queues are represented with circles, whereas arrows represent how a process navigates in the system. Now let’s interrogate the diagram.
When a process enters main memory for execution, it is placed in the ready queue till the CPU can be allotted to it or it is dispatched.
Now, once CPU is allotted to the process, there are a series of events that can occur in the process we will discuss them one by one.
- If a process requests for an I/O device, then it would be placed in the I/O queue or a device queue.
- In case the process had created a child process, then it has to wait till the child executes completely.
- In case the process is interrupted due to any reason, it will be placed back in the ready queue.
When a process gets executed completely and is terminated, it is removed from the queuing sequence else it switches between the ready queue and the waiting queue.
Types of Process Scheduling
There are two types of scheduling:
- Pre-emptive Scheduling: Pre-emptive scheduling of a process allows the system to pre-empt the process in between its execution and respond to another process.
- Non-Preemptive Scheduling: This kind of schedule does not allow the system to pre-empt the process in between its execution.
Types of Scheduler
After the classification of process, and scheduling, let’s move on to the types of process schedulers. There are three types of scheduling, as discussed below:
- Long-Term Scheduler:
As we have seen above, whenever a process is created, it is placed in the job queue. Now, it is the task of the long-term schedular to determine which process from the job queue will be placed into the main memory.
Alternatively, we call the medium-term scheduler – a job scheduler, as it adds the processes from the job queue to the main memory. Job scheduler has to perform less frequently as it at least requires a minute to create a new process.
- Short-Term Scheduler:
Now, when the processes are in the main memory waiting for CPU allocation, it is the task of the short-term schedular to select the process from the main memory and assign CPU to it for its execution.
Alternatively, we call the medium-term scheduler – the CPU scheduler. This is because it allocates CPU to the processes ready for execution. Short-term schedular has to perform frequently as it has to keep the CPU busy.
- Medium-Term Scheduler:
When the process is in execution, it may be interrupted, or it may wait for an event to occur. The suspended or waiting processes occupy memory space, so they are swapped to the secondary memory, and in favourable conditions, it is again placed in the main memory.
This swapping of processes is conducted by the medium-term schedular.
As we know, whenever the interrupt occurs, the CPU has to suspend its current process and run the kernel routine. Further, when its processing is complete, it resumes the suspended process. This scenario is context switching.
When the CPU suspends its current process, it stores the context of the process in the PCB. It stores information such as process current state, CPU registers values memory management information.
This information helps the CPU identify at what stage it has left the process when it resumes back the suspended process. Context switching creates overhead for the system as it is not performing any useful work at that moment. The switching speed varies from system to system depending on their memory speed.
So, in this article, we have studied process scheduling, its classification and its type. We have also covered the context-switching concept in process scheduling.