Course outline · 0% complete

0/28 lessons0%

Course overview →

User space and kernel space

lesson 8-1 · ~11 min · 25/28

From lesson 1-3, guests in the hotel model never walk into the kitchen and cook. They order room service, and the OS equivalent is that programs make requests to the OS.

Programs never touch hardware directly, they ask. This unit gives that request its real name, the system call, and shows the wall that makes asking mandatory rather than merely polite.

Two privilege levels, enforced by the CPU

This boundary is where the OS's authority physically lives, and it shows up in daily engineering. Profilers report time spent in syscalls, container security is a policy about which syscalls a process may make, and "too many small writes" performance bugs are really "too many boundary crossings". Knowing the wall exists changes how all three read.

The separation between programs and the OS is not a polite convention, it is built into the CPU hardware, which runs in one of two modes.

ModeWho runs in itPrivileged instructions
user modeyour programsphysically disallowed
kernel modethe kernelall allowed

In user mode, instructions that touch hardware, another process's memory, or privileged settings are physically refused, and attempting one triggers a trap that usually kills the process, which is the segfault from lesson 4-2. In kernel mode, everything is allowed.

The phrases user space and kernel space mean the code and memory living on each side of this wall.

A program reads a file or sends a packet by asking the kernel to do it, through a system call, often shortened to syscall. That is a special CPU instruction which safely switches into kernel mode at a fixed, kernel-controlled entry point, runs the kernel's code for the request, and then returns to user mode with the result.

USER SPACEyour program: open(), print(), socketsno direct hardware accessKERNEL SPACEdrivers, filesystem, schedulerfull hardware accesssystem call boundaryrequestresult
A system call: the request crosses into kernel space through a controlled gate, the kernel does the privileged work, and the result comes back.

The core syscall vocabulary

A handful of system calls underlie almost everything a program does. On Linux the big ones are:

SyscallWhat it doesYou have been using it via
openget access to a fileopen("notes.txt") in unit 7
read and writemove bytes in or outf.read(), f.write(), print()
fork plus execcreate a process, load a programsubprocess.run in lesson 3-3
exitterminate, reporting an exit codesys.exit(2) in lesson 3-3

The list is short on purpose. A modern kernel offers a few hundred syscalls, and ordinary programs spend nearly all their time in these five.

Each syscall is a mode switch into the kernel and back, which is why buffering exists as covered in lesson 7-2. Fewer syscalls means less boundary-crossing overhead, and that single trade explains most of the performance advice about I/O.

Why a program cannot move disk bytes itself

A Python program runs in user mode, where the CPU physically refuses privileged hardware instructions, so it cannot skip the kernel.

User mode is a hardware-enforced cage. Privileged operations trap instead of executing, and the trap hands control to the kernel rather than completing the instruction.

Mechanism from earlierDepends on user mode
process isolation, lesson 3-1yes
memory protection, lesson 4-2yes
multi-user securityyes

That one mechanism is what makes all three possible at all. Without it, every rule the OS enforces would be a suggestion any program could ignore by writing to the hardware directly.

Naming the controlled request into the kernel

The controlled request a user-mode program makes to ask the kernel for privileged work is a system call, usually shortened to syscall.

It is the only doorway through the user and kernel wall: a special instruction that enters the kernel at a fixed entry point, runs the requested service, and returns to user mode with a result.

In the hotel model from lesson 1-3, it is the room service order. The guest never enters the kitchen, and the kitchen decides what it will and will not make.

Because the entry point is fixed and kernel-controlled, a program cannot jump into the middle of kernel code. It can only arrive at the door the kernel chose, which is what makes the boundary safe rather than merely conventional.