map / systems / processes-threads

Systems · principle 1 of 7

Processes vs threads

Processes get separate address spaces; threads share one — which makes threads cheap to communicate and easy to corrupt.

01 · Why it’s true

A process is a running program with its own private memory and its own set of open files and sockets. The operating system keeps processes isolated, so one crashing or corrupting its memory doesn’t affect another.

Threads are separate paths of execution inside one process. They share the heap, global variables, and open files; each has only its own stack and registers. Sharing makes communication cheap, but it also means one misbehaving thread can corrupt data — or crash the process — for all the others.

02 · Picture it

Two processesEach has its own memoryEach has its own open files and socketsA crash in one leaves the other runningTalk via pipes, sockets, or sharedmemoryOne process, two threadsShare one heap and globalsShare open files and socketsEach has only its own stackA crash in one thread takes down theprocessvsProcesses buy isolation; threads buy cheap sharing. Pick by which failure you can afford.
Two processesEach has its own memoryEach has its own open files andsocketsA crash in one leaves the otherrunningTalk via pipes, sockets, or sharedmemoryOne process, two threadsShare one heap and globalsShare open files and socketsEach has only its own stackA crash in one thread takes downthe processProcesses buy isolation; threads buycheap sharing. Pick by which failureyou can afford.
Isolation versus sharing.

03 · In the interview

  • Choosing between worker processes and threads: isolation versus efficiency.
  • Explaining why one bad thread can take down a whole service.
  • Seeing threads in tools: top -H, ps -L, or /proc/<pid>/task.

04 · Practice question

“When would you run several worker processes instead of one process with many threads?”

A strong answer covers

  1. Processes isolate failures: a crash or leak in one worker doesn’t take down the rest, and each can be restarted on its own.
  2. Threads are cheaper and share memory directly, which suits work that needs shared state or a shared cache.
  3. Some runtimes limit parallel execution of threads within a process (for example, CPython’s global interpreter lock in its default build), which pushes CPU-bound work toward processes.
  4. Trade-off: processes use more memory and need explicit communication.