The Linux Kernel – Explained in an Easy to Understand Way

In this article, we’ll go over the Linux Kernel in layman terms. It all began when a young Linus Torvalds, programmed his own Operating System as a hobby.

He allowed other programmers and computer geeks to experiment on the code in order to improve quality and performance. After a few months of modifications, LINUX was launched.

The Linux kernel, as designed by Linus, is the brain of the operating system. The kernel contains thousands of lines of code at first, which eventually reached millions after few versions. Being open-source software, it can be accessed by anyone at any time on the internet.

What is a Linux Kernel?

In simpler terms, Linux Kernel is the bridge of communication between the user applications and the underlying hardware. In general, there are different types of kernels. A Linux kernel is a monolithic kernel, which essentially means that almost the entire work of the Operating System is handled by the kernel itself.

Linux Kernel Monolithic
A Monolithic Kernel

In the above illustration, we can see that a monolithic kernel manages the entire communication between the applications and the hardware. The Linux kernel is written in C programming language, therefore GCC (GNU Compiler Collection) is the default compiler for all kernel source codes.


Where is the Linux Kernel located?

Since the Linux kernel is a piece of code, it must be stored somewhere on the file-system, such that every time the system reboots, the kernel is loaded in the memory.

In Debian/Ubuntu systems, the Linux kernel can be found within the /boot directory. In order to access the Linux kernel, we must know the current Linux kernel version. Using the basic uname -r command, we can access the Linux kernel version.


What does the Linux Kernel exactly do?

There is no shortage of skills and positions of responsibility in the resume of a Linux Kernel. It manages the entire functioning of the Linux system. From scheduling the processes to providing resources to applications, the Linux kernel does it all.

Let us take a quick look on a few fundamental jobs of the Linux kernel:

1. The Linux Kernel is a Memory Manager

The Linux kernel manages memory. The allocation and freeing of physical memory is part of the Kernel’s basic job. There is a three-level page table for storing the mapping of addresses. A page is a basic unit of memory, which typically has a size of 4096 bytes.

Most of the kernel tasks happen through system calls. For instance, let us perform a system call for retrieving page size in the memory.

#include<unistd.h>
#include<stdio.h>

int main(){

	// Performing a system call
	int size = getpagesize();

	// Printing the page size
	printf("%d\n", size);
	return 1;
}

Output:

4096

in the above example getpagesize() is predefined system call.

The memory management unit is responsible for paging and swapping of pages in virtual memory. The virtual memory allocated to the process is completely separate providing a layer of protection from maleficent programs.

Linux commands like htop and atop can be used to monitor the memory related information for every running process.

Linux Kernel Atop Memory
Memory-specific output of atop command

2. The Kernel Manages Processs

Every running program in the system is a process. The kernel manages the creation, running, and deletion of every process. System calls like fork() and exec() are powerful tools in multi-processing environments.

Every process in the system requires CPU for its execution. Process Scheduler is an integral part of process management. There are multiple kinds of scheduling algorithms that can be implemented.

There is yet another responsibility of the Linux kernel, which is IPC (Inter-Process Communication). The mechanisms like pipes and semaphores are coordinated by the kernel with the aim of communication between processes.

In order to take a quick peek at all running processes with their properties like scheduling priorities and percentage of CPU occupied, we can run htop command in the terminal.

Linux Kernel Process Htop
Process-related output of htop command.

For more information regarding the Linux Kernel, we can refer the book by David A. Rustling.


Installing a specific Linux kernel

It may sound shady, but the user can replace their kernel with a new or an old version. The steps to implement this task are too elaborate to cover entirely in this article. Therefore, we will provide a short guide to install a specific kernel in our system.

The first job is to download the kernel file. The Linux kernel files can be downloaded from the official site. After the download is complete, the files must be extracted and configured.

The configuration of files is done via the make command. By running make menuconfig, a window appears that can be used to select the modules and features for our new kernel. The shortcut for this overwhelming process is to copy the current configuration file from the running kernel to the extracted directory.

cp /boot/config-$(uname -r) .config

Note: Make sure the permissions for running the command are elevated to root access. There are certain commands that might not work under user-level access. We can use sudo -s or sudo su for achieving the task.

The above command basically copies the config file present in the boot directory of the running kernel as a new config file named .config.

The next step is installing certain tools and compilers for compiling the kernel files. Debian/Ubuntu users can do so by:

sudo apt install build-essential libncurses-dev bison flex libssl-dev libelf-dev

The kernel image is compiled using the make command within the directory containing new kernel files. The compilation of kernel takes a bunch of time, therefore no need to press any panic button.

After the compilation is complete, we need to install the modules and the kernel image. This can be achieved in a single command by:

make install_modules && make install

The first sub-command installs the kernel modules and the next one installs the complete kernel along with creating images and other important files within the /boot directory.

The last step is to switch the current kernel version to the new installed one. To do so, we run:

sudo update-initramfs -c -k <NEW_VERSION_NUMBER>

The version number must be similar to the name of the newly installed image file. This followed by the update-grub command to finalize the current kernel version.

All the changes have been done. In order to restart the system, run reboot in the terminal.


Conclusion

The Linux kernel is the powerhouse of the Linux operating system. Understanding its concepts takes us closer to an understanding of Linux. We hope this article was easy to follow. Feel free to comment below for any queries or feedback.


References

The Linux Kernel – by David A. Gosling

The Linux Kernel – by Andries Brouwer