Notes
A process
A process is a running program. It has a process ID, a parent's process ID, a user, its own memory, a set of open files, a working directory and environment variables, and a state. ps minus e f lists them all with their parents; top shows the busiest live. And everything they show comes from the per-process folders in slash proc that you met in episode three.
- a PID, and a parent PID
- a user and groups
- its own memory
- open files (descriptors)
- a working directory and environment variables
- a state: running, sleeping...
- see: ps -ef, top, /proc/<pid>
fork
How is a process born? Its parent copies itself, with a system call called fork, or clone in modern Linux. The copy is cheap, because memory is shared until one of them writes to it, a trick called copy on write. Then the copy usually replaces itself with a new program, using execve. The parent waits for it, and when the child exits, the parent collects its exit code. That is exactly how your shell runs every command.
- 1 fork (clone): the shell copies itself
- 2 the copy calls execve: replaced by the new program
- 3 the parent waits (wait4)
- 4 the child exits, parent collects the exit code
- copy is cheap: memory shared until written (copy-on-write)
Watch it
Here it is, traced. The shell, process eight seven two, started. A child, eight seven three, was attached: that is the fork. The child called execve and became ls. The parent sat in wait four. The child finished with exit group zero, and the parent's wait returned: eight seven three exited with status zero. Every command you have ever run on Linux went through those steps.
execve("/usr/bin/sh", ...)
Process 873 attached ← fork
[pid 873] execve("/usr/bin/ls",
["ls", "/etc/hostname"]) = 0
[pid 872] wait4(-1, ...)
[pid 873] exit_group(0)
wait4 → 873 exited, status 0 Zombies
What if the parent never collects the exit code? The child becomes a zombie: dead, using no memory, but still holding a slot in the process table, shown with state Z. We created one on purpose: ps showed process eight six two in state Z. When the parent called waitpid, the zombie was reaped and disappeared. A few zombies are harmless; thousands mean a buggy parent, and can exhaust the process limit. The fix is the parent, not the zombie.
child exits, parent does not wait yet: PID PPID STAT COMMAND 862 861 Z python3 parent calls waitpid(): reaped (gone) many zombies = a buggy parent
States
The state column tells you what a process is doing. R means running or ready. S means sleeping, waiting for something, which is where most processes spend their life. D means uninterruptible sleep, usually waiting on a disk or a network filesystem, and these cannot be killed until the I O finishes, so many D processes point at storage trouble. T is stopped, and Z is zombie. Orphans, whose parent died first, are adopted by PID one.
- R running or ready to run
- S sleeping, waiting for an event (most processes)
- D uninterruptible: waiting for disk or NFS; cannot be killed
- T stopped (Ctrl+Z)
- Z zombie
- orphans: adopted by PID 1
Signals
Signals are short messages the kernel delivers to a process. SIGTERM, number fifteen, means please stop: it is what kill sends by default, and what systemd, docker stop and Kubernetes send when they stop your app. SIGKILL, number nine, stops the process immediately and cannot be caught or ignored. SIGINT is control C. And SIGHUP traditionally means reload your configuration, which nginx and many daemons support.
- 15 SIGTERM please stop (default for kill, systemd, docker stop, Kubernetes) 9 SIGKILL stop now; cannot be caught 2 SIGINT Ctrl+C 1 SIGHUP reload config (nginx, many daemons)
- $ kill -l the full list
Graceful vs forced
Here is the difference in practice. Our small program caught SIGTERM. When we sent it, the program printed that it was closing connections and exited cleanly, with exit code zero. Then we started it again and sent SIGKILL. No message, no cleanup, and exit code one three seven, which is one hundred and twenty eight plus nine. A web server killed this way drops every request it was handling.
program traps SIGTERM: $ kill -TERM <pid> got SIGTERM: closing connections, exiting cleanly exit code 0 $ kill -KILL <pid> exit code 137 (128 + 9) no message, no cleanup
Why deploys care
This is why deployments care. On SIGTERM, a good service stops accepting new requests, finishes the ones in flight, closes database connections, flushes its logs, and exits with zero. Every platform waits a while before sending SIGKILL: by default ninety seconds in systemd, ten in docker stop, thirty in Kubernetes. If your app ignores SIGTERM, every deploy waits the full timeout and then drops requests anyway.
- stops accepting new requests
- finishes the ones in flight
- closes database connections
- flushes logs and buffers
- exits 0 within the timeout
- timeout then SIGKILL: systemd 90 s, Docker 10 s, Kubernetes 30 s by default
Common mistakes
Mistakes. Reaching for kill minus nine first, which skips all cleanup. Apps that ignore SIGTERM. In containers, a shell script running as process one that never passes signals to the real app, so stops always time out; part six fixes this with exec. Trying to kill zombies instead of fixing their parent. And ignoring piles of D state processes, which usually mean a storage problem.
- kill -9 first, every time
- apps that ignore SIGTERM
- a shell script as PID 1 in a container that does not pass signals on
- killing zombies (fix parent)
- ignoring many D-state processes (storage trouble)
Say it like this
In the interview: SIGTERM asks a process to stop. The process can catch it, stop taking new work, finish what is in flight, and exit cleanly. SIGKILL cannot be caught; the kernel ends the process at once, and the exit code is one three seven. systemd, Docker and Kubernetes send SIGTERM, wait a grace period, then SIGKILL, so services must handle SIGTERM for zero-downtime deploys.
- "SIGTERM asks a process to stop;
- it can catch it, finish in-flight
- work and exit cleanly. SIGKILL
- cannot be caught; the kernel
- ends it at once, exit 137.
- Platforms send TERM, wait a grace
- period, then KILL, so apps must
- handle TERM to deploy cleanly."