Kernel
The Linux kernel is basically the brain of a Linux-based operating systems, creating a bridge between the computer's hardware and its software processes. Think of the kernel as the core program that takes care of all the heavy lifting needed for a computer to function.
Kernels are essentially the backbone of any operating system. While modern operating systems come packed with a bunch of extra features like tools for managing logs and user accounts, it's the kernel that handles the fundamental job of meshing software with hardware.
There are countless kernels out there, each serving as the heart for different operating systems. Regardless of their differences, all kernels share the common goal of ensuring the smooth operation of an operating system.
Linux Kernel VS Linux OS
When people talk about "Linux," they're often referring to operating systems that are built around the Linux kernel. However, strictly speaking, Linux itself is just the kernel. All the other software, like the interfaces you interact with and the apps you use, isn't part of the Linux kernel but comes from various other projects.
That's why you might hear the term "GNU/Linux" used, especially when talking about operating systems like Ubuntu or Red Hat Enterprise Linux. This term acknowledges both the Linux kernel and the significant contribution of the GNU project, which supplies a lot of the software used alongside the Linux kernel to make a fully functional operating system. But, it's common to just say "Linux" when referring to the whole operating system package.
They do this because many (though not all 🤓) of the programs that developers combine with the Linux kernel to create a complete Linux-based OS are developed by the GNU project, which creates a wide range of open source software.
Note
Linux is not developed by GNU, and so to give credit to the work of GNU developers in addition to the Linux kernel developers, people use the phrase GNU/Linux.
The bottom line here is that the term Linux can be used to refer specifically to the kernel or to any operating system that uses the Linux kernel in addition to programs from other sources in order to provide a complete operating system.
Linux VS Unix
You may also sometimes hear people use the term Linux somewhat interchangeably with Unix 😅
This is because the Linux kernel is designed to operate in the same way as the kernel for Unix, an operating system developed between 1969 and the mid-1990s.
Because the Unix kernel was only compatible with certain types of computer hardware and was subject to licensing restrictions, a number of programmers developed their own kernels to serve as alternatives to the Unix kernel. Linux is one example of these alternatives.
Note
Other popular examples include the kernels used by FreeBSD, NetBSD, and even macOS.
To be clear, Linux does not use any source code from Unix. It’s simply designed to work similarly to the Unix kernel. For that reason, Linux is often described as a Unix-like kernel. Linux can be used to run systems that are like Unix, but Linux is not Unix.
Kernel Architecture
Like most modern kernels, the Linux kernel uses what’s known as a monolithic kernel architecture. This means that the kernel runs as a single program.
An alternative to Linux’s kernel architecture is the microkernel architecture, which breaks the kernel into smaller, discrete programs. Microkernels were a popular concept in the 1980s and 1990s, but many attempts to implement them were not as successful as hoped. Like many tech discoveries is was the lack of hardware that was the main issue ⚙️
Operating systems like macOS have adopted some microkernel features (although their kernel architectures are also partly monolithic, which is why macOS is usually described as having a hybrid kernel architecture 😇), but Linux has retained a monolithic architectural style throughout its history.
But what it is look like,?If you see this schema on wikipedia, ok first it can be not very friendly 😅
Focus Linux's Kernel Architecture
Talking about the Linux kernel is like diving into the brains behind your Linux OS, showing you how it chats with your computer's hardware. It's like the ultimate manager at a restaurant, making sure everything from seating guests (processes) to cooking meals (memory management) runs smoothly 👩🍳
The Linux kernel is pretty much a one-stop-shop, packing all the essential services it needs right into the kernel itself. This is different from those fancy microkernels that keep things minimal and plug in extras as needed like raspberry Pi or some arduino for the mechanics guy out there 🧰.
Whether you're on Team Monolithic or Team Microkernel, there's a lot to say about both.
As time's gone by, the Linux kernel has become a lean, mean computing machine, known for not hogging memory or CPU and being as stable as a table. But the coolest thing? It's like a chameleon, able to run on a massive range of gadgets, from powerful servers to tiny devices that don't even have a memory management unit (MMU). Thanks to a special version called uClinux, it can even run on gadgets without MMU support.
Looking inside the Kernel's Toolbox
Imagine the Linux kernel as a toolbox, each tool (what we have seen on the first schema) where build to tackle a specific job 🧰
- System Call Interface (SCI): This is the front desk where user space chats with the kernel. Depending on your computer's architecture, this desk looks different. You'll find the SCI's workings in the kernel's source code, specifically in the
./linux/kernel
and architecture-specific bits in./linux/arch
. - Process Management: Here, the kernel is like a multitasking wizard, juggling processes (or threads) so your system runs smoothly. It's all about managing who gets the CPU's attention, using a super efficient scheduler that doesn't care if it's handling one thread or a thousand. Dive into
./linux/kernel
and./linux/arch
to see how it's done. - Memory Management: The kernel's also a memory maestro, managing RAM in chunks called pages. It's got tricks up its sleeve, like the slab allocator, to make memory allocation smarter. When the system's out of memory, it starts swapping pages to disk. Check out
./linux/mm
for the lowdown. - Virtual File System (VFS): Think of VFS as your file system's translator, making sure your files play nice with different storage formats. It sits between your apps and the physical file systems, offering a common set of file operations. You can find the details in
./linux/fs
. - Network Stack: This is the kernel's internet café, where data packets hang out before hitting the web. It's set up just like the internet protocols, with layers for IP, TCP, and the user-friendly sockets layer. For a closer look, head to
./linux/net
. - Device Drivers: Most of the kernel's code is in the device drivers, turning your hardware's babble into something the kernel understands. These drivers cover everything from Bluetooth to USB. Find them in
./linux/drivers
. - Architecture-Dependent Code: Not all of Linux can ignore the hardware it's running on. Some parts need to be tailored for specific architectures, found in
./linux/arch
. This is where the kernel gets cozy with the actual chips and circuits of your device.
User mode VS Kernel mode
In Linux, like in many operating systems, there are two distinct modes of operation for the software running on the system: user mode and kernel mode. Understanding the difference between these two modes is crucial for grasping how Linux manages the security and stability of the system.
Kernel Mode
In Kernel mode, the executing code has complete and unrestricted access to the underlying hardware. It can execute any CPU instruction and reference any memory address. Kernel mode is generally reserved for the lowest-level, most trusted functions of the operating system. Crashes in kernel mode are catastrophic; they will halt the entire PC.
- Privileged Access: Kernel mode is a privileged mode of operation where the code has unrestricted access to all system resources, including the hardware and critical system data. This mode is reserved for the most trusted functions of the operating system.
- Performance and Control: Operating in kernel mode allows the OS to perform efficiently and manage hardware interactions directly, such as executing CPU instructions that access I/O devices, managing memory, and handling interrupts.
- Risk: Because kernel mode code has access to critical system resources, errors or bugs in kernel code can lead to system crashes or vulnerabilities. This is why kernel code must be stable, secure, and carefully managed.
User Mode
In User mode, the executing code has no ability to directly access hardware or reference memory. Code running in user mode must delegate to system APIs to access hardware or memory. Due to the protection afforded by this sort of isolation, crashes in user mode are always recoverable. Most of the code running on your computer will execute in user mode.
-
Restricted Access: User mode is a non-privileged mode where applications and processes run. In user mode, software does not have direct access to hardware or critical system resources. Instead, applications must communicate with the kernel (which operates in kernel mode) to request these resources, using a controlled interface such as system calls.
-
Safety and Security: This separation helps ensure system stability and security. If a user-mode application crashes or encounters an error, it does not directly compromise the integrity of the entire system. Similarly, malicious code running in user mode is limited in the harm it can do.
-
Switching Between Modes: When a user-mode application needs to perform an operation that requires kernel-level access (e.g., reading a file from disk, opening a network socket), it makes a system call. The CPU then switches to kernel mode to execute the necessary operations on behalf of the application. Once complete, control is handed back to the application in user mode.
Kernel program example
While you don't directly interact with user mode and kernel mode via commands or code in the same way you might execute a regular program, understanding how system calls work can give you insight into the interaction between user mode and kernel mode. Let's write a simple hello worl c program here 🤓
#include <unistd.h>
#include <sys/syscall.h>
int main() {
// Using the write system call to print "Hello, World!" to stdout
syscall(SYS_write, 1, "Hello, World!\n", 14);
return 0;
}
More details about kernel here : Understanding User and Kernel Mode
Monitoring system performance and resources wirh CLI
Understanding your system's performance and resource usage is crucial for optimization and troubleshooting. The Linux kernel provides various tools to monitor these aspects.
Some system performance commands
-
top
: Displays an ongoing view of system processes and resource usage, including CPU and memory. Great for a quick overview. -
vmstat
: Reports virtual memory statistics, offering insight into memory, swapping, and system processes.This command displays virtual memory stats at 1-second intervals, repeated five times.
iostat
: Useful for monitoring system input/output device loading by observing the time devices are active in relation to their average transfer rates.Run this to see CPU and device I/O statistics. Like the read/write data ratio on your nvme device 🤓
Managing Process Priorities
The Linux kernel allows you to adjust the priority of system processes, which can be particularly useful when you need to allocate more CPU resources to critical tasks.
Use Case: Increasing the priority of a backup script to complete system backups more quickly during off-peak hours.
nice
: Starts a process with a defined niceness (a priority), where a lower number means a higher priority. The default niceness for processes is 0.
This starts the backup script with a higher priority than normal. You can also changes the niceness of an already running process with
renice
.renice -n -5 -p [PID]
Loading and Unloading Kernel Modules
Kernel modules are pieces of code that can be loaded into the kernel to extend its functionality without the need to reboot the system.
lsmod
: Lists currently loaded kernel modules. Let's try to print our nvidia kernel after installing some drivers for our gpus ⚙️lsmod | grep nvidia
- Displays information about a nvidia kernel module with
modinfo
:modinfo nvidia
- Adds or removes modules from the kernel with
modprobe
. Unlikeinsmod
,modprobe
automatically handles module dependencies.
Tip
If you want to become a real linux kernel shinobi and you are cool about doing C take a look at : Write your first kernel module
Hardware management on Linux
Right off the bat, the Linux kernel is like the conductor of your computer's orchestra, deciding which piece of hardware plays and how, as soon as you power up. It's also got a neat trick up its sleeve for managing top-notch software through a special programming interface. For instance, it lets you peek at the hardware plugged into your motherboard's slots, giving you the lowdown on what's connected.
Moreover, this programming interface acts like a middleman. Let's say you're itching to video chat with your buddies; you're gonna need a webcam for that. Thanks to this middleman, the chat software doesn't sweat over what brand or model your webcam is. It talks to a standard Linux interface, and the Linux kernel translates this chitchat into the actual commands your webcam understands.
To dig on your system's hardware, the Linux kernel uses something like a virtual bulletin board, known as the /proc
and /sys
file systems. Here are a couple of tools that tap into this info, showing you what's what with your devices:
lspci
: This tool rolls out details on PCI devices - think graphics cards, network cards, and more.lsusb
: Got USB gadgets? This command lists all USB devices hooked up to your system.lspcmcia
: For those using PCMCIA cards (a bit old school, but still around), this command gives you the scoop.
In short, the Linux kernel doesn't just make sure your hardware and software are on speaking terms; it also lets you and your apps get the most out of the system, no matter the make or model of your devices.
Linux File System Management
File systems are a big deal in the Linux world, offering one of the coolest features of using Linux. Imagine all your files living together in a big family tree - that's pretty much how Linux organizes everything. At the top of this family tree is the root directory, noted as "/", and under this root, you'll find a bunch of other folders branching out.
One key folder under the root is the /home
directory, pretty much your personal space where you keep all your stuff. Inside /home
you'll have your own folder, and inside that, all sorts of files and other folders, like the Desktop.
So, if you've got a file called "helloworld.txt" sitting on your desktop, Linux sees it as something like /home/yourusername/Desktop/helloworld.txt
where "yourusername" is, well, your username 😅. This setup helps the Linux kernel to bridge the gap between the file you see and where it actually lives on your computer's hard drive.
Linux is also pretty smart when it comes to handling multiple hard drives. Through a process called mounting, Linux can make other disks look like they're just another folder under the root directory. So, if you've got your /home
directory on a second disk, Linux makes it seamless to access it as if it was on the main disk all along.
You can check out how and where disks are mounted using the findmnt -A
command, which shows you all the mount points.
And talking about disk formats, Linux is pretty versatile. It mainly uses the ext2, ext3, and ext4 formats but can handle a bunch of others too. Before you can use a new disk, though, you've got to format it with the right file system. For example, using
mkfs.ext3
will format a disk to the ext3 format. Just a heads-up, formatting wipes the disk, so be sure you know what you're doing before you hit enter.
Linux isn't just about local storage, either. It plays well with network file systems like NFS, where your files are stored on a server somewhere else but act like they're right there on your local machine. This way, you get to keep using Linux's neat file system structure without having to worry about where the data physically resides.
So, whether you're dealing with local or network storage, Linux's file system has got you covered, making it super easy to keep track of your digital stuff in an organized way.
Become a Kernel ninja 🥷🏼
So you want to dig more into Linux Kernels and beacome a real shinobi, here you are some lecture and labs to put your hand on it 🧰