Linux Processes & Scheduling Algorithm

In this article, we will look at what are Linux Processes & Scheduling Algorithm. We will go through Linux processes lists and a few Linux Processes commands to get a good grasp on it.  

We will also look at a few Linux Scheduling Algorithm examples along with the Bash Script coding implementation. We would also look at a few Linux scheduling tasks. There is a lot to cover, so let’s begin. 

Prerequisite

Table of Content

Contents

Linux Processes & Scheduling Algorithm

What are Linux Processes? 

When a command is given by a user, a task is created which needs to be executed. For that, the OS has to assign resources to that task. Those resources would then be utilized in order to finish the execution of that task. These tasks are called processes or process singular. 

When a command is executed, it creates a new process, identified by its unique process id called PID.  

We could all list Linux processes or could filter out only those Linux processes running in active state. We could even send these Linux processes to background execution so that we could simultaneously work on a different task.  

Foreground and Background Processes 

  1. Foreground Process: Any command executed will start a process until finished and run in the foreground by default.

It will be able to take in input from the user and display output to the screen. Like how it’s done generally. Example:  

				
					pwd
				
			
  1. Background Process: It will also perform the exact same task in the exact same way as it does in the foreground process. Except, it will do so in the background. (No duh)

Meaning it would not be interactive and you would be able to perform other tasks meanwhile the process is under execution in the background. Example: 

				
					pwd & 
				
			

The & sign would make any process work in the background. 

Let’s have a practical look at these Linux processes commands. 

Processes 

1. View all Linux processes list:

				
					ps 

ps -f 
				
			

2. View a single Linux process: 

				
					# ps [PID] 

ps 20 

ps -fp 2226 1154 1146
				
			

PS Options 

1. Linux processes list which is active

				
					ps -A 

ps -e 

ps -eF 
				
			

-a: Shows information about all users

-x: Shows information about processes without terminals

-u: Shows additional information like -f option

-e: Displays extended information

2. Processes that are owned by a certain user

				
					ps -x 

ps -fU myUser 

ps -fU myUser 501
				
			

-x: Shows process information about the current user

-U: Shows information about the processes of the user mentioned in the command

3. To view Linux processes with admin privileges. 

				
					ps -U root 
				
			

4. Processes which are owned by a certain group

				
					#ps –fG [groupname] 

ps –fG Students 
				
			

Attributes of a Process 

  1. UID: User ID that this process belongs to (the person/user/account running it)
  2. PID: Process ID
  3. PPID: Parent process ID (the ID of the process that started it)
  4. C: CPU utilization of process
  5. STIME: Process start time
  6. TTY: Terminal type associated with the process
  7. TIME: CPU time is taken by the process
  8. CMD: The command that started this process

Termination of a Process 

To terminate a specific process, execute the following command: 

				
					ps -f  

kill 21 

kill 2226 1154 1146
				
			

Linux Processes & Scheduling Algorithm

What is CPU Scheduling in Linux? 

CPU Scheduling is a procedure where a certain task is assigned some resources to get executed.

All the tasks coming in are sent to a queue, where they would wait for their turn until all the previous tasks prior to it get executed. These tasks are called a process.  

The CPU Scheduling’s job is to make sure that the CPU does not sit idle. And that it should be executing the next process from the queue. It’s the CPU Scheduler who decides which task/process would go next to be executed.  

Why Scheduling is Important?

There are a few factors that the CPU Scheduler considers before deciding which process to send next for execution.  

  1. Maximize throughput (number of processes executed per unit of time) 
  2. Minimize wait time (the time each process must wait in the queue until its turn to execute) 
  3. Minimize response time (the time taken to fully execute a process) 

Based on the above parameters, Linux Scheduler improves performance and response time for all the tasks. Which is essential to improve user experience. Our goal is to maximize throughput but minimize wait and response time. You can read more here

Two Types of Linux Schedulers  

1. Preemptive Scheduler 

If a scheduler is preemptive, then it could decide that a certain process had enough CPU resources assigned to it and could revoke it back. Only to give it to another process instead.  

It’s done only to improve the overall performance. This decision of Linux Scheduling is based on the factors mentioned earlier. 

2. Non-Preemptive Scheduler 

If a scheduler is non-preemptive, then it does not follow the same rules as the previous scheduler type. It would only revoke a process resource once execution is finished, or the process is waiting for an IO operation response.  

Scheduling Algorithm

Types of Scheduling Algorithm in Linux  

There are quite a lot of Process Scheduling Algorithms in Linux. We will be focusing on mainly these 3. 

  1. First Come First Served (FCFS) 
  2. Shortest Job First (SJF) 
  3. Round Robin (RR) 

Scheduling Algorithm: First Come First Serve (FCFS)  

Scheduling Algorithm FCFS is probably the easiest to understand. Linux processes are executed sequentially. A process only starts to execute, once the previous process has finished. 

Pros 

  • It is a non-preemptive scheduling algorithm. 
  • It is easy to implement and use. FIFO wait queue. 

Cons 

  • Poor performance as the wait time is high. 
  • Shorter tasks might have to wait for a long time. 

Scheduling Algorithm: Shortest Job First (SJF)   

Unlike the Scheduling Algorithm FCFS, the Shortest Job First (SJF) executes those processes first and takes the least time to finish execution. Regardless of which process was in the queue first. 

Pros 

  • Done in batch environments. 
  • Shorter tasks do not have to wait long. 

Cons 

  • Not preferred in shared systems. Because the length of execution would be unknown beforehand. 

Scheduling Algorithm: Round Robin (RR)    

In Round Robin Scheduling Algorithm, each process is assigned a certain time period, also known as quantum, in which they would get executed. After the period ends, the next process would start executing for their allotted time.  

For instance, let’s take 3 processes A, B, and C. They were all assigned a time period by the scheduler. The first process A would start getting executed. Regardless of whether it has finished or not, once its quantum ends, the process is preempted, and process B would begin the execution.  

Once all processes’ time period ends, process A would start again. 

Pros 

  • All processes get an equal share of the CPU. 
  • It is generally used in multi-tasking scenarios. 

Cons 

  • If the quantum is too long, then the RR algorithm essentially acts like FCFS. 
  • If the quantum is too short, it will lower the efficiency of the CPU. 

Linux Scheduling Implementation

Shortest Job First (SJF)   

1. Initialize arrays for Processes and Burst Time as: 

				
					#!/bin/bash 

read –a Processes 

read –a BurstTime 
				
			

2. Initialize the count 

				
					let length=${#Processes[@]} 
				
			

3. Setup loops 

				
					for (( i=0;i<=$length;i++ )) 
do 
    for (( j=((i+1));j<=$length;j++ )) 
    do 
    
    done 
done 
				
			

4. Swapping 

				
					if [[ ${BurstTime[i]}  gt ${BurstTime[j]} ]] 
then 
    temp=${BurstTime[i]} 
    BurstTime[i]=${array[j]} 
    BurstTime[j]=$temp 
fi
				
			

Linux Scheduling Task

  1. Calculate batch job process sequence for provided data 
  2. Print Job sequence with Job name and Burst Time 

 

Processes 

Burst Time 

firefox 

2 

nano 

8 

gedit 

4 

mail 

1 

ftp 

3 

http 

7 

 

And that’s a wrap! 

These commands will only work on a LINUX terminal. And a common way to run Linux with Windows is to start a Virtual Machine using VMware. 

I hope this article helped you learn exactly what are Linux Processes & Scheduling Algorithm. You may also want to learn Basic Linux commands hands on or Linux Bash Scripting. Please like this article and leave your reviews in the comment section below. 

Have a great one!