System Calls & the Kernel
How a program asks the kernel for services — the syscall mechanism, common Linux system calls, libc wrappers, costs, strace, and vDSO/io_uring optimisations.
A system call is the only way a user program can ask the kernel to do something privileged: open a file, allocate memory, create a process, send a packet. Everything your program does that touches the outside world goes through them.
How a system call works
- The program calls a wrapper function in the C library (or the language runtime).
- The wrapper places the syscall number (e.g. 0 =
readon x86-64 Linux) and arguments in registers. - It executes the
syscallinstruction: the CPU switches to kernel mode and jumps to the kernel's entry point. - The kernel looks up the handler in the syscall table, validates every argument (especially user-supplied pointers), and does the work.
- It returns to user mode with the result; on error the wrapper sets
errnoand returns −1.
Common system calls
| Category | Linux system calls |
|---|---|
| Processes | fork, clone, execve, exit, wait4, getpid, kill |
| Files | open/openat, read, write, close, lseek, stat, fsync, unlink, rename |
| Memory | mmap, munmap, brk, mprotect |
| Networking | socket, bind, listen, accept, connect, sendto, recvfrom |
| Waiting for I/O | select, poll, epoll_wait, io_uring_enter |
| IPC | pipe, shmget, futex, eventfd |
| Time | clock_gettime, nanosleep |
Linux has about 450 system calls; Windows has a similar set behind the Win32 API (the Nt* functions).
Seeing them: strace
$ strace -c python3 -c "print('hi')" # count calls by type
$ strace -e trace=openat,read cat notes.txt
openat(AT_FDCWD, "notes.txt", O_RDONLY) = 3
read(3, "buy milk\n", 131072) = 9
read(3, "", 131072) = 0strace (Linux), dtruss (macOS) and Process Monitor (Windows) show every call a program makes. It's an invaluable debugging tool: "which config file is it actually reading?", "why is it hanging?" (it's blocked in connect).
Your language hides them
// Each of these eventually makes system calls:
Files.readString(Path.of("notes.txt")); // openat, fstat, read, close
new ProcessBuilder("ls").start(); // clone/fork + execve
Thread.sleep(100); // futex / nanosleepimport os
fd = os.open("notes.txt", os.O_RDONLY) # thin wrapper over open(2)
data = os.read(fd, 4096) # read(2)
os.close(fd)
with open("notes.txt") as f: # buffered: fewer read(2) calls
text = f.read()import { openSync, readSync, closeSync, readFile } from "node:fs";
const fd = openSync("notes.txt", "r"); // open(2)
const buf = Buffer.alloc(4096);
readSync(fd, buf); // read(2)
closeSync(fd);
readFile("notes.txt", () => {}); // async: runs on libuv's thread poolWhat a system call costs
A trivial syscall takes roughly 100 ns – 1 µs — 100 or more times the cost of a function call. Mode switches, register saving, security mitigations (Spectre/Meltdown fixes made them slower) and cache/TLB pollution all add up.
Avoiding the round trip
| Technique | Idea |
|---|---|
| vDSO | The kernel maps some read-only data and code into every process, so clock_gettime and gettimeofday run without entering the kernel |
mmap | Map a file into memory; reads become ordinary memory accesses (with page faults on first touch) |
sendfile / splice | Copy file → socket inside the kernel (zero-copy); used by web servers and Kafka |
epoll | Register interest in many sockets once, then wait for whichever are ready (scales to 100k+ connections) |
io_uring | Shared ring buffers: submit many I/O requests and collect completions with few or no syscalls |
Context switch vs mode switch
- A mode switch (user → kernel → user) happens on every system call. The same process continues.
- A context switch changes which thread runs: save its registers, maybe switch page tables, restore another thread. It happens when a thread blocks (waiting on I/O or a lock) or its time slice ends. It costs a few microseconds plus cold caches afterwards.
- System calls are the controlled gate from user mode into the kernel.
- Mechanism: syscall number + args in registers →
syscallinstruction → kernel handler → return. - The kernel validates everything user programs pass in.
- Syscalls cost 100 ns – 1 µs; buffer and batch I/O.
- vDSO, mmap, sendfile, epoll and io_uring reduce syscall overhead.
QWhat happens when you call printf in C?medium
printf formats into a user-space stdio buffer. Only when the buffer is flushed (on a newline for a terminal, when the buffer is full, or at exit) does libc call write(1, buf, n), which traps into the kernel. The kernel passes the bytes to the terminal or pipe driver.
QWhy is reading a file byte-by-byte with read(2) slow?easy
Each call pays the fixed user→kernel→user transition cost (hundreds of nanoseconds) to move a single byte. Reading 64 KB per call amortises that cost over many bytes.