TExcelTAD
Home › Operating Systems: Linux & Windows › Linux Architecture

Linux Architecture

"Linux" strictly means only the kernel, the program Linus Torvalds started in 1991 that still runs in privileged mode on every Linux machine. What people install, Ubuntu, Fedora, Debian, Arch, Alpine, Android, is a distribution: the kernel plus thousands of user-space programs, a C library, an init system, a package manager and default settings. This lesson maps out those layers and the parts inside the kernel.

The layers

text
 +----------------------------------------------------------------------+
 |  Applications: firefox, nginx, python, bash, your programs           |
 +----------------------------------------------------------------------+
 |  System programs & daemons: systemd, journald, sshd, NetworkManager, |
 |  coreutils (ls, cp), util-linux (mount, lsblk), udev                 |   user space
 +----------------------------------------------------------------------+   (ring 3)
 |  C library: glibc (most distros) or musl (Alpine)                    |
 |  open() fork() malloc() printf() -> wrappers around system calls     |
 +======================= system call interface ========================+
 |  Linux kernel                                                        |
 |   process mgmt & scheduler | memory mgmt | VFS + file systems         |   kernel space
 |   networking stack | IPC | security (LSM, namespaces, cgroups)       |   (ring 0)
 |   device drivers (mostly loadable modules)                           |
 +----------------------------------------------------------------------+
 |  Hardware: CPU, RAM, disks, NICs, GPU, USB                           |
 +----------------------------------------------------------------------+

The thick line is the important one. Code above it runs in user mode and cannot touch hardware or other processes' memory. To do anything real, open a file, send a packet, start a process, it makes a system call, which switches the CPU into kernel mode at a fixed entry point. (Details in User Mode & Kernel Mode and System Calls.)

Let's look at the sandbox's layers from the inside:

bash · try it yourself
uname -s -o              # kernel name and operating system
cat /proc/sys/kernel/ostype
getconf PAGESIZE         # the memory page size the kernel uses
getconf LONG_BIT         # a 64-bit system
ldd /bin/bash | awk '{print $1}'
Linux GNU/Linux
Linux
4096
64
/lib/ld-musl-x86_64.so.1
libreadline.so.8
libc.musl-x86_64.so.1
libncursesw.so.6

ldd lists the shared libraries bash is linked against. This sandbox runs Alpine, so the C library is musl; on Ubuntu you would see libc.so.6 (glibc) and ld-linux-x86-64.so.2 instead. Either way the C library is the layer that turns printf or fopen into the kernel's write and openat system calls.

Inside the kernel

Linux is a monolithic kernel: all the subsystems below run in one address space in kernel mode and call each other directly, which is fast. It is also modular: most drivers and many file systems are separate .ko files loaded on demand (Kernel Modules & Drivers). Compare that with microkernels, where drivers run as separate user processes, in Kernel Designs.

Subsystem Job You meet it as
Process management Creating processes and threads (clone), signals, exit ps, /proc/<pid>
Scheduler (EEVDF since 6.6) Choosing which thread runs on which CPU, and for how long nice, chrt, taskset
Memory management Virtual memory, page tables, page cache, swap, OOM killer free, /proc/meminfo
VFS + file systems One API over ext4, XFS, Btrfs, NFS, tmpfs, proc (Linux File Systems) mount, df
Networking Sockets, TCP/IP, routing, netfilter firewall ip, ss, nft
IPC Pipes, Unix sockets, shared memory, futexes |, /dev/shm
Security Users and capabilities, LSMs (SELinux, AppArmor), seccomp id, getenforce
Isolation Namespaces and cgroups, the base of containers docker, systemd-cgls
Device drivers Talking to hardware; the largest part of the source tree lsmod, lspci, dmesg

A system call's journey

Here is a toy model of what happens when a program calls write(1, "hi\n", 3). The C library puts the syscall number in a register and executes the syscall instruction; the kernel looks the number up in its system call table and hands the work to the right subsystem. The numbers below are the real x86-64 ones.

python · try it yourself
syscall_table = {            # number -> (name, subsystem)   real x86-64 numbers
    0:  ("read",   "VFS"),
    1:  ("write",  "VFS"),
    39: ("getpid", "process management"),
    57: ("fork",   "process management"),
    41: ("socket", "networking"),
    9:  ("mmap",   "memory management"),
}

def libc(func, *args):
    number = {name: no for no, (name, _) in syscall_table.items()}[func]
    print(f"user:   {func}{args} -> rax={number}, then the 'syscall' instruction")
    return kernel_entry(number, args)

def kernel_entry(number, args):
    if number not in syscall_table:
        return -38                                    # -ENOSYS: no such system call
    name, subsystem = syscall_table[number]
    print(f"kernel: table[{number}] = sys_{name}, handled by {subsystem}")
    if name == "write":
        fd, data, count = args
        return count                                  # bytes "written"
    if name == "getpid":
        return 4242
    return 0

print("result:", libc("write", 1, "hi\\n", 3))
print("result:", libc("getpid"))
print("result:", kernel_entry(999, ()), "(ENOSYS)")
user:   write(1, 'hi\\n', 3) -> rax=1, then the 'syscall' instruction
kernel: table[1] = sys_write, handled by VFS
result: 3
user:   getpid() -> rax=39, then the 'syscall' instruction
kernel: table[39] = sys_getpid, handled by process management
result: 4242
result: -38 (ENOSYS)

Linux has roughly 450 system calls on x86-64, and it promises never to break them: a binary compiled for Linux 2.6 still runs on Linux 6.x. That stable user-space ABI ("we do not break user space") is one of the project's strongest rules. In contrast, the kernel's internal interfaces change freely, which is why out-of-tree drivers need rebuilding for every new kernel.

User space: what a distribution adds

Component Examples Role
Boot loader GRUB, systemd-boot Loads the kernel (The Linux Boot Process)
Init system systemd, OpenRC (Alpine), runit PID 1: starts and supervises everything (init & systemd)
C library glibc, musl System call wrappers, malloc, DNS lookup, locales
Core tools GNU coreutils, util-linux, BusyBox ls, cp, mount, kill
Shell bash, zsh, dash The command line
Package manager apt (.deb), dnf (.rpm), pacman, apk Installs and updates everything else
Device manager udev (systemd-udevd) Creates /dev entries, loads drivers for new hardware
Graphics stack Mesa, Wayland compositor (GNOME's Mutter, KDE's KWin) Desktop

The same kernel can therefore look very different. Android uses Linux with its own C library (Bionic) and init; a router may run BusyBox and musl in a few megabytes; a container (like this sandbox) contains no kernel at all: it shares the host's kernel and brings only user space. You can check that the container is not a full machine:

bash · try it yourself
cat /proc/1/comm 2>/dev/null || echo "cannot read PID 1"
[ -d /boot ] && echo "has /boot" || echo "no /boot: this user space never boots a kernel"
[ -d /lib/modules ] && echo "has modules" || echo "no /lib/modules: drivers belong to the host"
bash
no /boot: this user space never boots a kernel
no /lib/modules: drivers belong to the host

PID 1 inside the container is just the shell that runs the example. On a real distribution it would print systemd.

Kernel versions

Linus Torvalds releases a new mainline kernel about every 9–10 weeks (6.12, 6.13, …). Each release starts with a two-week merge window for new features, followed by weekly -rc release candidates that only fix bugs. One release per year becomes a longterm (LTS) kernel, maintained for several years; distributions build on those and add their own patches (sample output):

bash
ada@ubuntu:~$ uname -r
6.8.0-45-generic
ada@ubuntu:~$ cat /proc/version
Linux version 6.8.0-45-generic (buildd@lcy02-amd64-075) (x86_64-linux-gnu-gcc-13 (Ubuntu 13.2.0-23ubuntu4) 13.2.0, GNU ld (GNU Binutils for Ubuntu) 2.42) #45-Ubuntu SMP PREEMPT_DYNAMIC Fri Aug 30 12:02:04 UTC 2024

6.8.0-45-generic reads as: upstream 6.8, Ubuntu's 45th package revision, the generic flavour.

Learn more: Kernel Designs · System Calls · Windows NT Architecture

Test yourself

Strictly speaking, what is "Linux"?

  1. A complete operating system with desktop
  2. The kernel
  3. The GNU tools
  4. A package manager

What does the C library (glibc or musl) do for a program that calls printf?

  1. Draws the text on screen directly
  2. Formats the text and makes the write system call
  3. Runs the program in kernel mode
  4. Nothing; printf is a CPU instruction

Why can a container run without a /boot directory or kernel modules?

  1. Containers are interpreted
  2. It shares the host's running kernel and brings only user space
  3. It uses a microkernel
  4. Docker emulates the kernel in Python