Dining-Philosphers Solution using Monitors

An operating system is software that manages every single aspect of a computer so that it can function smoothly and properly. Because of this reason, the OS has to perform several tasks simultaneously. Doing simultaneous tasks isn’t really a problem for the OS but when this simultaneous task uses a common resource, then it becomes a mishap. To overcome this situation synchronization is introduced, which basically manages the processes that share the same resource. Dining Philosophers problem is a classic synchronization problem.

What is Dining Philosophers Problem?

The story behind Dining Philosophers problem is that it represents a scenario where a group of philosophers sat around a round table and spend their lives either thinking or eating spaghetti. For the sake of simplicity, let’s consider that there are five philosophers sitting in the table. The circular table has five forks. To eat, each philosophers need two forks; one on his left side and other in his right. A philosopher may pick up only one chopstick at a time. He can’t pick up a chopstick that is already in the hand of another philosopher sitting next to him. When the philosopher has both chopsticks at the same time, he starts eating without releasing the chopsticks. The problem is to design an algorithm for allocating these limited resources(chopsticks) among the processes(philosophers) without causing a deadlock or starvation.

Now, there exist some algorithms that can solve the Dining Philosophers problem but may have a deadlock situation. Also, deadlock free situation is not necessarily starvation free. In solving, the Dining Philosophers problem Semaphores can be used but it can cause into a deadlock. Thus, to avoid these circumstances we use Monitors with conditional variables.

What is Monitors?

An abstract data type (ADT) is a programming concept that encapsulates private data with public methods to operate on data. This means that the data is hidden from the outside world and it can only be accessed or manipulated by a set of pre-defined methods.

A monitor is also an ADT that presents a set of programmer defined operations that are provided mutual exclusion within the monitor. In simpler words, monitor is a synchronization concept that provides mutual exclusion and coordination among concurrent threads or processes that access shared resources. It encapsulates a shared resource along with a set of procedures that can be used to access and modify that resource in a thread safe way.


Pseudo Code for monitors –

monitor monitor_name{
   procedure p1(…){
   procedure p2(…){
   procedure pn(…){
   Initialization code (…){

What are conditional variables?

Conditional variables provide synchronization inside the monitor. If a process wants to sleep or it allows a waiting process to continue, in that case conditional variables are used in monitors. Two operations can be performed on the conditional variables: wait and signal

  • wait() − Process invoking this operation is suspended and placed in block queue of that conditional variable.

  • signal() − This operation resumes the suspended process.

What is Semaphores? And why Monitors are considered over Semaphores?

Semaphores is an integer variable that can be used by processes to allow them the access of sharing common resources. Apart from initialization, semaphores can be accessed by only two atomic operations: wait() and signal(). There are two types of semaphores –

  • Binary Semaphores

  • Counting Semaphores

Both semaphores and monitors can be used to solve the Dining Philosophers problem. But there is no definite answer regarding which one is better – semaphores or monitors. It solely depends on the specific requirements of the problem.

In one hand, Monitors provides much more simple and direct solution for the Dining Philosophers problem because of its built-in mutual exclusion and condition variables.

On the other hand, Semaphores require more complex synchronization code and more prone to error and deadlock due to reasons that are explained earlier.

In summary, while both Monitors and Semaphores are used to solve the Dining Philosophers problem but Monitors are much handy than Semaphores.

Dining Philosophers Solution using Monitors

Monitors are used because they give a deadlock free solution to the Dining Philosophers problem. It is used to gain access over all the state variables and condition variables. After implying monitors, it imposes a restriction that a philosopher may pickup his chopsticks only if both of them are available at the same time.

To code the solution, we need to distinguish among three states in which may find a philosopher.





Here is implementation of the Dining Philosophers problem using Monitors –

monitor DiningPhilosophers {
   enum {THINKING, HUNGRY, EATING} state[5];
   condition self[5];
   void pickup(int i) {
      state[i] = HUNGRY;
      if (state[i] != EATING) {
   void putdown(int i) {
      state[i] = THINKING;
      test((i + 4) % 5);
      test((i + 1) % 5);
   void test(int i) {
      if (state[(i + 4) % 5] != EATING &&
      state[i] == HUNGRY &&
      state[(i + 1) % 5] != EATING) {
         state[i] = EATING;
   initialization code() {
      for(int i=0;i<5;i++)
      state[i] = THINKING;
DiningPhilosophers dp;

Here in the implementation, the distribution of chopstick is controlled by the monitor DiningPhilosophers. Before start eating, each philosopher must invoke the pickup() operation. It indicates that the philosopher is hungry, means that the process wants to use the resource. It also set the state to EATING in test() only if the philosopher’s left and right neighbors are not eating. If the philosopher is unable to eat, then wait() operation is invoked. After the successful completion of the operation, the philosopher may now eat.

Keeping that in mind, the philosopher now invokes the putdown() operation. After leaving forks, it checks on his neighbors. If they are HUNGRY and both of its neighbors are not EATING, then invoke signal() and offer them to eat.

Thus a philosopher must invokes the pickup() and putdown() operations simultaneously which ensures that no two neighbors are eating at the same time, thus achieving mutual exclusion. Thus, it prevents the deadlock. But there is a possibility that one of the philosopher may starve to death.

Advantages and Disadvantages

The Dining Philosopher problem can be implemented using both the monitors and semaphores. Both, monitors and semaphores are synchronized constructs used in concurrent programming. However, there are some advantages and disadvantages of using monitors instead of semaphores for the mentioned phenomenon.


  • Simple to use − Monitors are easier to use than semaphores. By encapsulating the initialization code within the monitors it is relatively easier to debug.

  • Mutual Exclusion − By implementing monitors in the Dining Philosophers problem, it achieves mutual exclusion. Thus, it prevents two neighbor philosophers to eat at the same time, thus preventing a deadlock situation.

  • Condition Variables − Monitors provide condition variables which indicates a thread to wait for a specific condition to be met.


  • Performance − Monitors requires a lock on a specific resource which can cause unnecessary delays. Thus, the performance can be compromised.

  • Flexibility − Monitors can be less flexible than semaphores due to specific synchronization requirements of the problem. In more complex scenarios, semaphores are likely to be well fitted because of its flexibility.


The Dining Philosopher problem is an illustration of a synchronization issue that can arise in operation system. However, by using monitors to implement a solution to the problem, mutual exclusion is achieved on the shared resources, preventing the occurrence of a deadlock.

Monitors provide a built-in mechanism for mutual exclusion, which enables only one process to access only one resource at a time. This ensures that, in this case, the forks (the shared resources) that the philosophers (process) need to eat, are not accessed concurrently, thereby avoiding a deadlock situation.

Overall, by using monitors in the Dining Philosophers problem the potential synchronization issues are prevented.

Updated on: 07-Apr-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started