4 min to read
Understanding Processes and Threads
A comprehensive guide to processes, threads, and their differences
🎯 Overview
Understanding the differences between processes and threads is crucial for software development. Let’s explore these concepts in detail.
💡 Program vs Process
A program is a set of instructions stored on disk, while a process is a running instance of a program. Key differences:
- Program is static, Process is dynamic
- Program is code, Process is code in execution
- Multiple processes can run from the same program
🔄 Process vs Thread
Process
- Independent entity with its own memory space
- Provides isolation and security
- Key concept: Isolation
Thread
- Lightweight unit within a process
- Shares memory with other threads
- Key concept: Concurrency
📊 Comparison Table
🔑 Aspect | 🖥️ Process | 🧵 Thread |
---|---|---|
Definition | Program instance in execution | Smallest unit of programmed instructions |
Memory | Separate memory space | Shared memory within process |
Creation | Resource-intensive | Lightweight |
Communication | Complex (IPC) | Simple (shared memory) |
Control | Controlled by Operating System | Controlled by Process |
Overhead | High | Low |
Use Case | When isolation is needed | For concurrent tasks |
Failure Impact | Failure is isolated | Failure affects other threads |
📈 Process States
1. NEW
- Process being created
2. READY
- Waiting for processor
3. RUNNING
- Currently executing
4. WAITING
- Waiting for event
5. TERMINATED
- Execution completed
🔄 Multi-Threading vs Multi-Processing
Multi-Threading
- Single process, multiple threads
- Shared memory space
- Fast context switching
- Lower resource usage
Multi-Processing
- Multiple processes
- Separate memory spaces
- Slower context switching
- Higher resource usage
🔄 Context Switching
Context Switching is the process where the CPU switches from one process or thread to another. It involves storing the state of the current process and loading the state of the next process.
🔄 Steps in Context Switching:
- Time Out (Preemption):
- The running process (e.g., P1) reaches its time slice limit.
- The Scheduler is notified to switch the running process.
- Saving State:
- The state of P1 (CPU registers, program counter, etc.) is saved to memory.
- This ensures that P1 can resume from where it stopped.
- Loading Next Process State:
- The state of P2 is loaded from memory.
- P2 is now ready to run.
- Dispatching:
- The CPU starts executing P2.
- This cycle repeats for process scheduling.
⚠️ Impact of Context Switching:
- Overhead: Frequent context switching can slow down the system due to time spent saving/loading states.
- Efficiency: Modern schedulers optimize switching to balance performance.
- Multitasking: Enables smooth multitasking by sharing CPU time among processes.
🔍 Example Use Cases:
- Operating systems using Round-Robin Scheduling.
- Switching between foreground and background tasks in apps.
⚡ Hyper-Threading
Intel’s implementation of SMT (Simultaneous Multi-Threading):
1. Physical Core Duplication
- Duplicate register sets
- Manages multiple thread states
2. Shared Resources
- Shared execution engine
- Shared cache
3. Benefits
- Improved efficiency
- Enhanced parallelism
- Better multitasking
Comments