Part 3 · 2 chapters · ~18 min
The Operating System
Kernel and user space, system calls and their cost, interrupts, blocking and the epoll and kqueue facilities under every event loop; then processes and threads, context switches, Linux and macOS scheduling, multicore with performance and efficiency cores, and the browser's process and thread model.
7
Kernel and user space, syscalls and interrupts
one boundary, two ways across
- Privilege levels are enforced in hardware.
- Syscalls: load registers, trap, validate, do the work, return.
- Cost: hundreds of nanoseconds per crossing, which is why I/O is buffered and batched.
- Interrupts: devices and the timer take control between instructions.
- Blocking a thread costs no CPU.
- Event loops sit on epoll, kqueue or IOCP.
code
# see the syscalls Node makes to read a file
# Linux:
strace -f -e trace=openat,read,close,epoll_wait node -e "require('fs').readFileSync('/etc/hostname')"
# macOS (needs SIP adjustments, or use dtruss / Instruments):
sudo dtruss -f node -e "require('fs').readFileSync('/etc/hosts')" 2>&1 | grep -E 'open|read|close'KERNEL SPACE, USER SPACE, SYSCALLS, INTERRUPTS
the privilege boundary, the two ways across it, and what each crossing costs
swipe the figure sideways, or tap expand for full screen
1/6
two worlds
Two worlds: user space (your Node process, the browser's renderer) cannot execute privileged instructions, cannot touch other processes' memory and cannot talk to devices. Kernel space can. The CPU enforces this in hardware (rings on x86, exception levels on ARM).
8
Processes, threads and scheduling
isolation versus sharing
- Processes are isolated, so a crash stays inside one.
- Threads share memory and need synchronisation.
- Context switches cost a little directly and more through cold caches afterwards.
- Scheduling is a fair share on Linux and QoS classes on macOS.
- Multicore means run queues, affinity, and a mix of performance and efficiency cores.
- The browser uses separate processes per site, with threads inside each.
the frontend link
"JavaScript is single-threaded" means one renderer process's main thread runs your JS, style and layout, while the compositor, raster and worker threads run beside it. A long task blocks that one thread. Moving work to a Worker (the FSD course part 2) puts it on another OS thread, which the scheduler can run on another core.
PROCESSES, THREADS AND SCHEDULING
what isolates, what shares, and how the OS decides who runs on which core
swipe the figure sideways, or tap expand for full screen
1/6
a process
A process: its own page tables (part 2), file descriptor table, credentials, and one or more threads. Created by fork/exec on Unix or CreateProcess on Windows. A crash in one process cannot corrupt another; that is why browsers put each site in its own renderer process.