ホーム 暮らし [Thorough Explanation] What exactly is the Linux kernel?

[Thorough Explanation] What exactly is the Linux kernel?

八木 麻衣 八木 麻衣 • 更新: • 公開: • 読了約 26 分
目次

1. What is the Linux kernel?

n769ef84b2c8d
1.1 The kernel as the "center" of the OS

The Linux kernel is the software that handles the most core part of an operating system (OS). An OS has the function of abstracting hardware such as the CPU, memory, hard disk, network, and keyboard, and providing an API that can be easily used by applications (user-space programs). The "main body" that actually performs this abstraction is the kernel.

  • Main functions of an OS

    1. Abstraction for unified handling of CPU, memory, storage, networks, etc.

    2. Security and access control

    3. Process management and file management

    4. Foundation for starting and stopping the computer

In Linux, the "Linux kernel" plays this central role. However, to use it as an actual OS, shells, commands, and various services that run in user space are required. Distributions (Ubuntu, Fedora, Debian, RHEL, etc.) bundle a wealth of user-space programs in addition to the Linux kernel and provide them as a single package.

1.2 Why "the kernel alone is not an OS"

It is often said that "Linux is an OS," but strictly speaking, the "Linux kernel" is the core of the OS, and the kernel alone cannot yet be called a "complete OS" that a user can operate. The commands users normally use (ls, grep, find, etc.), network tools, ssh, and GUI environments (X Window or Wayland) are all part of user space.

  • Kernel ... A single binary (+ modules) that consolidates foundational functions such as CPU control, memory management, network stacks, and file system management

  • User space ... Shells, various utilities, GUI, desktop environments, applications, etc.

Therefore, even if you have the absolute minimum kernel, it remains incomplete as an OS if the tools for users to handle it are not in place.

2. Basics of Linux architecture

2.1 Three layers

As mentioned above, a Linux system is broadly composed of three layers (see Figure 2-1):

  1. Hardware layer

    • All physical devices, such as the CPU, main memory, disk drives, network cards, keyboards, and monitors.

  2. Kernel

    • Abstracts hardware and provides a unified API for use from user space.

    • A privileged program executed in "kernel mode" that handles process management, memory management, file systems, network stacks, device drivers, etc.

  3. User Space

    • Various commands and utilities such as shell, ps, and ssh.

    • Graphical environments such as the X Window System and Wayland.

    • Various applications (web browsers, text editors, DB servers, etc.) also run here.

2.2 Kernel Mode and User Mode

Linux has "modes" with different privilege levels.

  • Kernel Mode ... A privilege level that allows direct access to hardware resources. Code executed by the kernel runs in this mode, making it fast and flexible, but there is a risk that bugs or incorrect implementations could make the entire system unstable.

  • User Mode ... A mode where normal applications and commands run. It uses the kernel indirectly (via system calls) in a restricted manner. It is highly secure but cannot touch hardware directly.

Application developers are usually conscious of the user mode side, and entering kernel mode is not very common except in special cases (such as when implementing drivers). However, understanding how applications interact with the kernel (system calls, device files, etc.) is useful for troubleshooting.

3. CPU Architecture and the Linux Kernel

3.1 Diverse CPU Families

A major feature of Linux is that it has been ported to multiple CPU architectures (families).

  • x86 (including x86_64)
    Mainly Intel and AMD. Widely used in desktop PCs and servers, and most public clouds are x86_64 based.

  • ARM
    Adopted by smartphones (iPhone, Android), Raspberry Pi, AWS Graviton, etc. It is suitable for embedded devices because it consumes less power and suppresses heat generation.

  • RISC-V
    : An emerging open architecture that is rapidly gaining attention in the fields of education and research. Major IT companies are also beginning to take notice.

3.2 Architecture-Specific Code within the Kernel

Although the Linux kernel is monolithic, architecture-specific parts are separated into directories such as arch/.

  • Boot process: There are differences, such as booting via BIOS/UEFI for x86, or often using U-Boot for ARM.

  • Interrupt controller: Since usage differs depending on the CPU, it is handled by specific code.

  • Instruction set/register control: When using architecture-specific optimizations or extensions such as x86 SSE/AVX or ARM NEON, those parts are implemented with dedicated code.

The ability to support a diverse range of CPUs is precisely because architecture-dependent code is organized.

4. Kernel Components

4.1 Process Management

4.1.1 Processes and Threads

  • Process: A unit assigned to an executable program. Uniquely identified by a PID.

  • Thread: An execution unit within a process. In Linux, "threads are also treated almost as processes," but they differ from normal processes in that they share memory space and file descriptors.

The kernel manages processes using a data structure called task_struct, and handles scheduling, memory allocation, and signal processing in an integrated manner.

