Definition:
- A Process is an independent program in execution with its own memory space; a Thread is a lighter unit of execution inside a process that shares the process’s memory
- A process has one or more threads; threads of the same process share its address space
Memory & isolation:
- Process: own address space (code, data, heap, stack) and own file descriptors — isolated from other processes
- Thread: shares code, data, heap and open files with sibling threads; has its own registers and stack
- Isolation: a crash in one process doesn’t kill others; a crash in one thread can take down the whole process
Communication:
- Between processes: needs Inter-Process Communication (pipes, sockets, shared memory, message queues) — relatively slow
- Between threads: shared memory, just read/write the same variables — fast, but needs synchronization (Mutex Lock, Semaphore) to avoid race conditions and Deadlock
Cost:
- Process creation / context switch: expensive (new address space, more state to save and restore)
- Thread creation / context switch: cheap (shared address space)
Process vs Thread (summary):
- Owns memory: process yes (private) ; thread no (shared)
- Creation cost: process high ; thread low
- Context switch: process slow ; thread fast
- Communication: process via IPC ; thread via shared memory
- Fault isolation: process strong ; thread weak (shared fate)
- Parallelism: both — see Thread Level Parallelism, Parallel Programming
Python note (GIL):
- CPython’s Global Interpreter Lock (GIL) lets only one thread execute Python bytecode at a time
- threads → good for I/O-bound work (waiting on I/O releases the GIL)
multiprocessing(processes) → needed for CPU-bound parallelism (sidesteps the GIL)