How the Linux kernel works
medium 40 minThe kernel is the one program that runs with full privilege.
When it comes up
The moments this idea belongs in a design interview, and the moments it does not.
Signals
When the kernel map comes up
| You hear | Say | Because |
|---|---|---|
| "What happens when you run a command?" | fork, execve, page faults, syscalls, exit and wait | It walks every subsystem once |
| "The command is slow" | Is it waiting for CPU, disk or a lock? Check r, b, D state | Waiting looks the same from outside |
| "kill -9 does not work" | D state or zombie, and why signals wait | Signals act on return to user mode |
| "Load is high but CPU is idle" | Load counts R and D tasks | From the proc manual |
| "Container restarts silently" | memory.max, OOM killer, memory.events | SIGKILL leaves no log |
| "System CPU is high" | %sys versus %irq and %soft, perf top, strace -c | Each has a different cause |
Word for word
Interviewer
cat on a small file takes two seconds on one host. Where could the time go?
You
Three places: waiting for a CPU, waiting for the disk, or waiting for a lock. I would run vmstat 1 first and look at r and b.
Interviewer
r is 1, b is 12, and the disk looks idle.
You
Then twelve tasks are in D but not on the local disk. I would check ps for D tasks and their wchan. If they sit in a network file system function, the file is on a slow mount.
Interviewer
And if wchan shows nothing useful?
You
I would read /proc/PID/stack as root for the full kernel stack, and check PSI io to see how much time is lost.
Explain why a process ignores kill -9.
BreaksThe signal was lost
- How
- Say signals are unreliable and send it again in a loop.
- Limit
- SIGKILL is not lost. The loop changes nothing and hides the real cause.
- Choose when
- Never.
WorksIt is a zombie
- How
- Say the process has exited and its parent must call wait.
- Limit
- True for state Z only. A process in D is alive and stuck, and the parent cannot help.
- Choose when
- When ps shows Z.
StrongRead the state, then explain
- How
- Check STAT. Z: reap through the parent. D: signals act on return to user mode, so read wchan and the kernel stack and fix the device or mount.
- Limit
- Needs access to /proc and sometimes root for the stack.
- Choose when
- Every time.