What is Concurrency Control? Explain with clanical problem.
Concurrency Control in Operating Systems (with a Classical Problem)
Concurrency control in operating systems is the set of techniques used to manage multiple processes or threads running at the same time, so they access shared resources safely and correctly. It prevents issues like race conditions, deadlocks, and starvation by enforcing rules such as mutual exclusion and proper synchronization.
Why Concurrency Control Is Needed
- To avoid race conditions when processes access shared data.
- To ensure mutual exclusion (only one process enters a critical section at a time).
- To guarantee progress (some process will make progress if the critical section is free).
- To provide bounded waiting (no process waits forever).
- To prevent deadlock, livelock, and starvation.
Key Challenges
- Race condition: Output depends on unpredictable execution order.
- Deadlock: Two or more processes wait forever for resources locked by each other.
- Starvation: A process never gets a chance to execute its critical section.
- Livelock: Processes keep changing state but make no progress.
- Priority inversion: A low-priority task holds a resource needed by a high-priority task.
Common Concurrency Control Techniques
- Locks/Mutexes: Provide mutual exclusion to critical sections.
- Semaphores (binary and counting): Synchronize access and track resource availability.
- Monitors and condition variables: High-level synchronization with wait/signal on conditions.
- Atomic operations: Test-and-set, compare-and-swap for building lock primitives.
- Spinlocks: Busy-wait locks used in kernels or short critical sections.
- Message passing: Communicate via send/receive without shared memory.
Classical Problem: Producer–Consumer (Bounded Buffer Problem)
Problem statement: One or more producers generate items and place them into a finite buffer. One or more consumers remove items from that buffer. We must prevent the producer from inserting into a full buffer and the consumer from removing from an empty buffer, while ensuring mutual exclusion on the buffer.
Idea: Use a mutex for mutual exclusion, and two counting semaphores:
- empty: counts free slots in the buffer (initially buffer size N)
- full: counts filled slots (initially 0)
- mutex: binary semaphore to protect the buffer (initially 1)
Pseudocode Using Semaphores
// Shared state
const N = buffer_size
semaphore empty = N // how many free slots
semaphore full = 0 // how many filled slots
semaphore mutex = 1 // mutual exclusion for the buffer
buffer B[N]
// Producer thread
procedure PRODUCER()
loop forever
item = produce_item()
wait(empty) // ensure there is space
wait(mutex) // enter critical section
insert item into B
signal(mutex) // leave critical section
signal(full) // one more item available
end loop
// Consumer thread
procedure CONSUMER()
loop forever
wait(full) // ensure there is an item
wait(mutex) // enter critical section
item = remove item from B
signal(mutex) // leave critical section
signal(empty) // one more free slot
consume_item(item)
end loop
Why This Works
- Safety: The mutex ensures only one thread manipulates the buffer at a time.
- No overflow/underflow: The empty and full semaphores block producers when the buffer is full and consumers when it is empty.
- Progress: Whenever a slot is freed or filled, the corresponding semaphore is signaled, unblocking waiting threads.
- Bounded waiting: With fair semaphore scheduling, producers and consumers are served without indefinite delay.
Other Classical Synchronization Problems
- Readers–Writers: Multiple readers can access a shared database together, but writers need exclusive access. Focuses on avoiding starvation and balancing read/write priority.
- Dining Philosophers: Models resource allocation with circular wait; highlights deadlock, starvation, and the need for ordering or resource hierarchy.
- Sleeping Barber: Manages a waiting room and a barber; demonstrates synchronization between producer-like (customers) and consumer-like (barber) roles.
Quick Exam Pointers
- Define concurrency control and its goals: correctness, mutual exclusion, progress, bounded waiting.
- List issues: race condition, deadlock, starvation.
- Mention tools: semaphores, mutexes, monitors.
- Explain one classical problem (e.g., Producer–Consumer) with semaphore-based logic.
