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.

Intermediate 3 min readLesson 2 of 16 Interview Q&A

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

Your codelibcKernelread(fd, buf, 4096)put syscall number + args in registerssyscall instruction (trap to kernel mode)validate args, do the worksysret: result in raxreturn bytes read (or -1, errno)
  1. The program calls a wrapper function in the C library (or the language runtime).
  2. The wrapper places the syscall number (e.g. 0 = read on x86-64 Linux) and arguments in registers.
  3. It executes the syscall instruction: the CPU switches to kernel mode and jumps to the kernel's entry point.
  4. The kernel looks up the handler in the syscall table, validates every argument (especially user-supplied pointers), and does the work.
  5. It returns to user mode with the result; on error the wrapper sets errno and returns −1.

Common system calls

CategoryLinux system calls
Processesfork, clone, execve, exit, wait4, getpid, kill
Filesopen/openat, read, write, close, lseek, stat, fsync, unlink, rename
Memorymmap, munmap, brk, mprotect
Networkingsocket, bind, listen, accept, connect, sendto, recvfrom
Waiting for I/Oselect, poll, epoll_wait, io_uring_enter
IPCpipe, shmget, futex, eventfd
Timeclock_gettime, nanosleep

Linux has about 450 system calls; Windows has a similar set behind the Win32 API (the Nt* functions).

Seeing them: strace

Shell
$ 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)                       = 0

strace (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 / nanosleep

What 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

TechniqueIdea
vDSOThe kernel maps some read-only data and code into every process, so clock_gettime and gettimeofday run without entering the kernel
mmapMap a file into memory; reads become ordinary memory accesses (with page faults on first touch)
sendfile / spliceCopy file → socket inside the kernel (zero-copy); used by web servers and Kafka
epollRegister interest in many sockets once, then wait for whichever are ready (scales to 100k+ connections)
io_uringShared 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.
Key takeaways
  • System calls are the controlled gate from user mode into the kernel.
  • Mechanism: syscall number + args in registers → syscall instruction → 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.
Interview questions
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.