Processes, Signals and Resources

Understand how Linux runs programs, how to inspect and stop them, and how the kernel shares CPU and memory between them.

Beginner · about 6 hours · difficulty 2/5

Learn first: Linux Command Line and Packages

Every running program is a process with a PID, a parent, an owner, open file descriptors and a slice of CPU and memory. When a service misbehaves in production, you will be inspecting processes.

Key concepts

  • fork and exec: new processes are cloned from a parent, then replaced with a new program. PID 1 is the init system (or your app, inside a container).
  • States: running, sleeping, stopped, and zombie (finished but not reaped by its parent).
  • Signals: SIGTERM asks a process to stop gracefully, SIGKILL forces it, SIGHUP often means "reload config", SIGINT is Ctrl+C. Apps should handle SIGTERM to shut down cleanly; Kubernetes and Docker send it before killing.
  • Foreground and background jobs: &, jobs, fg, bg, nohup.
  • Priorities and limits: nice, renice, ulimit (open files is a classic production limit).

Tools

ps aux, pstree, top/htop, kill, pgrep/pkill, lsof (who has this file or port open), and the /proc filesystem, which exposes every process as a directory.

Pitfalls

Reaching for kill -9 first (no cleanup, possible corruption), and forgetting that a container whose PID 1 ignores SIGTERM will take the full grace period to stop.

Good enough to move on: you can find which process is using a port, explain what happens when you send SIGTERM versus SIGKILL, and read a process's limits in /proc.

Key concepts

  • PIDs, parents and fork/exec
  • Signals: TERM, KILL, HUP, INT
  • ps, top, htop, pgrep
  • lsof and file descriptors
  • Jobs, nohup and background work
  • nice and ulimit
  • /proc filesystem
  • cgroups and namespaces: The kernel features containers are built on

Mini project

Write a tiny program that traps SIGTERM and logs a graceful shutdown message, run it in the background, then use pgrep, lsof and kill to find and stop it while watching the log.

Check yourself

Why might a containerised app take 30 seconds to stop, and how would you fix it?

Free resources