In this blog, we will learn about process management in the Operating System and various Algorithms and interesting facts related to it. Before actually looking into process management and how it works, let's start with the definition and various aspects of a process.
Most simply, a program in execution is called a process. In other words, it is an instance of a program that actually runs, i.e., an entity that can be assigned and executed on a processor. Two essential elements of a process are program code and a set of data associated with that code.
Process memory is divided into four sections:
Note: stack and the heap start at opposite ends of the process's free space and grow towards each other. A stack overflow error will occur if they meet, or a call to new memory allocation will fail due to insufficient memory available in a heap section.
A program executing as a process is uniquely determined by various parameters. These parameters are stored in a Process Control Block (PCB) for each process. It is a data structure, which stores the following information:
Now, we understood the process, where we defined one parameter of a process called 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 transition from one state to another state:
Note: Some systems may have other states besides the ones listed here. Explore and think!
The operating system executes various activities in creating a process, which uses a process control block (PCB) to track the execution status of each process.
Process scheduling is critical for selecting and removing the running process based on a particular strategy or algorithm. The main objectives of the process scheduling are to keep the CPU busy and deliver "acceptable" response times for all programs. Multiprogramming operating systems allow more than one process to be loaded into the executable memory at a time, and the loaded process shares the CPU using time multiplexing.
The operating system has three types of process schedulers for process scheduling.
Long Term or job scheduler: This scheduler's job is to bring the new process 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.
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.
Medium-term scheduler: It is in charge of swapping the processes when a particular process is performing an I/O operation. If a running process makes an I/O request, it may be suspended. A process that has been suspended cannot make any progress toward completion. The suspended process is transferred to secondary storage in this situation to remove it from memory and make room for other processes. This is known as switching, and the procedure is referred to as being switched out or rolled out.
Now our next aim would be to understand the concept of CPU Scheduling and why do we need it.
I/O and CPU time are both used in a typical procedure. Time spent waiting for I/O in an old operating system like MS-DOS is wasted, and the CPU is free during this time. One process can use the CPU while another waits for I/O in multiprogramming operating systems. Only process scheduling allows for this.
CPU Scheduling is the process of determining which process will have exclusive use of the CPU while another is paused. The basic goal of CPU scheduling is to ensure that whenever the CPU is idle, the OS chooses at least one of the programs in the ready queue to run. The CPU scheduler will be in charge of the selection process. It chooses from among the processes in memory that are ready to run.
There are two major Kinds Of CPU Scheduling:
Now, we will look at the overview of the various scheduling algorithms involved in Process Management one by one. We will cover each algorithm separately in different blogs.
FCFS stands for “First Come, First Serve.” It is the most basic and straightforward CPU scheduling algorithm. The process that asks the CPU to get the CPU allocation first in this type of method. A FIFO queue can be used to manage this scheduling strategy. The PCB (Process Control Block) 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 should be assigned to the process at the front of the queue.
Some Important Points of this method:
A simple example of this Algorithm:
Suppose we have 5 processes p1, p2, p3, p4, p5, and they are received by the ready queue at time t1, t2, t3, t4, t5 such that t1 < t2 < t3 < t4 < t5. Hence p1 arrived first in the ready queue and therefore it will be executed first followed by p2, p3, p4, p5 respectively.
Convoy Effect
In the FCFS (First Come First Serve) type of algorithm if a certain effect with a large CPU burst time arrived before any small process then the small process will get blocked by that large process and this effect is called Convoy Effect.
The SJF algorithm is a non-preemptive one. It’s a scheduling policy that prioritizes the 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 sorts all of the processes by arrival time. Then choose the method with the shortest arrival time and the shortest burst time. After completing the process, create a pool of processes that will run until the preceding process is completed, then choose the process with the shortest Burst time from the pool.
Some Important Points of this method:
A simple example of this Algorithm:
Suppose we have 5 processes p1, p2, p3, p4, p5, and they are received by the ready queue at time t1, t2, t3, t4, t5 such that t1 < t2 < t3 < t4 < t5. Now, you can assume this time the ready queue as the priority queue which rearranges the incoming process on the basis of CPU bursts time. Therefore, the process with the least CPU burst time is delivered first, and so on.
The algorithm Longest Job First (LJF) is a type of non-preemptive scheduling. This algorithm primarily keeps track of the Burst time of all processes accessible at the moment of arrival, and then assigns the 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. Only until the allocated process has completed its processing and been terminated may any other process be executed. It organizes the processes in ascending order of their Arrival Time. Then, out of all the processes that have arrived up to that point, it will choose the one with the longest Burst Time. After that, it will process it throughout the duration of the burst. Until this process completes its execution, the LJF monitors if any more processes arrive.
Some Important Points of this method:
A simple example of this Algorithm:
Similar to the above example but you can assume here that the same ready queue prioritizes on the basis of a larger CPU first time i.e. out of those five processes the one with the largest CPU burst time will be executed first and so on.
Round Robin is a CPU scheduling system in which each process is cyclically assigned a set time slot. The Round Robin algorithm was created with time-sharing systems in mind. This algorithm is similar to FCFS scheduling, except that it includes preemption, which allows the system to transition between processes. Each process has a set amount of time assigned to it, and once that time period has passed, the process is preempted and another process takes its place. As a result, all processes get an equal amount of CPU time. This algorithm performs the best in terms of average response time.
Some Important Points of this method:
A simple example of this Algorithm:
Again suppose we have 5 processes p1, p2, p3, p4, p5, and let them have total execution time t1, t2, t3, t4, t5. Now, we have one extra factor t`(a time quanta) which is going to ensure the equal sharing of CPU time for each process. Suppose the first state arrives and after t` time this p1 process is been executed for (t1 — t`) time. Now, it moves to the wait state where it can perform its I/O operation but now the main memory is released for the next process p2. After completing its I/O operation the process p1 is pushed to the ready queue again for its next cycle of processing. The data of the p1 process for its execution up till (t1 — t`) is already saved by the CPU so that it can continue from that state in the next cycle. The same goes for all the processes.
Priority scheduling is one of the most often used batch scheduling methods. A priority is assigned to each process. The highest-priority process is carried out first, and so on. On a first-come, first-served basis, processes of the same priority are executed. Prioritization can be determined by memory limitations, time constraints, or any other resource constraint. Priority scheduling does not always set the priority as the inverse of the CPU burst time and memory; instead, it can be set internally or externally, but the scheduling is done on the basis of process priority, with the most urgent processes being processed first, followed by those with lower priority in order.
Some Important Points of this method:
A simple example of this Algorithm:
Similar to the example of the FCFS algorithm the processes are inserted in the ready queue but here on the basis of a priority now which can be CPU burst time, memory constraints, etc, and then its execution follows similar to the FCFS algorithm.
Reference: Operating System Concepts by Abraham Silberschatz, Greg Gagne, and Peter Baer Galvin
Enjoy learning! Enjoy Algorithms!
System design is one of the critical topics that large tech companies ask about during the interview. On the other hand, it is also necessary for solving large-scale software problems. This blog will help you get familiar with concepts important for solving system design questions and learning system design at an advanced level.
Load balancers can receive and distribute requests to a particular server based on various load balancing techniques. These techniques use different algorithms to select servers based on a specific configuration. These algorithms are categorized into two parts: Static load balancing, 2) Dynamic load balancing
Whatsapp is a social messenger platform, which allows users to send messages to each other. It is a messaging system that is widely used throughout the globe. Here in this blog, we’ll be discussing WhatsApp’s generic architecture and which could also be used as a base for designing any such chat application. So let’s get started by discussing the key requirements of our service.
In this blog, we will learn about Caching, an important fundamental concept in system design. Have you ever experienced that when you open a website the first time, it takes more time than usual, but when you open the same website again, it takes no time to load? Do you know why this happens? Let's find out!
Subscribe to get free weekly content on data structure and algorithms, machine learning, system design, oops design and mathematics.