The textbook line is that a process is a program in execution and a thread is a lightweight unit of execution inside a process. That is correct and not very useful. The more useful way to think about it is in terms of what is shared and what is private, because almost every practical consequence follows from that one question: performance, crash behaviour, security and the kind of bugs you will spend your weekends on.
The sharing table
When the operating system starts a process, it gives it a private virtual address space, a set of open file handles, environment variables, a security identity and at least one thread. Threads created later inside that process share most of the process’s resources but keep a few things to themselves.
| Item | Separate processes | Threads in one process |
|---|---|---|
| Heap memory and global variables | Private to each | Shared |
| Open files and sockets | Private (unless inherited or passed) | Shared |
| Stack | Private | Private, one per thread |
| Registers and program counter | Private | Private, one set per thread |
| Thread-local storage | Not applicable | Private per thread |
| A crash from a bad memory access | Kills only that process | Usually kills the whole process |
Read the last row twice. Sharing memory is what makes threads fast to communicate, and it is also what makes them fragile together. A thread that corrupts the heap corrupts it for every other thread in the process.
Why threads are called lightweight
Creating a thread is cheaper than creating a process because the operating system does not need to build a new address space. Switching between two threads of the same process is also cheaper than switching between two processes. In both cases the CPU must save one set of registers and load another, but a process switch also changes the memory mapping, which typically means the processor’s cache of address translations (the TLB) loses much of its usefulness and has to warm up again.
On Linux the distinction is less sharp than textbooks suggest. Internally, both processes and threads are “tasks”, created by the same underlying system call with different flags saying what to share. A new process created with fork also uses copy-on-write: the child initially shares the parent’s memory pages, and a page is physically copied only when one side writes to it. So process creation on Linux is cheaper than a naive full copy, though still more work than a thread.
Lightweight does not mean free. Each thread needs its own stack. On a 64-bit JVM on Linux, the default thread stack size is typically 1 MB of reserved virtual memory, although only the touched part is backed by physical memory. A server that creates one platform thread per connection and receives 10,000 concurrent connections is asking the operating system to schedule 10,000 threads. Long before memory runs out, the time spent context switching and the pressure on caches starts to dominate. This is the problem that event loops (Node.js, Nginx) and, more recently, Java’s virtual threads (final in Java 21) address in different ways.
Communication: easy versus explicit
Two threads communicate by reading and writing the same objects. No copying, no serialisation. The catch is that you now need synchronisation: locks, atomic variables or concurrent data structures. Forget one, and you get a race condition, such as two threads incrementing a shared counter and losing one of the updates because each read the old value before the other wrote.
Two processes must communicate explicitly through inter-process communication: pipes, sockets, message queues, shared memory segments set up on purpose, or files. This is more work and usually slower per message, but every exchange is visible in the code. You cannot accidentally share a variable between processes, which removes an entire category of bugs.
Real systems that chose differently
Looking at how well-known software made this choice is the fastest way to build intuition.
- PostgreSQL uses a process per client connection. A crash in one backend is contained, and the postmaster can recover the cluster. The price is that each connection is relatively expensive, which is why tools such as PgBouncer are common in front of busy PostgreSQL servers.
- MySQL uses a thread per connection inside a single server process. Connections are cheaper, but the server is one shared memory space.
- Chromium-based browsers run tabs and site content in separate renderer processes. A crashing page takes down one tab rather than the browser, and process boundaries are used as a security barrier between websites.
- Python web servers such as Gunicorn commonly run multiple worker processes, partly because CPython’s global interpreter lock limits how much Python code threads can run in parallel within one process.
- Java application servers such as Tomcat traditionally handle requests on a pool of threads within one JVM, sharing caches and connection pools cheaply.
The pattern is consistent. Where isolation and fault containment matter more, designers choose processes. Where cheap sharing of large in-memory state matters more, they choose threads.
Concurrency is not parallelism
A single-core machine can run many threads concurrently: the scheduler interleaves them, so each makes progress over time. Only multiple cores give true parallelism, with instructions from different threads executing at the same instant. This matters when estimating benefits. If a task waits on the network for 200 milliseconds and computes for 5, threads help even on one core, because one thread can compute while others wait. If a task is pure computation, adding more threads than you have cores mostly adds switching overhead.
A rough illustrative sizing: a service on an 8-core machine doing CPU-heavy image resizing gains little beyond about 8 worker threads. The same machine handling requests that spend 90% of their time waiting on a database could usefully run many more, limited by what the database can absorb rather than by the CPU.
Choosing in your own design
When you are deciding, ask three questions. First, if this unit of work crashes or leaks memory, how much else should die with it? Second, how much data do the units need to share, and how often? Third, how many of them will exist at once? A plugin system running untrusted customer code points firmly to processes or containers. A web server sharing a 2 GB in-memory product cache across request handlers points to threads. Many production systems combine both: several processes for isolation and to use all cores, each running a pool of threads or an event loop for concurrency within.
Written by the RP Strativa Team. Sizing figures in the examples are illustrative.
Leave a Reply