Process vs Thread
Process: An execution environment managed by the OS with its own address space.
Thread: An execution unit inside a process.
Process
- independent address space
- memory is not shared with other processes
- the OS manages its resources
Address Space
Address space is the range of virtual memory addresses that a process can access. Each process has its own address space, which provides isolation between processes.
Why virtual memory?
Virtual memory adds a layer of abstraction between the memory addresses used by a process and the actual physical memory. This allows the OS to manage memory more flexibly while providing each process with its own isolated address space.
Process
↓
Virtual Address
↓
Page Table (OS)
↓
Physical Address
↓
RAM
For example, when the OS creates a process, it gives the process a virtual address space. Later, the OS may move the underlying physical memory pages for optimization or memory management.
Without virtual memory, the process would need to know every physical memory change, which would be impractical.
Virtual memory solves this problem. The process only uses virtual addresses, and the OS updates the page table to map those virtual addresses to the correct physical memory locations.
Thread
- shares the same memory attached to the process
- uses the same address space
- lightweight
Why lightweight?
Threads are cheaper because they share the same address space as the process. Creating a new process requires creating a new address space and page tables, while creating a thread only requires allocating a stack and register context.
Summary
A process is an independent execution environment managed by the OS with its own address space. A thread is a unit of execution inside a process. Multiple threads in the same process share the same memory space, while processes are isolated and communicate through IPC.