What Is the Linux Kernel, and Why Does It Matter?


Key Takeaways

  • An operating system’s kernel manages resources, handles system calls, governs hardware access, and controls processes.
  • The Linux kernel was started as a way to overcome the licensing limitations of MINIX, and to let Linus Torvalds learn to program the 80386 CPU.
  • The Linux kernel, once it was paired with the GNU utilities, produced a working Unix-like operating system. If there was no kernel, there’d be no Linux as we know it.

The Linux kernel is the beating heart of every Linux installation. We look at where it came from, what its purpose is and why, without the kernel, there would be no Linux.

The Story Behind the Linux Kernel

Angered by Unix adopting a commercial license, the GNU Project started to write their own Unix lookalike operating system in 1983.

In 1987, the MINIX operating system was released. This was a bare-bones Unix-like operating system, used as a working example in a computer science textbook. MINIX was licensed for non-commercial use only.

By 1991, the GNU Project had created many of the command-line tools of a Unix-like operating system, but it hadn’t produced a kernel. The kernel is the central controller of an operating system. It sits between the hardware and all other software, managing resources and many other critical tasks.

Linus Torvalds was introduced to MINIX as a computer science student. Annoyed by the MINIX license model, and wanting to learn to program the 80386 CPU in his personal computer, he set about writing his own kernel.

Development took place on MINIX, using the GNU C compiler. By September 1991, Torvalds’ kernel, combined with the GNU core utilities, functioned as a working operating system.

The GNU Project and the Linux Kernel required each other to deliver something functionally meaningful, prompting suggestions that Linux should be referred to as GNU/Linux. Today, Linux distributions contain much more than the GNU tools and the Linux kernel and, whether fair or not, it’s the term “Linux” that has stuck.

Different Kernel Architectures

The Linux kernel is a monolithic kernel. It’s mainly one large program, but it closely interacts with other, distinct, programs such as drivers and kernel modules. The kernel, drivers, and modules all run in kernel space, a dedicated and restricted region of memory that is strictly out-of-bounds to other, regular, processes, which run in user space.

Another approach to kernel design, and one favored by the author of MINIX, Andrew S. Tanenbaum, is the microkernel architecture. A microkernel is a very small kernel running in kernel space, with its supporting processes running in user space. Tannenbaum’s criticism of the design of the Linux kernel triggered the famous Tannebaum-Torvalds debate of 1992.

As a size comparison, the MINIX 3 kernel has about 12,000 lines of code in it. The Linux kernel 6.12.1 has almost 40 million lines.

        find ./linux-6.12.1/ -type f -exec wc -l {} \; | awk '{lines += $1} END {print lines}'
    
Counting the lines of code in the Linux kernel repository.

That’s a rough figure, because it includes everything in the repository, such as licenses, READMEs, and makefiles, not just source code.

A hybrid kernel contains a cherry-picked combination of features from both the monolithic and microkernel architectures. The macOS kernel, XNU, is a hybrid kernel combining features and code from the Mach and FreeBSD kernels.

The Linux Kernel Files

Typically, the kernel is a file called vmlinuz, located in the /boot directory.

The Unix kernel was called “unix.” Following suit, the Linux kernel was called “linux.” The “vm” was appended when support for virtual memory was added, and the “x” was replaced with a “z” when the kernel image was compressed, or gzipped.

Sometimes, the vmlinuz file is a symbolic link to the actual kernel file. This can be useful, because the full filename often contains version and build identifiers that are lost when you rename it to vmlinuz.

        ls -hl
    
Listing the files in the /bopot directory, showing that the vmlinuz file is a symbolic link to the actual kernel image file.

You can also see the kernel version by using the uname command, with the -r (kernel release) option.

        uname -r
ls -hl /boot/vmlinuz
Using the uname command with the -r option to see the kernel version.

Or you can look at the contents of the /proc/version pseudo-file.

        cat /proc/version
    
Using cat with the /proc/version file to discover the kernel version.

What Does the Linux Kernel Do?

The Linux kernel, like all kernels, manages system resources so that processes get a share of CPU time and RAM. It also handles system calls, and controls access to hardware, such as permanent storage devices, graphics cards, and the networking stack.

Resource Management

With finite reserves of RAM and CPU time, it’s impossible to service all the requests of all process for all users, all the time. The kernel has to manage the requests so that all processes have their requests serviced. To help with this, processes are given a priority, with higher priority tasks getting more CPU time than lower priority tasks. You can use the renice command to alter the nice value of a process.

The kernel also has routines to create and terminate processes and tasks, and switching execution between processes and threads.

Input and Output

The Linux kernel provides access to persistent and non-persistent storage. As well as avoiding conflicts and imposing security through permissions, the kernel hides the low-level implementation of the storage device and file systems from processes. The kernel provides a set of system calls that applications use to request actions and responses from the kernel.

System Calls

System calls are the requests processes make to the kernel for actions that only the kernel can conduct. Usually, a process makes a system call through a wrapper function in the language the process has been written in. The wrapper might be a function in a linked library or a runtime environment.

Device Management

The kernel allows programs to interface with a wide variety of devices that can be connected to, or fitted within, a computer. This includes devices such as hard drives, network interfaces, and graphics cards. Linux Kernel Modules (LKMs) are the most common method of implementing these interfaces.

Linux Kernel Modules

Linux kernel modules (LKMs, also called loadable kernel modules) are small programs that the kernel can load at runtime. They’re an ideal way to provide drivers for hardware, interfaces to file systems, and other system features such as security extensions.

To see the LKMs your kernel is using, use the lsmod command.

        lsmod
    
Listing the loaded kernel modules with the lsmod command.

The kernel on this test computer is using 67 modules, which is fairly low. On another machine I checked, the figure was 138.

        lsmod | wc
    
Using wc and lsmod to count the number of kernel modules.

You can get a more detailed view of a single module by using the modinfo command and supplying the name of the module you’re interested in.

        modinfo autofs4
    
Using modinfo to examine the details of a single kernel module.

Why Does the Kernel Matter?

Without a kernel, an operating system simply cannot function. And without the kernel that was started as a student project in Helsinki, way back in 1991, we wouldn’t have Linux. And Linux is used the world over, in many surprising ways.



Source link

Previous articleI can’t believe how cheap these Sony headphones are for Black Friday weekend