Unveiling the Dynamics of CPU Scheduling Algorithms in Operating Systems with a Bash Scripting Dive

Exploring the Core Strategies: Round Robin vs. Shortest Job First (SJF) — A Comprehensive Analysis with a Video Demo of the emulation of Round Robin in Bash Scripting

saed
4 min readJan 14, 2024

CPU scheduling is imperative in achieving the operating systems’ (OS) design objectives. Developing and understanding scheduling algorithms have proven to be time constrictive and challenging due to the necessity of constantly adjusting and assessing the OS kernel code. The objective of this study is to present the distinction amongst the two algorithms used in modern computer systems with optional access to custom code that illustrate one of the algorithms written in bash scripting.

Introduction

The Round Robin Algorithm (RR) is one of the older and most common algorithms used in CPU scheduling. The processes are ordered in an orbitual fashion. The CPU allots a unit of time to each process which is called the time quantum. The CPU picks the process that is foremost in the catalogue of jobs, it sets a metre to disturb after one quantum and transmits the process. RR is pre-emptive, this means once a process has run its first unit and the job is not complete, it will return to that process as it is placed back in the queue of jobs with its contents to continue where the schedular had left off. This utilizes the CPU more efficiently in comparison to non-pre-emptive scheduling. All new processes are then added to the end of the log.

The Shortest Job First (SJF) algorithm executes and completes tasks in the order of those with the shortest execution times. This scheduling algorithm is not pre-emptive, which means that, unlike the Round Robin Algorithm, when it starts a job on a process, it cannot pause the process and pick up where it left off; rather, it must complete the job before starting another one. This means this scheduling has no such overhead of having to switch from process to process in the ready state.

The Round Robin Algorithm prioritizes jobs in order of the time they arrived, the utilized process is the one with the highest precedence. This is crucial since it gives RR the elasticity to handle the more urgent assignments on an order-by-order basis. This ensures that the CPU is distributed evenly among all processes for all jobs. However, this leaves the algorithm with the overhead of having to constantly change the running process in turn lengthening the average time the scheduler completes a job. Whereas SJF has no such overhead.

Moreover, the CPU is assigned to the and cannot be paused in the middle . This ensures that the job has been executed and finished before allocating the CPU to another process. Additionally, SJF offers less complexity as the CPU simply runs one process at a time which is far less computational reserves in comparison to the RR schedular.

Shortest Job First is commonly used for long-term scheduling. The runtime is known in advance, consequently making it easier for bulk-type dispensation of processes. However, this also proves to be a limitation as it is difficult to implement this algorithm for scheduling short-term due to long turnarounds of processes, thus leading to process starvation.

The Round Robin Algorithm offers a larger waiting time and response time, this leads to low throughput and can be very time consuming especially if there is a small-time quantum as well as outputting a very large Gantt chart . The Shortest Job First has the best average turnaround time in contrast to the Round Robin, however the RR offers a solution in light to the fairness of the CPU optimisation providing better average response time.

In conclusion the two scheduling algorithms have their use cases for example, SJF is more frequently used for long-term scheduling as well as batch-type processing where the arrival times are known in advance. Whereas RR does cuts off the prospect of process starving with its equal distribution of CPU allocation.

Design: Architecture of Code

Live Demo of the code in Bash:

https://www.youtube.com/watch?v=hFk9Kp-oZWU&t=22s

--

--

saed
saed

Written by saed

Senior Security Engineer @ Google

Responses (1)