BCS401 Operating System Semester IV · AY 2025-26 onward
Unit 2 · Concurrent Processes

Lecture 9: Dining philosopher, sleeping barber & IPC models

Learning outcomes

  • Apply synchronisation techniques to the dining philosophers and sleeping barber problems and outline common IPC mechanisms.
Lecture Notes

Main content

Dining Philosophers, Sleeping Barber, and IPC Models

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.

Synchronization Semaphores Dining Philosophers Sleeping Barber IPC Shared Memory Message Passing

1. Why These Problems Matter

In concurrency, several processes or threads may compete for limited resources. If synchronization is not handled properly, the system may suffer from:

  • Race conditions – incorrect results due to uncontrolled interleaving
  • Deadlock – processes wait forever for one another
  • Starvation – some processes wait indefinitely while others keep progressing
  • Busy waiting – CPU time is wasted while a process repeatedly checks a condition
Key idea: Classical synchronization problems are not just puzzles. They model real operating-system situations involving shared resources, waiting queues, and coordination.

2. Dining Philosophers Problem

2.1 Problem statement

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.

P0 P1 P2 P3 P4 Dining Table
Figure 1: Dining philosophers compete for shared chopsticks.

2.2 What this problem models

The Dining Philosophers problem models a situation where:

  • processes compete for limited resources,
  • each process needs more than one resource,
  • resources are shared and mutually exclusive,
  • poor synchronization may cause deadlock or starvation.

2.3 Naive solution and deadlock

Suppose each philosopher first picks up the left chopstick and then tries to pick up the right one. If all philosophers do this simultaneously:

  • each philosopher holds one chopstick,
  • each waits for the other chopstick,
  • nobody can proceed.
Deadlock case: Everyone holds one resource and waits for another. The dinner becomes a philosophical conference with no eating.

2.4 Semaphore-based solution idea

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.

2.5 Common techniques to avoid 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

Safe waiter-based idea

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.

2.6 What to remember

  • Dining philosophers demonstrates mutual exclusion and deadlock risk.
  • Naive semaphore use is not enough.
  • A good solution must aim to avoid deadlock and reduce starvation.

3. Sleeping Barber Problem

3.1 Problem statement

A barber shop has:

  • one barber,
  • one barber chair,
  • a number of waiting chairs for customers.

The rules are:

  • If there are no customers, the barber sleeps.
  • If a customer arrives and the barber is sleeping, the customer wakes the barber.
  • If the barber is busy and a waiting chair is free, the customer waits.
  • If all waiting chairs are occupied, the arriving customer leaves.
Barber Shop Waiting Chairs Barber Chair Barber Sleeping if no customers
Figure 2: Customers wait if chairs are free; otherwise they leave; the barber sleeps when idle.

3.2 What this problem models

The Sleeping Barber problem models:

  • a single server,
  • many competing clients,
  • a bounded waiting queue,
  • sleep/wakeup synchronization.

3.3 Synchronization requirements

A correct solution must ensure:

  • the barber does not work when no customer exists,
  • customers do not occupy more waiting chairs than available,
  • barber and customers coordinate correctly,
  • the shared customer count is updated safely.

3.4 Semaphore-based model

A common semaphore-based solution uses:

  • customers – counts waiting customers
  • barber – indicates barber availability
  • mutex – protects the shared count of waiting customers
semaphore 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();
}

3.5 Key idea of the solution

  • customers semaphore wakes the barber when someone arrives.
  • barber semaphore lets one customer move to the barber chair.
  • mutex prevents race conditions on the waiting count.
Core lesson: The Sleeping Barber problem is really about coordinating a bounded queue with a single service provider.

4. Dining Philosophers vs Sleeping Barber

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

5. IPC Models

5.1 Meaning of IPC

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:

  • share information,
  • coordinate tasks,
  • signal events,
  • maintain consistency of shared resources.
Main idea: IPC is about both communication and synchronization.

5.2 Two major IPC models

Shared-Memory Model

Processes create or attach to a common memory region and exchange data by reading and writing there directly.

Message-Passing Model

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

5.3 Common IPC mechanisms

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

5.4 Shared memory vs message passing

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
Important: Shared memory is fast, but without proper synchronization it becomes a race-condition factory.

6. Applying Synchronization Techniques

The classical way to solve Dining Philosophers and Sleeping Barber is to apply synchronization tools such as:

  • Binary semaphores / mutexes – for mutual exclusion
  • Counting semaphores – for resource counts or waiting customers
  • Monitors – for high-level structured synchronization
  • Condition variables – for waiting until a condition becomes true

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.

7. Quick Revision Snapshot

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.

8. One-Page Summary

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.

One-Page Summary

9. Likely University Questions

  1. Explain the Dining Philosophers problem and discuss how deadlock can be avoided.
  2. Explain the Sleeping Barber problem with semaphore-based synchronization.
  3. Differentiate between Dining Philosophers and Sleeping Barber problems.
  4. What is IPC? Why is it needed in operating systems?
  5. Compare shared-memory IPC and message-passing IPC.
  6. Explain pipes, message queues, shared memory, signals, and sockets as IPC mechanisms.
  7. How are semaphores used in synchronization problems?