>[!quote] In a Nutshell >- Processes are isolated and have their own memory, making them suitable for running separate applications. >- Threads are lightweight and share resources, making them ideal for concurrent tasks within the same application. --- #### Process >[!info] Process >A process is an independent program in execution. It has its **own memory** space, resources, and a unique Process ID (PID). They are used to run independent applications (e.g., a browser and a text editor). Characteristics are ... >- **Heavyweight**: Requires significant resources to start and maintain. >- **Isolated**: Processes do not share memory by default. >- Can consist of multiple threads. >- Context switching between processes is relatively expensive. - **Key Lingo:** - Program Code: The set of instructions to execute. - Process Memory: Includes stack, heap, and data sections. - Process Control Block (PCB): Stores process-specific information (e.g., PID, state, priority). - **Lifecycle of a Process:** 1. New: The process is created. 2. Ready: Waiting for CPU allocation. 3. Running: Instructions are being executed. 4. Waiting: Blocked, waiting for resources or events. 5. Terminated: Process has completed execution. --- #### Thread >[!info] Thread >A thread is the smallest unit of execution within a process. Multiple threads can exist within a single process, **sharing the same memory space**. They are used to perform concurrent tasks in a single application (e.g., multiple tabs in a web browser). Characteristics are ... >- Lightweight: Threads share memory and resources of their parent process. > - Shared Resources: Threads within a process share code, data, and files. > - Fast Context Switching: Less overhead compared to processes. > >**[[Multithreading]]** involves running multiple threads concurrently within a single process, see [[Multithreading]] note. - **Key Lingo:** - Thread ID - Program Counter - Registers - Stack (separate for each thread) - **Types of Threads:** - User Threads: Managed by user-level libraries. - Kernel Threads: Managed directly by the operating system. - Daemon Thread: runs in the background, does not block program from exiting. usually not critical for main programs execution flow - **Lifecycle of a Thread:** Similar to processes: New, Ready, Running, Waiting, Terminated. --- #### Comparison of Threads and Processes | Aspect | Process | Thread | | ---------------------- | ---------------------------------------------------- | ----------------------------------------------------------------- | | Definition | Independent program in execution | Lightweight unit within a process | | Memory | Each process has its own memory | Threads share memory within a process | | Overhead | Higher overhead | Lower overhead | | Communication | Inter-process communication (IPC) needed | Direct memory sharing within process | | Fault Isolation | Strong: Failure in one process doesn’t affect others | Weak: Failure in one thread can affect others in the same process | | Context Switching Time | Higher | Lower |