Process management is one of the critical concepts used in operating systems that involves activities related to managing and controlling processes. So before looking into process management and how it works, let's start with the definition of a process. In simple words, a program in execution is called a process. It is an instance of a program that runs, i.e., an entity that can be assigned and executed on a processor. There are two essential elements of a process: program code and data associated with that code.
Process memory is divided into four sections:
Note: Stack and heap sections start at opposite ends of the process free space and grow towards each other. When they meet, a stack overflow error will occur or a call to new memory allocation will fail due to insufficient memory available in the heap section.
A program executing as a process is uniquely determined by various parameters. These parameters are stored in a Process Control Block (PCB) i.e. a data structure which holds the following information:
Now, we understood the process, where we defined one parameter of a process called Process State. Processes in the operating system can be in any of the five states: start, ready, running, wait, and terminated.
Let's understand these states and the transition of a process from one state to another state:
Note: Some operating systems may have other states besides the ones listed here.
The operating system uses a process control block (PCB) to track the execution status of each process.
Process scheduling is critical for selecting and removing running processes based on a particular algorithm. Here one of the key objectives is to keep the CPU busy and deliver "acceptable" response times for all programs. Note: Multiprogramming operating systems allow more than one process to be loaded into the executable memory at a time, and the loaded process shares CPU using time multiplexing.
The operating system has three types of process schedulers:
Long-Term or Job Scheduler: This scheduler's job is to bring new processes to the Ready state. It determines which process is assigned to the CPU for processing, selects processes from the queue, and loads them into memory for execution. The primary objective of the job scheduler is to provide a balanced mix of jobs (such as I/O bound and processor bound) and control the degree of multiprogramming.
Short-Term or CPU Scheduler: It is in charge of selecting one process from the ready state and scheduling it to the running state. They are also known as Dispatchers.
Now our aim would be to understand CPU Scheduling concept and why we need it.
There are two major types of CPU Scheduling:
Now, we will look at the various scheduling algorithms involved in process management one by one.
It is the most basic CPU scheduling algorithm, where a FIFO queue is used to manage the scheduling strategy. The idea is simple: a process that asks first to get the CPU allocation, get access to the CPU first.
PCB of the process is linked to the tail of the queue as it enters the ready queue. As a result, whenever a CPU becomes available, it is assigned to the process at the front of the queue. Some important points:
A simple example: Suppose we have five processes p1, p2, p3, p4, and p5, and the ready queue receives them at times t1, t2, t3, t4, and t5 such that t1 < t2 < t3 < t4 < t5. So p1 arrived first in the ready queue, so it will be executed first, followed by p2, p3, p4, and p5, respectively.
Convoy Effect
In First Come First Serve algorithm, if a process with a large CPU burst time arrives before any small process, then the small process will get blocked by that large process, which is called Convoy Effect.
SJF algorithm is a non-preemptive scheduling algorithm. This policy prioritises waiting process with the shortest execution time. Among all scheduling algorithms, Shortest Job First has the advantage of having the shortest average waiting time. It first creates a pool of processes and then chooses the process with the shortest burst times. After completing the process, it again selects the process with the shortest burst time from the pool. Some important points:
It is further categorized into two types:
A simple example: Suppose we have five processes p1, p2, p3, p4, and p5, and the ready queue receives them at times t1, t2, t3, t4, and t5 such that t1 < t2 < t3 < t4 < t5. Now, you can assume the ready queue as the priority queue, which rearranges the incoming process based on CPU bursts time. Therefore, process with the least CPU burst time is delivered first, and so on.
Longest Job First (LJF) is non-preemptive scheduling. This algorithm keeps track of the burst time of all processes accessible at the moment of arrival and then assigns processor to the process with the longest burst time. In this algorithm, once a process begins to run, it cannot be halted in the middle of its execution.
It organise processes in ascending order of their arrival time. Then, out of all processes that have arrived up to that point, it will choose one with the longest burst time. After that, it will process it throughout the duration of the burst. Until this process completes its execution, LJF monitors if any more processes arrive.
Some important points:
A simple example: Similar to the above example, you can assume here that the same ready queue prioritises based on a larger CPU burst time i.e. out of those five processes, the one with the largest CPU burst time will be executed first.
In Round Robin scheduling each process is cyclically assigned a set time slot. This looks similar to FCFS scheduling, except that it includes preemption, which allows system to transition between processes. In other words, round robin strategy was created with time-sharing systems in mind.
Each process has a set amount of time assigned to it, and once that time period has passed, process is preempted and another process takes its place. As a result, all processes get an equal amount of CPU time. This algorithm performs best in terms of average response time.
Some important points:
A simple example: Suppose there are five processes p1, p2, p3, p4, and p5, with total execution times of t1, t2, t3, t4, and t5. Now, we have one extra factor, t (a time quanta), which will ensure equal CPU time sharing for each process. Suppose the first process p1 arrives, and after t time of execution, p1 will be preempted and wait for the remaining execution of (t1 — t) time. At this stage, p1 will move to the wait state, where it can perform its I/O operation. Now CPU is released for the next process, p2. After completing the I/O operation, process p1 is pushed to the ready queue again for its next processing cycle. The same goes for all the processes.
Priority scheduling is one of the most often used priority scheduling methods. Here priority is assigned to each process, and the highest-priority process will be executed first. The processes of the same priority will be executed based on the first-come first-serve basis. Note: Priority can be determined by memory limitations, time constraints, or other resource constraints.
Some important points:
It is also categorized into two types:
A simple example: Similar to the example of the FCFS algorithm, the processes are inserted in the ready queue but based on a priority now, which can be CPU burst time, memory constraints, etc.
Reference: Operating System Concepts by Abraham Silberschatz, Greg Gagne, and Peter Baer Galvin
Thanks to Vishal Das for his contribution to creating the first version of the content. Please write in the message below if you find anything incorrect, or if you want to share more insight. Enjoy learning, Enjoy algorithms!
Subscribe to get well designed content on data structure and algorithms, machine learning, system design, object orientd programming and math.
©2023 Code Algorithms Pvt. Ltd.
All rights reserved.