What a running program really is: processes, threads and the memory they use.
Every server you design runs on an operating system (OS). A few OS ideas explain a lot of system design: what a process is, what a thread is, and how memory is used.
A program is a file on disk. When you start it, the OS creates a process: a running copy with its own memory space, its own open files, and at least one thread.
A thread is the smallest unit of work the OS can schedule on a CPU core. A process can have one thread or thousands. Threads of the same process see the same heap, but each has its own stack for its function calls and local variables.
| Process | Thread | |
|---|---|---|
| Memory | Its own private address space | Shares the heap of its process; has its own stack |
| Cost to create | Heavier | Lighter |
| Cost to switch | Higher (new address space) | Lower (same address space) |
| Talking to each other | Needs IPC: pipes, sockets, shared memory | Just read and write shared variables |
| If one crashes | Others usually survive | Can take down the whole process |
| Main risk | Overhead and slow sharing | Race conditions and deadlocks |
| Region | What lives there | Who sees it | How it fails |
|---|---|---|---|
| Code | Program instructions | All threads | Rarely changes |
| Heap | Objects and data created at run time | All threads of the process | Leaks (never freed), fragmentation, races |
| Stack | Function calls and local variables | One thread only | Stack overflow from very deep recursion |
A teaching model with a fixed order of operations (no random numbers). The real CPU can switch between threads at almost any instruction, and the exact costs depend on the OS and hardware, so only the comparison (lower or higher) is shown here.
| Term | Meaning in easy words |
|---|---|
| Program | A file of instructions on disk. It is not running yet. |
| Process | A running program, with its own memory and resources |
| Thread | A line of execution inside a process. Threads share the process’s heap. |
| Heap | Memory for data created at run time, shared by the threads of a process |
| Stack | Memory for function calls and local variables. Each thread has its own. |
| Context switch | The OS pauses one thread or process and starts another. It costs a little time. |
| Scheduler | The part of the OS that decides what runs on which core, and when |
| Virtual memory | Each process sees its own private memory. The OS maps it to real RAM. |
| Race condition | A bug where the result depends on the exact timing of threads |
| Mutex (lock) | A tool that lets only one thread into a section of code at a time |
| IPC | Inter-process communication: ways for processes to exchange data (pipes, sockets, shared memory) |
| Choice | We gain | We lose |
|---|---|---|
| Threads | Fast sharing, low overhead | Risk of races, one crash can kill all |
| Processes | Isolation and safety | More memory, slow sharing |
| Few workers | Low memory and little switching | CPU idle while others wait |
| Many workers | Hide waiting on I/O | More memory and context switching |
| Locks | Correct shared data | Waiting, and a risk of deadlock |
| No sharing (message passing) | Fewer races | Copying data and a different design |