Learning outcomes
- Apply synchronisation techniques to the dining philosophers and sleeping barber problems and outline common IPC mechanisms.
These notes explain two classical synchronization problems — Dining Philosophers and Sleeping Barber — and then outline the major Inter-Process Communication (IPC) models used by operating systems. These problems are important because they capture the real difficulties of mutual exclusion, deadlock, starvation, and coordination among concurrent processes.
In concurrency, several processes or threads may compete for limited resources. If synchronization is not handled properly, the system may suffer from:
Five philosophers sit around a circular table. Between every pair of adjacent philosophers there is one chopstick. Each philosopher alternates between thinking and eating. To eat, a philosopher needs both left and right chopsticks.
Since each chopstick is shared between neighbors, the problem is to design a synchronization scheme that allows philosophers to eat without causing deadlock or starvation.
The Dining Philosophers problem models a situation where:
Suppose each philosopher first picks up the left chopstick and then tries to pick up the right one. If all philosophers do this simultaneously:
Let each chopstick be represented by a binary semaphore.
semaphore chopstick[5] = {1,1,1,1,1};
philosopher(i):
while(true){
think();
wait(chopstick[i]); // left
wait(chopstick[(i+1) % 5]); // right
eat();
signal(chopstick[i]);
signal(chopstick[(i+1) % 5]);
}
This ensures mutual exclusion for each chopstick, but the naive order still allows deadlock.
| Technique | Idea | Effect |
|---|---|---|
| Resource ordering | Force all philosophers to pick chopsticks in a fixed global order. | Breaks circular wait |
| Odd-even rule | Odd philosophers pick right first, even philosophers pick left first. | Reduces deadlock possibility |
| At most four philosophers | Allow only four philosophers to compete at once. | Prevents complete circular hold-and-wait |
| Monitor / waiter solution | A central controller grants permission to eat only when both chopsticks are available. | Cleaner and safer coordination |
A waiter checks whether both required chopsticks are available before allowing a philosopher to take them. If not, the philosopher waits. This prevents the classic deadlock where each philosopher grabs one chopstick and waits forever.
A barber shop has:
The rules are:
The Sleeping Barber problem models:
A correct solution must ensure:
A common semaphore-based solution uses:
customers – counts waiting customersbarber – indicates barber availabilitymutex – protects the shared count of waiting customerssemaphore customers = 0;
semaphore barber = 0;
semaphore mutex = 1;
int waiting = 0;
int chairs = N;
barber_process:
while(true){
wait(customers); // sleep if no customers
wait(mutex);
waiting--;
signal(barber); // barber ready
signal(mutex);
cut_hair();
}
customer_process:
wait(mutex);
if(waiting < chairs){
waiting++;
signal(customers); // wake barber if needed
signal(mutex);
wait(barber); // wait until barber is ready
get_haircut();
}
else{
signal(mutex);
leave_shop();
}
| Aspect | Dining Philosophers | Sleeping Barber |
|---|---|---|
| Main issue | Competing for multiple shared resources | Coordinating clients with a single server |
| Classical risk | Deadlock | Lost wakeups / queue overflow / race conditions |
| Shared structure | Chopsticks | Waiting chairs and barber availability |
| Common tool | Semaphores / monitors | Semaphores / monitors |
Inter-Process Communication (IPC) refers to the mechanisms by which processes exchange data and coordinate their actions.
IPC is necessary because processes often need to:
Processes create or attach to a common memory region and exchange data by reading and writing there directly.
Processes exchange information by sending and receiving messages through the operating system.
| IPC Model | Main idea | Strength | Weakness |
|---|---|---|---|
| Shared Memory | Processes access a common memory area | Fast data exchange | Needs explicit synchronization |
| Message Passing | Processes communicate using send/receive operations | Cleaner isolation | Can be slower due to kernel mediation |
| Mechanism | Use | Typical feature |
|---|---|---|
| Pipes | Unidirectional communication between related processes | Simple byte stream |
| Named Pipes (FIFOs) | Communication between unrelated processes | Persistent OS object |
| Message Queues | Send/receive discrete messages | Structured message transfer |
| Shared Memory | Fast data sharing | Highest speed, but requires semaphores/mutexes |
| Sockets | Communication across same or different machines | Network-capable IPC |
| Signals | Notify a process that an event occurred | Lightweight event notification |
| Semaphores | Synchronization, not bulk data transfer | Controls access and ordering |
| Basis | Shared Memory | Message Passing |
|---|---|---|
| Speed | Usually faster | Usually slower |
| Synchronization need | Must be handled explicitly | Often partly built into send/receive |
| Isolation | Lower | Higher |
| Complexity | Can be tricky due to race conditions | Conceptually cleaner |
The classical way to solve Dining Philosophers and Sleeping Barber is to apply synchronization tools such as:
Dining Philosophers: mainly demonstrates safe allocation of multiple shared resources.
Sleeping Barber: mainly demonstrates synchronization of a bounded waiting queue and a single server.
IPC models: provide the communication framework, while synchronization tools ensure correctness.
| Topic | Main lesson |
|---|---|
| Dining Philosophers | Naive resource acquisition can cause deadlock. |
| Sleeping Barber | Bounded waiting and wakeup coordination need synchronization. |
| IPC | Processes need mechanisms to exchange data and synchronize actions. |
| Shared Memory | Fast but requires explicit synchronization. |
| Message Passing | Cleaner and safer abstraction for communication. |
The Dining Philosophers problem models processes competing for multiple shared resources. If each process acquires resources carelessly, deadlock may occur.
The Sleeping Barber problem models one server, many clients, and a bounded waiting queue. A correct solution must safely coordinate arrivals, waiting chairs, and barber wakeup.
Semaphores, mutexes, monitors, and condition variables are standard synchronization tools used to solve such problems.
IPC models define how processes communicate. The two major models are shared memory and message passing. Common IPC mechanisms include pipes, message queues, shared memory, sockets, signals, and semaphores.