Classical IPC problems
Criteria Dining Bounded Buffer Sleeping Barber Readers-Writers
Philosophers (Producer-
Consumer)
Definition Philosophers Producer adds Barber sleeps if Multiple readers
eat & think, items to a buffer, no customers, can read; writers
but need 2 consumer cuts hair if need exclusive
forks (shared) removes. Buffer someone arrives. access.
— leads to has limited size.
deadlock.
Main Deadlock, Synchronization of Manage sleeping Ensuring writer
Challenge starvation producer and barber and exclusivity while
when trying to consumer access waiting maximizing
pick forks. to buffer. customers. reader
concurrency.
Processes 5 philosophers At least 1 1 barber, multiple Multiple readers,
Involved (threads), 5 producer, 1 customers. multiple writers.
forks (shared consumer, and the
resources). shared buffer.
Shared Forks (critical Circular buffer Barber chair, Shared data (file,
Resources sections). (critical section). waiting room. database, etc.).
Typical Semaphores Semaphores: Semaphores for Semaphores or
Solution (mutex + empty, full, mutex. chairs and barber RW locks;
availability), status. readers' count
monitors. tracking.
Working/Logic Acquire both Producer waits if Customer wakes Readers acquire
forks or wait. buffer full; barber; waits if shared lock;
Ensure no consumer waits if chairs full. Barber writer waits for
deadlock by empty. Mutex for sleeps if no one. all readers to
ordering or exclusive access. finish.
odd-even
strategy.
Advantages Models Very common in Realistic scenario Models
deadlock real-world systems with file/database
scenarios; (OS, printers). sleeping/waking access perfectly.
helps learn logic.
resource
sharing.
Disadvantages Can cause Buffer size and Complex if Writer starvation
starvation, not context switching multiple barbers unless priority is
scalable overhead. or non-FIFO. handled.
without extra
logic.
Starvation? Yes (if not Rare, but can Yes, customers Yes, writer or
handled happen in poorly may leave if no reader starvation
properly). balanced chairs. depending on
production. priority.
Deadlock? Yes — main Possible if Not typical. Can be avoided
issue. semaphores with proper
mishandled. ordering.
Key Concepts Resource Producer- Sleep-wake logic, Reader/writer
Used hierarchy, consumer waiting queue. lock logic,
semaphores, synchronization, prioritization.
deadlock counting
avoidance. semaphores.
Real-World Dining with Shared printer Barber shop with Online document
Analogy shared forks. queue. limited chairs. accessed by
editors/readers.
CLASSIC SYNCHRONIZATION PROBLEM SNIPPETS using semaphore
Dining Philosophers Problem (using semaphores)
// Assume semaphore fork [5] and functions wait () and signal () are already defined
void philosopher (int i) {
while (1) {
think ();
wait(fork[i]) ; //Pick up left fork
wait (fork [(i + 1) % 5]) ; //Pick up right fork
eat(); //Eating
signal(fork[i]); //Put down left fork
signal (fork [(i + 1) % 5]); //Put down right fork
}
}
Bounded Buffer Problem (Producer Code)
// Assume semaphore empty, full, mutex and buffer[] array are defined
void producer () {
while (1) {
int item = produce (); // Produce an item
wait(empty); // Decrease empty count
wait(mutex); // Enter critical section
insert_item(item); // Add item to buffer
signal(mutex); // Exit critical section
signal(full); // Increase full count
}
}
Sleeping Barber Problem (Barber Code)
// Assume semaphore customers, barber, mutex are defined
void barber () {
while (1) {
wait(customers); // Wait for a customer
wait(mutex); // Protect access to waiting room
signal(barber); // Barber is ready
signal(mutex);
cutHair (); // Cut the customer’s hair
}
}
Readers-Writers Problem (Reader Code – First Version)
// Assume semaphore mutex, wrt are defined
// Variable: int readcount = 0;
void reader() {
while (1) {
wait(mutex); // Lock access to readcount
readcount++;
if (readcount == 1)
wait(wrt); // First reader blocks writers
signal(mutex); // Unlock access to readcount
read(); // Reading is performed
wait(mutex);
readcount--;
if (readcount == 0)
signal(wrt); // Last reader allows writers
signal(mutex);
}
}