1. Kernel, user mode and syscalls
One-liner. The CPU has privilege levels; the kernel is the only code allowed to change the machine.
Memorize
- User mode cannot touch devices, page tables or other processes; kernel mode can.
- A syscall is a controlled transition: trap instruction → saved registers → handler → return.
- Cost: mode switch + register save/restore. Tens to hundreds of nanoseconds — cheap once, expensive in a loop.
- Interrupts (hardware) preempt everything; exceptions are synchronous (fault, trap, abort).
- The kernel is monolithic (Linux: all services in one address space) but modular (loadable modules LKMs).
- Common abstractions: process, file descriptor, socket, inode, page, namespace.
straceshows the boundary: every line is one syscall.
Trap. Believing write() is synchronous because it returned — it usually just copied into the page cache. Do this. Use fsync/fdatasync when durability matters, and measure it.
Check
Why is a syscall more expensive than a normal function call?
Why
The transition itself is the cost; the actual work may be trivial.