Unit 2: Concurrent Processes
Concurrent Processes
Process Concept
A process is a program in execution.
Simple explanation
When a program is running, it becomes a process.
Components of a Process
- Program code
- Data
- Stack
- Program Counter (PC)
- Registers
Process States
| State | Description |
|---|---|
| New | Process is being created |
| Ready | Waiting for CPU |
| Running | Executing on CPU |
| Waiting | Waiting for I/O |
| Terminated | Finished execution |
Principle of Concurrency
Concurrency means multiple processes execute at the same time.
Simple explanation: Many programs share the CPU by taking turns very fast.
Why Concurrency is Needed
- Better CPU utilization
- Faster response
- Resource sharing
Problem with Concurrency
- Data inconsistency
- Race conditions
- Deadlocks
Producer–Consumer Problem
A classic synchronization problem involving two processes.
Simple explanation
- Producer produces data
- Consumer consumes data
- Both share a buffer
Issues
- Producer should not produce if buffer is full
- Consumer should not consume if buffer is empty
Need
Proper synchronization is required to avoid data errors.
Mutual Exclusion
Mutual exclusion ensures only one process accesses shared resource at a time.
Simple explanation: If one process is using shared data, others must wait.
Example: Only one person can use a bathroom at a time.
Critical Section Problem
The critical section is the part of code that accesses shared resources.
Requirements of Critical Section Problem
| Condition | Description |
|---|---|
| Mutual Exclusion | Only one process allowed |
| Progress | Decision cannot be delayed |
| Bounded Waiting | Limited waiting time |
Dekker’s Solution
One of the first software solutions for mutual exclusion.
Characteristics
-
Works for two processes
Uses two shared variables:
flag[]- turn
Simple explanation: Processes take turns entering critical section.
Limitations
- Complex logic
- Works only for two processes
Peterson’s Solution
An improved software solution for mutual exclusion.
Characteristics
-
Works for two processes
Uses:
flag[2]- turn
Simple explanation: Process declares interest and gives priority to the other.
Advantages
- Simple and correct
- Satisfies all critical section requirements
Limitations
- Works only for two processes
- Busy waiting
Semaphores
A semaphore is a synchronization tool using an integer variable.
Types of Semaphores
| Type | Description |
|---|---|
| Binary Semaphore | 0 or 1 (Mutex) |
| Counting Semaphore | Any integer value |
Semaphore Operations
- wait() / P() → Decrease value
- signal() / V() → Increase value
Simple explanation
Controls access to shared resources using signals.
Semaphore Example (Producer–Consumer)
empty– counts empty slotsfull– counts filled slotsmutex– ensures mutual exclusion
Test-and-Set (Test)
A hardware-based solution for mutual exclusion.
Simple explanation
A special machine instruction that:
- Tests a memory value
- Sets it atomically (in one step)
Characteristics
- Prevents race conditions
- Uses busy waiting
Advantages
- Simple
- Fast
Disadvantages
- Wastes CPU time
- Starvation possible
Summary Table
| Topic | Key Point |
|---|---|
| Process | Program in execution |
| Concurrency | Multiple processes at same time |
| Producer–Consumer | Shared buffer problem |
| Mutual Exclusion | One process at a time |
| Critical Section | Code accessing shared data |
| Dekker | Software solution (2 processes) |
| Peterson | Improved software solution |
| Semaphore | Synchronization tool |
| Test-and-Set | Hardware solution |
Set Operation (in Operating Systems)
In Operating Systems, set operations are mainly used in resource allocation, scheduling, and deadlock handling.
Common Set Operations
| Operation | Meaning |
|---|---|
| Union (A ∪ B) | Combine two sets |
| Intersection (A ∩ B) | Common elements |
| Difference (A − B) | Elements in A not in B |
| Subset | One set contained in another |
Simple Explanation
- Processes and resources are often represented as sets
- OS applies set operations to analyze resource usage
Example
- Set of running processes
- Set of available resources
Classical Problems in Concurrency
Classical problems explain synchronization and resource-sharing issues.
Dining Philosopher Problem
A classic problem that demonstrates deadlock and starvation.
Problem Description
- Five philosophers sit around a circular table
- Each philosopher needs two chopsticks to eat
- One chopstick between each philosopher
Rules
- Philosopher can think or eat
- Must pick up both chopsticks to eat
Problem
-
If all philosophers pick up one chopstick, deadlock occurs
Issues Highlighted
- Deadlock
- Resource starvation
Solutions
- Limit number of philosophers
- Use semaphores
- Pick up both chopsticks at once
Sleeping Barber Problem
Demonstrates process synchronization and waiting problems.
Problem Description
- One barber
- One barber chair
- Limited waiting chairs
Rules
-
If no customers → barber sleep
If customer arrives:
- If barber sleeping → wake up
- If chairs full → customer leaves
Issues Highlighted
- Synchronization
- Deadlock avoidance
- Queue management
Solutions
- Semaphores
- Mutex locks
Inter-Process Communication (IPC)
IPC allows processes to exchange data and coordinate actions.
Simple Explanation
Processes cannot directly share memory. IPC provides safe communication methods.
IPC Models
(A) Shared Memory Model
| Feature | Description |
|---|---|
| Memory | Shared between processes |
| Speed | Fast |
| Control | Programmer manages synchronization |
Example
-
Producer–Consumer problem
(B) Message Passing Model
| Feature | Description |
|---|---|
| Communication | Send & receive messages |
| Memory | No shared memory |
| Speed | Slower than shared memory |
Example
-
Client–Server systems
IPC Schemes
Common IPC Schemes
| Scheme | Description |
|---|---|
| Pipes | One-way communication |
| Named Pipes (FIFO) | Two-way communication |
| Message Queues | Messages stored in queue |
| Shared Memory | Shared address space |
| Semaphores | Synchronization |
| Sockets | Network communication |
Process Generation
Process generation refers to creation of new processes.
Simple Explanation: A running process can create another process.
Parent and Child Process
- Parent process → creates child
- Child process → executes independently
Example
-
UNIX
fork()system call
Methods of Process Generation
| Method | Description |
|---|---|
| fork() | Creates a copy of process |
| exec() | Replaces process code |
| wait() | Parent waits for child |
Reasons for Process Generation
- Multitasking
- Parallel execution
- Better performance
Summary Table
| Topic | Key Point |
|---|---|
| Set Operation | Used for resource analysis |
| Dining Philosopher | Deadlock problem |
| Sleeping Barber | Synchronization problem |
| IPC Models | Shared memory & message passing |
| IPC Schemes | Pipes, queues, semaphores |
| Process Generation | Creating child processes |