How a process differs from a thread
A process is a running program together with the resources it needs. A thread is one line of execution inside it. The idea to keep: a process owns its memory, and the threads inside it share that memory.
The short version
The operating system creates a process when a program starts. The process gets its own virtual address space, its code, open handles, a security context and a process ID, and it begins with one thread. That thread is what the system schedules to run. A program can start more threads. They share the process's address space and system resources, and each one has its own stack and registers.
Step by step
- The system creates a process with its own address space and one primary thread.
- The program starts more threads when it wants work to run side by side.
- All threads in the process see the same heap and the same open files.
- Each thread has its own stack and registers, so each one tracks its own place in the code.
- A second process gets a separate memory space: after a fork, a write in one does not appear in the other.
| Process | Thread | |
|---|---|---|
| Memory | Its own address space | Shares the address space of its process |
| Stack and registers | One set per thread | Its own |
| Open files | Belong to the process | Shared by all threads of that process |
| Scheduled by the system | Through its threads | Yes, the unit that gets scheduled |
Why it matters
My reading: shared memory makes handing data between threads easy, and it also means one thread's write is visible to all the others. Separate processes cannot overwrite each other's memory.
Sources
- About processes and threads — what a process owns, the primary thread, threads sharing the address space and system resources, thread context (registers and stack)
- pthreads(7) — threads share global memory (data and heap), process ID and open file descriptors; each thread has its own stack
- fork(2) — parent and child run in separate memory spaces; a write by one does not affect the other