4.1.2 Sessions and Process Groups

  • Session: The top-level unit for shells that execute commands and for job control (identified by SID).

  • Process group: A unit that groups multiple processes (identified by PGID). Used when switching between foreground (fg) and background (bg) in shell job control.

4.1.3 Scheduling

The kernel determines "which process to run on the CPU and when."

  • Uses algorithms such as CFS (Completely Fair Scheduler) to aim for high throughput while maintaining fairness and responsiveness.

  • Capable of operating according to diverse needs, such as real-time scheduling and priority (nice values).

4.2 Memory Management

4.2.1 Virtual Memory and Paging

  • Virtual memory: Provides each process with a "vast address space," making it appear as if there is more memory than the physical memory available.

  • Page: A memory management unit, typically 4KB in size. Each process has its own page table to perform virtual-to-physical address translation.

4.2.2 TLB (Translation Lookaside Buffer)

  • The CPU has a cache called the TLB, which speeds up virtual-to-physical address translation.

  • Since frequent TLB misses lead to performance degradation, techniques such as using HugePages are employed to reduce these misses.

4.2.3 Overcommit and Swap

  • Linux has settings that allow memory overcommit, meaning physical memory is not reserved until it is actually used.

  • When physical memory runs low, the system operates by swapping data with the swap area (a portion of the disk).

  • Excessive swapping leads to performance degradation, so it is adjusted using kernel parameters (such as /proc/sys/vm/swappiness).

4.3 Networking

4.3.1 Layered Architecture

  • A layered structure consisting of Socket → TCP/UDP → IP → Device Driver.

  • By using the socket API, applications can perform network communication without being aware of hardware differences.

4.3.2 Network Interfaces and Routing

  • You can check the list of interfaces using the ip link command.

  • You can display the routing table with ip route.

  • NAT, firewalls (iptables, nftables), and similar functions are processed within the network stack of the kernel space.

4.4 File Systems

4.4.1 VFS (Virtual File System)

  • It abstracts operations such as open, read, write, and close, and treats underlying concrete file systems (ext4, btrfs, xfs, NFS, NTFS, etc.) like plugins.

  • The application side does not need to be aware of the file system type.

4.4.2 Mounting

  • Devices and partitions are used by "mounting" them into a directory tree rooted at /.

  • You can check what is connected where using the mount command.

4.5 Device Drivers

4.5.1 Small-scale in-kernel code

  • Small programs (drivers) that control input devices like keyboards and mice, GPUs, HDDs/SSDs, and USB devices run within the kernel.

  • They directly manipulate physical I/O ports, receive interrupts from devices, and provide them to user space in the form of file operations.

4.5.2 sysfs and /sys/devices/

  • A pseudo-file system called sysfs is provided under /sys to expose driver and device information to user space.

  • You can check connected devices using commands like ls -al /sys/devices/.

4.5.3 Kernel Modules

  • Drivers can not only be statically built into the kernel, but can also be dynamically loaded as modules (.ko files) as needed.

  • They are managed using commands such as lsmod, modprobe, and rmmod.

5. System Calls: Interaction between the kernel and user space

5.1 What are system calls?
  • A mechanism used by applications to access kernel functions.

  • Examples: open(), read(), write(), fork(), execve(), brk(), mmap(), and many others.

  • Switch the CPU mode from user space to kernel space using a "software interrupt" or a dedicated instruction (such as a syscall), perform the processing, and return the result.

5.2 Visualization with strace
  • Running strace <command> allows you to see which system calls are being invoked and in what order.

  • For example, running strace ls reveals numerous calls such as openat(), read(), close(), and execve().

  • This is useful for performance diagnostics and troubleshooting.

5.3 Examples of Representative System Calls
  • Process Management: fork(), execve(), wait(), exit()

  • Memory Management: brk(), mmap(), munmap(), mremap()

  • Networking: socket(), bind(), listen(), accept(), sendto(), recvfrom()

  • File System: open(), close(), read(), write(), ioctl(), stat()

  • Signals: kill(), sigaction(), signalfd()

6. How to Extend the Kernel

6.1 Kernel Modules
  • Programs (.ko) that can be loaded as needed.

  • Many drivers are distributed in module format and are often loaded automatically when a device is detected.

  • You can display dependencies using modprobe --show-depends <module name>.

6.2 eBPF (extended BPF)
  • A modern kernel extension technology that has been gaining attention in recent years.

  • It allows you to "inject" small programs from user space into a lightweight virtual machine running within the kernel, enabling advanced processing by hooking specific events such as network packets or system calls.

  • Use Cases: Container network implementation (Cilium, etc.), dynamic tracing (bpftrace), security (Falco), load balancers (katran), etc.


同じカテゴリの記事

八木 麻衣
トレンドリサーチャー 八木 麻衣
プロフィールを見る →

全記事の事実確認を担当。一次情報・公式発表・公開資料の三点を確認できない場合は公開を見送る判断をします。

おすすめの関連記事