Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Aptitude
  • Engineering Mathematics
  • Discrete Mathematics
  • Operating System
  • DBMS
  • Computer Networks
  • Digital Logic and Design
  • C Programming
  • Data Structures
  • Algorithms
  • Theory of Computation
  • Compiler Design
  • Computer Org and Architecture
Open In App
Next Article:
Dining-Philosophers Solution Using Monitors
Next article icon

Dining-Philosophers Solution Using Monitors

Last Updated : 22 Apr, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Prerequisite: Monitor, Process Synchronization 

Dining-Philosophers Problem - N philosophers seated around a circular table 

  • There is one chopstick between each philosopher
  • A philosopher must pick up its two nearest chopsticks in order to eat
  • A philosopher must pick up first one chopstick, then the second one, not both at once

We need an algorithm for allocating these limited resources(chopsticks) among several processes(philosophers) such that solution is free from deadlock and free from starvation. There exist some algorithm to solve Dining - Philosopher Problem, but they may have deadlock situation. Also, a deadlock-free solution is not necessarily starvation-free. Semaphores can result in deadlock due to programming errors. Monitors alone are not sufficiency to solve this, we need monitors with condition variables Monitor-based Solution to Dining Philosophers We illustrate monitor concepts by presenting a deadlock-free solution to the dining-philosophers problem. Monitor is used to control access to state variables and condition variables. It only tells when to enter and exit the segment. This solution imposes the restriction that a philosopher may pick up her chopsticks only if both of them are available. To code this solution, we need to distinguish among three states in which we may find a philosopher. For this purpose, we introduce the following data structure: THINKING - When philosopher doesn't want to gain access to either fork. HUNGRY - When philosopher wants to enter the critical section. EATING - When philosopher has got both the forks, i.e., he has entered the section. Philosopher i can set the variable state[i] = EATING only if her two neighbors are not eating (state[(i+4) % 5] != EATING) and (state[(i+1) % 5] != EATING). 

CPP
// Dining-Philosophers Solution Using Monitors monitor DP {     status state[5];     condition self[5];      // Pickup chopsticks     Pickup(int i)     {         // indicate that I’m hungry         state[i] = hungry;          // set state to eating in test()         // only if my left and right neighbors          // are not eating         test(i);          // if unable to eat, wait to be signaled         if (state[i] != eating)             self[i].wait;     }      // Put down chopsticks     Putdown(int i)     {          // indicate that I’m thinking         state[i] = thinking;          // if right neighbor R=(i+1)%5 is hungry and         // both of R’s neighbors are not eating,         // set R’s state to eating and wake it up by          // signaling R’s CV         test((i + 1) % 5);         test((i + 4) % 5);     }      test(int i)     {          if (state[(i + 1) % 5] != eating             && state[(i + 4) % 5] != eating             && state[i] == hungry) {              // indicate that I’m eating             state[i] = eating;              // signal() has no effect during Pickup(),             // but is important to wake up waiting             // hungry philosophers during Putdown()             self[i].signal();         }     }      init()     {          // Execution of Pickup(), Putdown() and test()         // are all mutually exclusive,         // i.e. only one at a time can be executing for     i = 0 to 4          // Verify that this monitor-based solution is         // deadlock free and mutually exclusive in that         // no 2 neighbors can eat simultaneously         state[i] = thinking;     } } // end of monitor 

Above Program is a monitor solution to the dining-philosopher problem. We also need to declare

condition self[5];

This allows philosopher i to delay herself when she is hungry but is unable to obtain the chopsticks she needs. We are now in a position to describe our solution to the dining-philosophers problem. The distribution of the chopsticks is controlled by the monitor Dining Philosophers. Each philosopher, before starting to eat, must invoke the operation pickup(). This act may result in the suspension of the philosopher process. After the successful completion of the operation, the philosopher may eat. Following this, the philosopher invokes the putdown() operation. Thus, philosopher i must invoke the operations pickup() and putdown() in the following sequence:

DiningPhilosophers.pickup(i);               ...               eat               ... DiningPhilosophers.putdown(i);

It is easy to show that this solution ensures that no two neighbors are eating simultaneously and that no deadlocks will occur. We note, however, that it is possible for a philosopher to starve to death.

When will the Deadlock occur?

Deadlock occurs when two or more processes are blocked, waiting for each other to release a resource that they need. This can happen in solutions that allow a philosopher to pick up one chopstick at a time, or that allow all philosophers to pick up their left chopstick simultaneously.

When will Starvation occur?

Starvation occurs when a process is blocked indefinitely from accessing a resource that it needs. This can happen in solutions that give priority to certain philosophers or that allow some philosophers to continually acquire the chopsticks while others are unable to.


Next Article
Dining-Philosophers Solution Using Monitors

M

mayank rana
Improve
Article Tags :
  • Misc
  • Operating Systems
  • GATE CS
Practice Tags :
  • Misc

Similar Reads

    Dining Philosopher Problem Using Semaphores
    The Dining Philosopher Problem states that K philosophers are seated around a circular table with one chopstick between each pair of philosophers. There is one chopstick between each philosopher. A philosopher may eat if he can pick up the two chopsticks adjacent to him. One chopstick may be picked
    11 min read
    Dining Philosophers Problem
    The Dining Philosopher Problem is a classic synchronization and concurrency problem that deals with resource sharing, deadlock, and starvation in systems where multiple processes require limited resources. In this article, we will discuss the Dining Philosopher Problem in detail along with proper im
    8 min read
    Monitors in Process Synchronization
    Monitors are a higher-level synchronization construct that simplifies process synchronization by providing a high-level abstraction for data access and synchronization. Monitors are implemented as programming language constructs, typically in object-oriented languages, and provide mutual exclusion,
    3 min read
    Reader-Writer problem using Monitors (pthreads)
    Prerequisite - Monitors, Readers-Writers Problem There is a shared resource that is accessed by multiple processes i.e. readers and writers. Any number of readers can read from the shared resource simultaneously, but only one writer can write to the shared resource at a time. When a writer is writin
    4 min read
    Seating Arrangement : Aptitude Questions and Answers
    Seating arrangement is the logical arrangement of people or objects. This concept involves arrangement of people in many possible ways. Seating Arrangement is a common category of Logical Reasoning that is asked in competitive exams.In these types of questions, you will have to arrange a group of pe
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences