Linux › Section A · The big picture › Topic A.3

The kernel: processes, memory, drivers, and system calls

The kernel is the one program that every other program depends on. It shares out the processor, guards each process's memory, operates the hardware and answers requests through system calls, and knowing how it does these four jobs explains most of what you see on a Linux system.

Four jobs for one program

The previous topic showed that a running system is bits in main memory. The kernel's work revolves around that memory: it divides it among processes and makes sure each stays inside its share. Its responsibilities fall into four areas. Process management decides which process may use a CPU, and for how long. Memory management tracks which memory belongs to whom, what is shared and what is free. Device drivers operate the hardware for processes. System calls are the requests processes make to get any of this done.

Process management: sharing the CPU

A desktop may run a browser and dozens of background services at once. On a single CPU core only one of them can execute at any instant. The kernel gives each runnable process a short time slice, typically a few milliseconds, then pauses it and lets another run. Handing the CPU from one process to another is a context switch. The slices are so short that everything appears to run together, which is called multitasking. With several cores, some processes really do run at the same moment, but each core still switches among many.

Inside a context switch

Suppose a process's time slice runs out. 1. A hardware timer interrupts the CPU, which switches to kernel mode and jumps into the kernel. 2. The kernel saves the interrupted process's registers and other state so it can resume exactly where it stopped. 3. It handles pending work, such as data arriving from a disk. 4. It picks the next process from those ready to run. 5. It loads that process's memory map and saved registers. 6. It sets the timer for the new slice. 7. It switches back to user mode and lets the process continue. So the kernel runs between slices, and when processes ask it for something.

Memory management: the rules

The kernel must enforce several rules at all times. The kernel needs a private area that no user process can touch. Each process needs its own section of memory, and one process may not read or write another's private memory. Yet processes must be able to share memory deliberately; many programs use one copy of a common library. Some memory, such as program code, should be read-only. And the system should be able to use more memory than it physically has, by moving rarely used data out to disk, as the topic "Swap space" explains.

Virtual memory and the MMU

Checking every access in software would be far too slow, so hardware helps. The processor's memory management unit (MMU) provides virtual memory: each process uses its own virtual addresses, as if it had the machine to itself. On every access, the MMU translates the virtual address into a physical one using a map called a page table, which works in fixed-size pages, commonly 4 KiB. The kernel builds and updates each process's page table, marks pages as read-only or as off-limits to user mode, and switches to the incoming process's map at every context switch. The Computer Science topic "The memory manager and the file manager" traces a translation step by step.

Worked example: one address, two processes

Processes A and B both read virtual address 0x401010. Pages are 4 KiB (0x1000 bytes). A's page table maps virtual page 0x401 to physical frame 0x2A3; B's maps page 0x401 to frame 0x7F0. 1. Split the address: with 4 KiB pages the last three hex digits are the offset, so the page is 0x401 and the offset 0x010. 2. For A, the MMU replaces the page number with frame 0x2A3, giving physical address 0x2A3010. 3. For B it gives 0x7F0010. 4. The same virtual address reaches two different places in RAM. Check: neither process can even name the other's memory, so isolation needs no effort from the programs.

Device drivers

Hardware is reached only in kernel mode, because a careless user process could corrupt a disk or hang a device. Yet devices doing the same job rarely share a programming interface: two network cards from different makers need different commands. A device driver is kernel code that knows one kind of device and presents it through a standard interface, so programs can use any disk or network card in the same way. Linux is a monolithic kernel: drivers run inside the kernel itself, though most are built as modules that the kernel loads when the hardware is found; the topic "In depth: SCSI, drivers, and the kernel" goes further.

System calls

A system call, or syscall, is a process's request to the kernel for something it cannot do alone, such as opening, reading or writing a file or creating a process. The process runs a special instruction that switches the CPU into kernel mode at a fixed entry point; the kernel checks the request, does the work and returns to user mode. System calls are written with parentheses, such as open() and read(), after their form in the C language. The kernel also offers pseudodevices, which look like devices but are pure software: /dev/null discards everything written to it, and /dev/random supplies random bytes.

fork and exec

Two system calls explain how programs start. fork() asks the kernel for a nearly identical copy of the calling process; the child gets a new process ID (PID) and records its parent's PID (PPID). exec() asks the kernel to replace the calling process's program with a different one, keeping the same PID. Strictly, exec is a family of calls built on the Linux system call execve(). When you type ls, the shell forks, and the child execs ls while the parent shell waits. Apart from PID 1, which the kernel starts at boot, every user process begins with a fork, so processes form a tree rooted at PID 1.

Worked example: watching fork and exec in bash

Bash keeps its PID in $$ and its parent's in $PPID. 1. Run echo $$. One run printed 18854; yours will differ. 2. Run bash -c 'echo $$ $PPID'. The output was 18855 18854: a new process whose parent is the first shell, created by a fork. 3. Run bash -c 'echo before; exec echo replaced; echo never'. It printed before, then replaced, and never was missing. 4. The exec builtin replaced that shell's program with echo, so no shell code was left to run the last command. Check: the earlier shell's prompt returned, because only the child was replaced.

Common mistakes to avoid

First, picturing the kernel as a process running alongside the others; it runs on interrupts and system calls. Second, thinking fork starts a new program; it copies the current one, and exec loads the new program. Third, assuming exec changes the PID; it keeps it. Fourth, believing virtual memory needs no physical memory; pages must be in RAM to be used. Fifth, assuming a user process can drive hardware directly; it must ask the kernel. Sixth, confusing multitasking on one core with true parallel execution.

Key takeaways

The kernel manages processes, memory, devices and system calls. It shares each CPU core through short time slices and context switches on timer interrupts. It isolates processes and allows deliberate sharing, using the MMU and per-process page tables to translate virtual addresses. Drivers give programs a uniform view of varied hardware. Processes request services through system calls such as open() and read(). fork() copies a process and exec() replaces its program, so every process descends from PID 1.

Flashcards

Flashcard

Kernel's four areas

Processes, memory, device drivers, system calls

Everything else asks the kernel.

Test your understanding

Question 1 of 5

You type ls at a bash prompt. Which sequence does the kernel carry out?

Question 2 of 5

Processes A and B both read virtual address 0x5000 at the same moment. What happens?

Question 3 of 5

What normally triggers a context switch when a process has used up its time slice?

Question 4 of 5

Running bash -c 'echo one; exec echo two; echo three' prints which lines?

Question 5 of 5

Why are device drivers usually part of the kernel?

← Previous topicNext topic →