Dining Philosophers without Deadlock

The Dining Philosophers problem is a classic synchronization problem in computer science. It involves a number of philosophers seated around a table, alternating between thinking and eating. The challenge is to devise a strategy that allows all philosophers to eat without facing a deadlock (where everyone is waiting and no one can proceed).

Using monitors for solving the Dining Philosophers problem can avoid deadlocks that are common with semaphore-based solutions. Here's how a monitor-based solution can be structured:

Monitor Structure

  1. Shared Resources: Each philosopher needs access to two forks (or chopsticks) to eat. These forks are the shared resources.

  2. States: Each philosopher can be in one of three states: thinking, hungry (wants to eat but doesn’t have forks), or eating.

  3. Synchronization: The monitor will control the access to forks to ensure that only one philosopher can try to pick them up or put them down at a time.

Monitor Implementation

monitor DiningPhilosophers
    enum State { THINKING, HUNGRY, EATING }
    State[] state = new State[5] // Assuming five philosophers
    condition[] self = new condition[5] // Condition variables for each philosopher

    initialize() {
        for i from 0 to 4:
            state[i] = THINKING

    // Entry Procedure for a philosopher who wants to eat
    pickupForks(int i) {
        state[i] = HUNGRY
        test(i) // Try to acquire two forks
        if state[i] != EATING
            self[i].wait() // Block if forks not available

    // Exit Procedure for a philosopher who has finished eating
    putdownForks(int i) {
        state[i] = THINKING
        // Test left and right neighbors
        test((i + 4) % 5)
        test((i + 1) % 5)

    // Test if the i-th philosopher can eat
    test(int i) {
        if state[(i + 4) % 5] != EATING and state[i] == HUNGRY and state[(i + 1) % 5] != EATING:
            state[i] = EATING
            self[i].signal() // Wake up the hungry philosopher

end monitor


  1. Initialization: All philosophers start in the THINKING state.

  2. Pick Up Forks: When a philosopher wishes to eat, they transition to the HUNGRY state and attempt to pick up the forks (by calling pickupForks). The test(i) function checks if the forks are available.

  3. Test Function: This function checks if a philosopher can transition to the EATING state. A philosopher can eat if both the left and the right neighbors are not eating. If the conditions are met, the philosopher's state is set to EATING, and they are signaled to start eating.

  4. Put Down Forks: After eating, a philosopher will put down the forks and return to the THINKING state. The putdownForks function then calls test on the left and right neighbors, potentially allowing them to start eating if they were waiting.

  5. Avoiding Deadlock: This solution avoids deadlock by ensuring that a philosopher can only pick up both forks or none. If a philosopher picks up one fork but the other is unavailable, they will release the fork and wait. This approach ensures that there is no circular wait condition, effectively preventing deadlock.

  6. Fairness: The use of condition variables ensures that no philosopher is starved. When a philosopher puts down their forks, they signal their neighbors, giving them a chance to eat if they were waiting.

This monitor-based solution ensures mutual exclusion, prevents deadlock, and avoids starvation, making it an effective approach for solving the Dining Philosophers problem.