Advanced Process and Thread
Management
1. Multithreading Models
Multithreading allows multiple threads to exist within the context of a single process,
sharing resources while running independently.
Models:
1. Many-to-One Model
- Many user threads mapped to a single kernel thread.
- Pros: Simple, efficient thread management.
- Cons: Entire process blocks if one thread blocks; limited parallelism.
Diagram:
[User Threads] → [1 Kernel Thread]
2. One-to-One Model
- Each user thread maps to a kernel thread.
- Pros: True concurrency on multi-core systems.
- Cons: Overhead of creating many kernel threads.
Diagram:
[User Thread] ↔ [Kernel Thread]
3. Many-to-Many Model
- Many user threads mapped to many kernel threads.
- Pros: Balances flexibility and concurrency.
- Cons: More complex to implement.
Diagram:
[User Threads] ↔ [Kernel Threads]
2. Thread Pools
A collection of pre-created threads that wait for tasks.
- Improves performance by reducing thread creation/destruction overhead.
Process:
1. Tasks arrive in a queue.
2. Available thread picks up a task.
3. On completion, the thread returns to pool.
Diagram:
[Task Queue] → [Thread Pool] → [CPU Execution]
3. Context Switching
Switching CPU from one process/thread to another.
Involves saving the state (registers, program counter, etc.) of the old process and loading
the state of the new one.
Diagram:
[Process A State Saved] → [CPU Switch] → [Process B State Restored]
Overhead: Time lost in switching, not in execution.
4. Synchronization Issues & Solutions
Issues:
- Race Condition: Two threads access shared data concurrently → inconsistent result.
- Deadlock: Threads wait indefinitely for resources held by each other.
- Starvation: Low-priority thread never gets CPU.
Solutions:
1. Semaphores
- Integer variable used for signaling.
- Types:
* Binary Semaphore (0/1, like a lock)
* Counting Semaphore (allows multiple resources)
Example:
wait(S) { while S <= 0 ; S--; }
signal(S) { S++; }
2. Monitors
- High-level abstraction with mutual exclusion + condition variables.
- Encapsulates shared variables, procedures, and synchronization.
3. Lock-Free Data Structures
- Uses atomic operations (CAS – Compare-And-Swap).
- Avoids blocking, better performance in multicore systems.
5. CPU Scheduling in Multi-Core Systems
Traditional Scheduling: One CPU, one ready queue.
Multi-Core Challenges:
- Core Affinity (keeping a thread on the same core improves cache performance).
- Load Balancing across cores.
Algorithms:
- Symmetric Multiprocessing (SMP): All cores share ready queue.
- Asymmetric Multiprocessing: One core is master, schedules for others.
Diagram:
[Ready Queue] → {Core1, Core2, Core3}
6. Activity: CPU Scheduler Simulation for Multi-Core Systems
Objective: Simulate scheduling on multi-core CPU.
Steps:
1. Create a ready queue of processes with burst times & priorities.
2. Assign processes to available cores.
3. Apply scheduling algorithm (e.g., Round Robin, Priority, FCFS).
4. Observe execution, context switches, and waiting times.
Example (2-core, Round Robin):
Time 0-2: Core 1 → P1, Core 2 → P2
Time 2-4: Core 1 → P3, Core 2 → P4 ...