Everything about linux kernel

What is the Linux kernel?

The Linux® kernel is the main component of a Linux operating system (OS) and is the core interface between a computer’s hardware and its processes. It communicates between the 2, managing resources as efficiently as possible.

The kernel is so named because—like a seed inside a hard shell—it exists within the OS and controls all the major functions of the hardware, whether it’s a phone, laptop, server, or any other kind of computer.

What the kernel does

  1. Memory management: Keep track of how much memory is used to store what, and where
  2. Process management: Determine which processes can use the central processing unit (CPU), when, and for how long
  3. Device drivers: Act as mediator/interpreter between the hardware and processes
  4. System calls and security: Receive requests for service from the processes

The kernel, if implemented properly, is invisible to the user, working in its own little world known as kernel space, where it allocates memory and keeps track of where everything is stored. What the user sees—like web browsers and files—are known as the user space. These applications interact with the kernel through a system call interface (SCI).

Think about it like this: The kernel is a busy personal assistant for a powerful executive (the hardware). It’s the assistant’s job to relay messages and requests (processes) from employees and the public (users) to the executive, to remember what is stored where (memory), and to determine who has access to the executive at any given time and for how long.

Where the kernel fits within the OS

To put the kernel in context, you can think of a Linux machine as having 3 layers:

  1. The hardware: The physical machine—the bottom or base of the system, made up of memory (RAM) and the processor or central processing unit (CPU), as well as input/output (I/O) devices such as storage, networking, and graphics. The CPU performs computations and reads from, and writes to, memory.
  2. The Linux kernel: The core of the OS. (See? It’s right in the middle.) It’s software residing in memory that tells the CPU what to do.
  3. User processes: These are the running programs that the kernel manages. User processes are what collectively make up user space. User processes are also known as just processes. The kernel also allows these processes and servers to communicate with each other (known as inter-process communication, or IPC).
Читайте также:  Installing microsoft fonts on linux

Code executed by the system runs on CPUs in 1 of 2 modes: kernel mode or user mode. Code running in the kernel mode has unrestricted access to the hardware, while user mode restricts access to the CPU and memory to the SCI. A similar separation exists for memory (kernel space and user space). These 2 small details form the base for some complicated operations like privilege separation for security, building containers, and virtual machines.

This also means that if a process fails in user mode, the damage is limited and can be recovered by the kernel. However, because of its access to memory and the processor, a kernel process crash can crash the entire system. Since there are safeguards in place and permissions required to cross boundaries, user process crashes usually can’t cause too many problems.
Also, because the Linux kernel can continue to work during live patching, there’s no downtime while patches are applied for security fixes.

Источник

The Linux Kernel – Explained in an Easy to Understand Way

Linux Kernel Featured Image

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

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.

Читайте также:  Mount iso linux centos

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.

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

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

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.

Читайте также:  Linux and windows on one disk

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:

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.

Источник

Оцените статью
Adblock
detector