AlgoViz

How the Linux kernel works

medium 40 min

The 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 hearSayBecause
"What happens when you run a command?"fork, execve, page faults, syscalls, exit and waitIt walks every subsystem once
"The command is slow"Is it waiting for CPU, disk or a lock? Check r, b, D stateWaiting looks the same from outside
"kill -9 does not work"D state or zombie, and why signals waitSignals act on return to user mode
"Load is high but CPU is idle"Load counts R and D tasksFrom the proc manual
"Container restarts silently"memory.max, OOM killer, memory.eventsSIGKILL leaves no log
"System CPU is high"%sys versus %irq and %soft, perf top, strace -cEach has a different cause

Word for word

  1. Interviewer

    cat on a small file takes two seconds on one host. Where could the time go?

  2. 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.

  3. Interviewer

    r is 1, b is 12, and the disk looks idle.

  4. 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.

  5. Interviewer

    And if wchan shows nothing useful?

  6. 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.