map / systems / syscalls

Systems · principle 5 of 7

Syscalls

A syscall is the only door from user space into the kernel, so tracing syscalls shows you what a program is really doing.

01 · Why it’s true

User programs can’t touch hardware or kernel data directly. To open a file, send on a socket, or get more memory, they make a system call — a controlled switch into the kernel, which checks permissions and does the work on their behalf.

Because every interaction with the outside world goes through system calls, tracing them shows what a program is really doing: which files it opens, which call it’s stuck in, and which calls fail. Each crossing also has a cost, so many tiny reads or writes are slower than a few large ones.

02 · Picture it

1Your programcalls read(), write(), connect()2Runtime / libcthin wrappers around system calls3System call boundaryswitch into kernel mode — checked4Kernelfiles, network stack, memory, scheduler5Hardwaredisk, network card, RAMevery request to the outside world goes down through the kernelBecause everything crosses the boundary, strace shows what a program is really doing —and many tiny calls cost more than a few big ones.
1Your programcalls read(), write(),connect()2Runtime / libcthin wrappers around systemcalls3System call boundaryswitch into kernel mode —checked4Kernelfiles, network stack, memory,scheduler5Hardwaredisk, network card, RAMevery request to the outside world goesdown through the kernelBecause everything crosses theboundary, strace shows what a programis really doing — and many tiny callscost more than a few big ones.
The only way from a program to the hardware is through the kernel.

03 · In the interview

  • Using strace to see what a stuck or failing process is doing.
  • Explaining why buffered I/O is faster than many tiny writes.
  • Reading error codes like ENOENT, EACCES, and ECONNREFUSED.

04 · Practice question

“A process is hung and its logs say nothing. How do you find out what it’s doing?”

A strong answer covers

  1. Attach strace -p <pid> to see which system call it’s blocked in — for example a read on a socket or a futex wait (a lock).
  2. Map the file descriptor in that call to a file or connection with ls -l /proc/<pid>/fd or lsof.
  3. Get thread stacks too, with a language-level thread dump or a debugger.
  4. Use strace briefly in production: it slows the traced process down.