Operating SystemUnit 210 min read
Processes & Threads: Definitions, States, Hierarchies & Multithreading
Unit 2 of Operating System: Covers process vs. thread definitions, lifecycle states, creation/deletion, inter-process communication (IPC), thread models (user/kernel), and real-world applications in banking, e-commerce, and mobile apps with visual comparisons and exam-focused examples.
TAKEAWAYS:
- A process is an instance of a program in execution with its own memory space, while a thread shares memory but runs independently within a process.
- Processes pass data via IPC (pipes, message queues, shared memory), while threads use shared variables for communication.
- Threads reduce overhead compared to processes but introduce synchronization challenges (race conditions, deadlocks).
- Multithreading improves responsiveness in GUI apps (e.g., WhatsApp background sync) and parallelism in servers (e.g., Daraz order processing).
- The process state diagram (New → Ready → Running → Waiting → Terminated) explains CPU allocation and blocking.
- Thread models (user-level vs. kernel-level) differ in scheduling control and context-switching cost.
1. Processes: Definition and Characteristics
A process is a program in execution with its own address space, resources (CPU, memory, I/O), and execution state. It is the basic unit of work in an OS.
1.1 Process States and Transitions
The lifecycle of a process is modeled as a finite state machine with five key states:
stateDiagram-v2
[*] --> New: Process Creation
New --> Ready: Admitted to Ready Queue
Ready --> Running: CPU Allocation
Running --> Waiting: I/O or Event Block
Waiting --> Ready: Event Occurs
Running --> Ready: Time Slice Expires
Running --> Terminated: Exit/Abort
Terminated --> [*]Key Transitions:
- New: Process is created but not yet ready to run (e.g., when a user opens Notepad).
- Ready: Process is loaded in memory but waiting for CPU (e.g., background apps in Windows Task Manager).
- Running: Process is executing (e.g., a video playing in YouTube).
- Waiting/Blocked: Process waits for an event (e.g., a file read operation in a text editor).
- Terminated: Process completes or is killed (e.g., closing a Chrome tab).
Worked Example: Consider a bank loan processing system:
- New: Loan application submitted (not yet processed).
- Ready: Application validated (waiting for approval).
- Running: Loan officer processes the request (CPU-bound task).
- Waiting: Credit check in progress (I/O-bound task).
- Terminated: Loan approved/rejected.
2. Process Control Block (PCB)
The PCB is a data structure that stores process metadata. It includes:
- Process ID (PID)
- Process State (New, Ready, Running, etc.)
- Program Counter (PC): Next instruction to execute.
- CPU Registers: Accumulator, stack pointer, etc.
- CPU Scheduling Info: Priority, pointers to queues.
- Memory Management Info: Page tables, segment tables.
- I/O Status Info: Open files, devices.
- Accounting Info: CPU time used, process owner.
3. Process Creation and Termination
3.1 Process Creation
A new process is created via:
- System calls (
fork(),exec()in Unix). - Parent-child hierarchy (e.g., a shell spawning a program).
- Batch processing (e.g., NTC’s automated billing system).
Example in Linux:
# Parent process (PID 1234) creates a child (PID 5678)
fork() → exec("ls") → Child runs "ls" command
3.2 Process Termination
A process terminates when:
- It completes execution (normal exit).
- It is killed by the OS (e.g.,
kill -9in Linux). - It fails (e.g., segmentation fault).
- Its parent terminates (unless it’s a daemon).
Worked Example (eSewa Payment):
- User initiates payment → New (eSewa process created).
- System validates credentials → Ready (waits for DB query).
- Payment processed → Running (CPU-bound transaction).
- Bank confirmation received → Waiting (awaits response).
- Transaction completes → Terminated (receipt generated).
4. Inter-Process Communication (IPC)
Since processes have isolated memory spaces, they communicate via IPC mechanisms:
| IPC Method | Description | Example Use Case | Pros | Cons |
|---|---|---|---|---|
| Pipes | Unidirectional byte stream (FIFO). | Parent-child data transfer (e.g., grep | sort). |
Simple, fast. | Limited to related processes. |
| Message Queues | Kernel-managed queues for messages. | Daraz order dispatch system. | Decoupled communication. | Slower than shared memory. |
| Shared Memory | Processes map to same memory region. | High-frequency trading systems. | Low latency, high speed. | Requires synchronization (semaphores). |
| Sockets | Network-based communication. | WhatsApp messaging (TCP/IP). | Works across machines. | Complex setup. |
| Semaphores | Synchronization tool (binary/mutex). | Bank account transactions (avoid race conditions). | Prevents deadlocks. | Requires careful design. |
Worked Example (Khalti Payment Gateway):
- Shared Memory: Multiple Khalti servers share transaction logs for fast updates.
- Message Queues: Order confirmation messages sent to Daraz’s inventory system.
5. Threads: Lightweight Processes
A thread is a subunit of a process that shares:
- Memory space (heap, global variables).
- File descriptors.
- Process ID (PID), but has its own Thread ID (TID).
5.1 Thread States
Threads have similar states but with faster context switching (no memory reload):
stateDiagram-v2
[*] --> New: Thread Creation
New --> Ready: Thread Ready
Ready --> Running: CPU Allocation
Running --> Blocked: I/O Wait
Blocked --> Ready: Event Occurs
Running --> Terminated: Exit
Terminated --> [*]5.2 Thread Models
| Model | Description | Example OS | Pros | Cons |
|---|---|---|---|---|
| User-Level Threads | Managed by user-space libraries (e.g., POSIX threads). | Java (Green Threads). | Fast context switch, no OS overhead. | No true parallelism (blocking call stalls all threads). |
| Kernel-Level Threads | Managed by the OS (e.g., Linux threads). | Windows, Linux. | True parallelism, preemptive scheduling. | Slower context switch (kernel involvement). |
| Hybrid Threads | Combines user and kernel threads. | Solaris. | Balances performance and parallelism. | Complex implementation. |
6. Multithreading in Real-World Systems
6.1 Example 1: WhatsApp (Mobile App)
- Main Thread: Handles UI updates (e.g., displaying messages).
- Background Threads:
- Network Thread: Fetches messages (non-blocking).
- Media Thread: Downloads images/videos.
- Why? Prevents app freeze during downloads.
6.2 Example 2: Daraz Order Processing
- Order Thread: Processes customer requests (CPU-bound).
- Inventory Thread: Updates stock (I/O-bound).
- Payment Thread: Handles Khalti/Ncell payments.
- Why? Parallel processing reduces wait time.
6.3 Example 3: NTC Billing System
- User Authentication Thread: Validates login.
- Billing Calculation Thread: Computes charges.
- Database Thread: Updates records.
- Why? Isolates failures (e.g., DB crash doesn’t halt authentication).
7. Thread Synchronization Challenges
Multithreading introduces race conditions, deadlocks, and starvation. Solutions include:
- Mutexes (Mutual Exclusion): Locks critical sections (e.g., bank balance updates).
- Semaphores: Controls access to shared resources (e.g., printer queues).
- Monitors: High-level synchronization (e.g., Java’s
synchronizedblocks).
Worked Example (Nepal Stock Exchange - NEPSE):
- Problem: Two traders try to buy the same stock simultaneously → race condition.
- Solution: Use a mutex lock on the stock record to ensure atomic updates.
8. Process vs. Thread Comparison
| Feature | Process | Thread |
|---|---|---|
| Memory Space | Separate (isolated). | Shared (same as parent process). |
| Creation Overhead | High (loads entire program). | Low (only stack and registers). |
| Context Switching | Slow (OS saves entire process state). | Fast (only thread-specific data). |
| Communication | IPC (slow). | Shared memory (fast). |
| Isolation | High (crash doesn’t affect others). | Low (crash affects entire process). |
| Use Case | Independent tasks (e.g., Chrome tabs). | Parallel subtasks (e.g., video encoding). |
In the Real World
eSewa Payments
- Uses multithreading to handle:
- User authentication (Thread 1).
- Bank transaction processing (Thread 2).
- SMS notification (Thread 3).
- Why? Ensures fast response even during peak hours (e.g., Dashain).
- Uses multithreading to handle:
Pathao Ride Booking
- Processes:
- Driver location tracking (separate process).
- Payment processing (separate process).
- Threads:
- Real-time GPS updates (Thread A).
- Route optimization (Thread B).
- IPC: Message queues coordinate between processes.
- Processes:
NTC’s Automated Billing
- Shared Memory: All billing servers access the same customer database.
- Semaphores: Prevent double-charging when users retry failed payments.
Exam Tip
Define Clearly:
- "A process is an instance of a program in execution with its own address space."
- "A thread is a lightweight subprocess that shares memory with its parent."
State Diagrams:
- Draw the process lifecycle (New → Ready → Running → Waiting → Terminated) in exams.
- Label transitions (e.g., "I/O Request" → Waiting).
IPC vs. Thread Communication:
- Processes → Use pipes, message queues, or sockets.
- Threads → Use shared variables + semaphores.
Real-World Applications:
- Relate multithreading to WhatsApp, Daraz, or eSewa.
- Explain why threads reduce latency in GUI apps.
Common Pitfalls:
- Don’t confuse process creation (
fork()) with execution (exec()). - User-level threads vs. kernel threads: Know the trade-offs.
- Race conditions → Always mention mutexes/semaphores as solutions.
- Don’t confuse process creation (
Numerical Problems:
- If asked about context-switching time, compare:
- Process switch: 1000 µs.
- Thread switch: 10 µs (100x faster).
- If asked about context-switching time, compare:
Based on the TU BIM syllabus for Operating System (IT241), unit 2.
Discussion
Loading…