SlideShare a Scribd company logo
1 of 19
What is Dining Philosophers Problem ?
• Five philosophers are in a thinking - eating
cycle.
• When a philosopher gets hungry, he sits
down, picks up two nearest chopsticks and
eats.
• A philosopher can eat only if he has both
chopsticks.
• After eating, he puts down both chopsticks
and thinks.
• This cycle continues.
What is Semaphore?
• Semaphore is a simply a variable. This
variable is used to solve critical section
problem and to achieve process
synchronization in the multi processing
environment.
• It consists of a counter, a waiting list of
processes and two methods (e.g.,
functions): signal and wait.
• Chopsticks are shared items (by two
philosophers) and must be protected.
• Each chopstick has a semaphore with
initial value 1.
• A philosopher calls wait() before picks
up a chopstick and calls signal() to
release it.
Solution for Dining Philosophers Problem using Semaphore
Does this solution works all the time?
No
Why? Let’s see
Circular Waiting & Deadlock
• If all five philosophers sit down and
pick up their left chopsticks at the
same time, this program has a
circular waiting and deadlocks.
Any Solutions for Deadlocks?
 Introduce a weirdo who picks up his right chopstick first!
 Allow at most 4 philosophers at the same table when there are 5
resources
 Allow a philosopher to pick up chopsticks only if both are free. This
requires protection of critical sections to test if both chopsticks are
free before grabbing them.
Now Fourth person act as an
weirdo and there is no
circular wait.
Introducing Weirdo
• The native solution to the dining philosophers causes
circular waiting.
• If only four philosophers are allowed to sit down, no
deadlock can occur.
• Why? If all four of them sit down at the same time, the
right-most philosopher can have both chopsticks!
• How about fewer than four?
This is obvious.
Allow at most four Philosophers to sit
Allow at most four Philosophers to sit Cont..
Introducing a new semaphore for
previews problem with initial
value of 4
This will allow to sit only 4
persons
Semaphores can result in deadlock due to
programming errors
Forgot to add a P() or V(), or miss ordered them, or duplicated them
to reduce these errors, introduce high-level synchronization primitives like
Monitors with condition variables
What is Monitor ?
In concurrent programming, a monitor is a
synchronization construct that allows threads to have
both mutual exclusion and the ability to wait (block) for
a certain condition to become true. Monitors also have a
mechanism for signaling other threads that their
condition has been met.
1. It is the collection of condition variables and procedures
combined together in a special kind of module or a package.
2. The processes running outside the monitor can’t access the
internal variable of monitor but can call procedures of the
monitor.
3. Only one process at a time can execute code inside monitors.
Condition Variables
Two different operations are performed on the condition
variables of the monitor.
Wait & Signal
let say we have 2 condition variables
condition x, y; //Declaring variable
Wait operation
x.wait() : Process performing wait operation on any condition
variable are suspended. The suspended processes are placed in
block queue of that condition variable.
Note: Each condition variable has its unique block queue.
Signal operation
x.signal(): When a process performs signal operation on condition
variable, one of the blocked processes is given chance.
What is Monitor ? Cont..
Monitor-based Solution to Dining Philosophers Problem
For prevent deadlock 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.
Key insight: pick up 2 chopsticks only if both are free
–this avoids deadlock
–reword insight: a philosopher moves to his/her eating state only if both neighbors are not in their eating states
•thus, need to define a state for each philosopher
–2nd insight: if one of my neighbors is eating, and I’m hungry, ask them to signal() me when they’re done
•thus, states of each philosopher are: thinking, hungry, eating
•thus, need condition variables to signal() waiting hungry philosopher(s)
–Also, need to Pickup() and Putdown() chopsticks
Monitor-based Solution to Dining Philosophers Problem
monitor DiningPhilosoper
{
status state[5]; // thinking, hungry, eating
condition self[5];
void pickup(int i) ; // Pickup Chopstick
void putdown(int i) ; // Put down chopsticks
void test(int i) ;
void init() {
for (int i = 0; i < 5; i++)
state[i] = thinking;
}
}
Monitor-based Solution to Dining Philosophers Problem Cont..
void pickup(int i) {
state[i] = hungry; //indicate that I’m hungry
test[i]; //set state to eating in test() only if my left and right neighbors are not eating
if (state[i] != eating)
self[i].wait(); //if unable to eat, wait to be signaled
}
void putdown(int i) {
state[i] = thinking;
// test left and right neighbors
test((i+4) % 5); //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);
}
Monitor-based Solution to Dining Philosophers Problem Cont..
•signal() has no effect during Pickup(), but is
important to wake up waiting hungry philosophers
during Putdown()
•Execution of Pickup(), Putdown() and test() are all
mutually exclusive, i.e. only one at a time can be
executing
•Verify that this monitor-based solution is
–deadlock-free
–mutually exclusive in that no 2 neighbors can eat
simultaneously
Any Questions ?
Thank You

More Related Content

What's hot

dining philosophers problem using montiors
dining philosophers problem using montiorsdining philosophers problem using montiors
dining philosophers problem using montiorsWarda Chaudhry
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Ra'Fat Al-Msie'deen
 
Semaphores OS Basics
Semaphores OS BasicsSemaphores OS Basics
Semaphores OS BasicsShijin Raj P
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's typesNishant Joshi
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating SystemsRitu Ranjan Shrivastwa
 
Moore and mealy machine
Moore and mealy machineMoore and mealy machine
Moore and mealy machineMian Munib
 
Process Synchronization And Deadlocks
Process Synchronization And DeadlocksProcess Synchronization And Deadlocks
Process Synchronization And Deadlockstech2click
 
CPU Scheduling algorithms
CPU Scheduling algorithmsCPU Scheduling algorithms
CPU Scheduling algorithmsShanu Kumar
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.Ravi Kumar Patel
 
Operating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronizationOperating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronizationVaibhav Khanna
 
Critical Section in Operating System
Critical Section in Operating SystemCritical Section in Operating System
Critical Section in Operating SystemMOHIT AGARWAL
 
Greedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.pptGreedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.pptRuchika Sinha
 

What's hot (20)

Semaphore
SemaphoreSemaphore
Semaphore
 
dining philosophers problem using montiors
dining philosophers problem using montiorsdining philosophers problem using montiors
dining philosophers problem using montiors
 
Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"Operating Systems - "Chapter 5 Process Synchronization"
Operating Systems - "Chapter 5 Process Synchronization"
 
CPU Scheduling Algorithms
CPU Scheduling AlgorithmsCPU Scheduling Algorithms
CPU Scheduling Algorithms
 
Process synchronization
Process synchronizationProcess synchronization
Process synchronization
 
Process synchronization in operating system
Process synchronization in operating systemProcess synchronization in operating system
Process synchronization in operating system
 
Semaphore
SemaphoreSemaphore
Semaphore
 
Semaphores OS Basics
Semaphores OS BasicsSemaphores OS Basics
Semaphores OS Basics
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's types
 
Scheduling algorithms
Scheduling algorithmsScheduling algorithms
Scheduling algorithms
 
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
Deadlock Avoidance in Operating System
Deadlock Avoidance in Operating SystemDeadlock Avoidance in Operating System
Deadlock Avoidance in Operating System
 
Monitors
MonitorsMonitors
Monitors
 
Moore and mealy machine
Moore and mealy machineMoore and mealy machine
Moore and mealy machine
 
Process Synchronization And Deadlocks
Process Synchronization And DeadlocksProcess Synchronization And Deadlocks
Process Synchronization And Deadlocks
 
CPU Scheduling algorithms
CPU Scheduling algorithmsCPU Scheduling algorithms
CPU Scheduling algorithms
 
Cpu scheduling in operating System.
Cpu scheduling in operating System.Cpu scheduling in operating System.
Cpu scheduling in operating System.
 
Operating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronizationOperating system 25 classical problems of synchronization
Operating system 25 classical problems of synchronization
 
Critical Section in Operating System
Critical Section in Operating SystemCritical Section in Operating System
Critical Section in Operating System
 
Greedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.pptGreedy Algorithms WITH Activity Selection Problem.ppt
Greedy Algorithms WITH Activity Selection Problem.ppt
 

Similar to Dining Philosopher Problem and Solution

Deadlock _Classic problems.pptx
Deadlock _Classic problems.pptxDeadlock _Classic problems.pptx
Deadlock _Classic problems.pptxGopikaS12
 
UNIT III Process Synchronization.docx
UNIT III Process Synchronization.docxUNIT III Process Synchronization.docx
UNIT III Process Synchronization.docxkarthikaparthasarath
 
Zuhri ramadhan dining philosophers theory and concept in operating system
Zuhri ramadhan   dining philosophers theory and concept in operating systemZuhri ramadhan   dining philosophers theory and concept in operating system
Zuhri ramadhan dining philosophers theory and concept in operating systemRaj Kamal
 
Process cooperation and synchronisation
Process cooperation and synchronisation Process cooperation and synchronisation
Process cooperation and synchronisation JayeshGadhave1
 
Operating system 26 monitors
Operating system 26 monitorsOperating system 26 monitors
Operating system 26 monitorsVaibhav Khanna
 
Operating systems
Operating systemsOperating systems
Operating systemsAbraham
 
Operating systems
Operating systemsOperating systems
Operating systemsAbraham
 
Dining_Philosopher.pptx
Dining_Philosopher.pptxDining_Philosopher.pptx
Dining_Philosopher.pptxmuraridesai2
 
Classical problems of process synchronization
Classical problems of process synchronizationClassical problems of process synchronization
Classical problems of process synchronizationsangrampatil81
 
Act1
Act1Act1
Act1dgzz
 
Act1
Act1Act1
Act1dgzz
 
Dinning philosopher
Dinning philosopherDinning philosopher
Dinning philosopherNehal Sharma
 
Algorithm Designs - Control Structures
Algorithm Designs - Control StructuresAlgorithm Designs - Control Structures
Algorithm Designs - Control Structureshatredai
 
Aug. 17 2011 (scientific method)
Aug. 17 2011 (scientific method)Aug. 17 2011 (scientific method)
Aug. 17 2011 (scientific method)Transition Academy
 
Breast Clinical Examination (unuploaded).pptx
Breast Clinical Examination (unuploaded).pptxBreast Clinical Examination (unuploaded).pptx
Breast Clinical Examination (unuploaded).pptxPradeep Pande
 

Similar to Dining Philosopher Problem and Solution (19)

Deadlock _Classic problems.pptx
Deadlock _Classic problems.pptxDeadlock _Classic problems.pptx
Deadlock _Classic problems.pptx
 
Deadlock.ppt
Deadlock.pptDeadlock.ppt
Deadlock.ppt
 
UNIT III Process Synchronization.docx
UNIT III Process Synchronization.docxUNIT III Process Synchronization.docx
UNIT III Process Synchronization.docx
 
Zuhri ramadhan dining philosophers theory and concept in operating system
Zuhri ramadhan   dining philosophers theory and concept in operating systemZuhri ramadhan   dining philosophers theory and concept in operating system
Zuhri ramadhan dining philosophers theory and concept in operating system
 
Process cooperation and synchronisation
Process cooperation and synchronisation Process cooperation and synchronisation
Process cooperation and synchronisation
 
Operating system 26 monitors
Operating system 26 monitorsOperating system 26 monitors
Operating system 26 monitors
 
12 deadlock concept
12 deadlock concept12 deadlock concept
12 deadlock concept
 
Operating systems
Operating systemsOperating systems
Operating systems
 
Operating systems
Operating systemsOperating systems
Operating systems
 
Dining_Philosopher.pptx
Dining_Philosopher.pptxDining_Philosopher.pptx
Dining_Philosopher.pptx
 
Classical problems of process synchronization
Classical problems of process synchronizationClassical problems of process synchronization
Classical problems of process synchronization
 
Act1
Act1Act1
Act1
 
Act1
Act1Act1
Act1
 
Dinning philosopher
Dinning philosopherDinning philosopher
Dinning philosopher
 
Algorithm Designs - Control Structures
Algorithm Designs - Control StructuresAlgorithm Designs - Control Structures
Algorithm Designs - Control Structures
 
Aug. 17 2011 (scientific method)
Aug. 17 2011 (scientific method)Aug. 17 2011 (scientific method)
Aug. 17 2011 (scientific method)
 
Breast Clinical Examination (unuploaded).pptx
Breast Clinical Examination (unuploaded).pptxBreast Clinical Examination (unuploaded).pptx
Breast Clinical Examination (unuploaded).pptx
 
Synchronization problems
Synchronization problemsSynchronization problems
Synchronization problems
 
Critical section operating system
Critical section  operating systemCritical section  operating system
Critical section operating system
 

More from Lahiru Danushka

Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygonsLiang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygonsLahiru Danushka
 
Virtual Machines - Virtual Box
Virtual Machines  - Virtual BoxVirtual Machines  - Virtual Box
Virtual Machines - Virtual BoxLahiru Danushka
 
Human Computer Interaction (HCI)
Human Computer Interaction (HCI)Human Computer Interaction (HCI)
Human Computer Interaction (HCI)Lahiru Danushka
 
Formalization Machines and Sastes
Formalization Machines and SastesFormalization Machines and Sastes
Formalization Machines and SastesLahiru Danushka
 
Election in Wireless Environment
Election in Wireless EnvironmentElection in Wireless Environment
Election in Wireless EnvironmentLahiru Danushka
 
Shuffle exchange networks
Shuffle exchange networksShuffle exchange networks
Shuffle exchange networksLahiru Danushka
 

More from Lahiru Danushka (7)

Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygonsLiang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
Liang- Barsky Algorithm, Polygon clipping & pipeline clipping of polygons
 
Virtual Machines - Virtual Box
Virtual Machines  - Virtual BoxVirtual Machines  - Virtual Box
Virtual Machines - Virtual Box
 
Human Computer Interaction (HCI)
Human Computer Interaction (HCI)Human Computer Interaction (HCI)
Human Computer Interaction (HCI)
 
Formalization Machines and Sastes
Formalization Machines and SastesFormalization Machines and Sastes
Formalization Machines and Sastes
 
My Sql Work Bench
My Sql Work BenchMy Sql Work Bench
My Sql Work Bench
 
Election in Wireless Environment
Election in Wireless EnvironmentElection in Wireless Environment
Election in Wireless Environment
 
Shuffle exchange networks
Shuffle exchange networksShuffle exchange networks
Shuffle exchange networks
 

Recently uploaded

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 

Recently uploaded (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 

Dining Philosopher Problem and Solution

  • 1.
  • 2. What is Dining Philosophers Problem ? • Five philosophers are in a thinking - eating cycle. • When a philosopher gets hungry, he sits down, picks up two nearest chopsticks and eats. • A philosopher can eat only if he has both chopsticks. • After eating, he puts down both chopsticks and thinks. • This cycle continues.
  • 3. What is Semaphore? • Semaphore is a simply a variable. This variable is used to solve critical section problem and to achieve process synchronization in the multi processing environment. • It consists of a counter, a waiting list of processes and two methods (e.g., functions): signal and wait.
  • 4. • Chopsticks are shared items (by two philosophers) and must be protected. • Each chopstick has a semaphore with initial value 1. • A philosopher calls wait() before picks up a chopstick and calls signal() to release it. Solution for Dining Philosophers Problem using Semaphore
  • 5. Does this solution works all the time? No Why? Let’s see
  • 6. Circular Waiting & Deadlock • If all five philosophers sit down and pick up their left chopsticks at the same time, this program has a circular waiting and deadlocks.
  • 7. Any Solutions for Deadlocks?  Introduce a weirdo who picks up his right chopstick first!  Allow at most 4 philosophers at the same table when there are 5 resources  Allow a philosopher to pick up chopsticks only if both are free. This requires protection of critical sections to test if both chopsticks are free before grabbing them.
  • 8. Now Fourth person act as an weirdo and there is no circular wait. Introducing Weirdo
  • 9. • The native solution to the dining philosophers causes circular waiting. • If only four philosophers are allowed to sit down, no deadlock can occur. • Why? If all four of them sit down at the same time, the right-most philosopher can have both chopsticks! • How about fewer than four? This is obvious. Allow at most four Philosophers to sit
  • 10. Allow at most four Philosophers to sit Cont.. Introducing a new semaphore for previews problem with initial value of 4 This will allow to sit only 4 persons
  • 11. Semaphores can result in deadlock due to programming errors Forgot to add a P() or V(), or miss ordered them, or duplicated them to reduce these errors, introduce high-level synchronization primitives like Monitors with condition variables
  • 12. What is Monitor ? In concurrent programming, a monitor is a synchronization construct that allows threads to have both mutual exclusion and the ability to wait (block) for a certain condition to become true. Monitors also have a mechanism for signaling other threads that their condition has been met. 1. It is the collection of condition variables and procedures combined together in a special kind of module or a package. 2. The processes running outside the monitor can’t access the internal variable of monitor but can call procedures of the monitor. 3. Only one process at a time can execute code inside monitors.
  • 13. Condition Variables Two different operations are performed on the condition variables of the monitor. Wait & Signal let say we have 2 condition variables condition x, y; //Declaring variable Wait operation x.wait() : Process performing wait operation on any condition variable are suspended. The suspended processes are placed in block queue of that condition variable. Note: Each condition variable has its unique block queue. Signal operation x.signal(): When a process performs signal operation on condition variable, one of the blocked processes is given chance. What is Monitor ? Cont..
  • 14. Monitor-based Solution to Dining Philosophers Problem For prevent deadlock 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. Key insight: pick up 2 chopsticks only if both are free –this avoids deadlock –reword insight: a philosopher moves to his/her eating state only if both neighbors are not in their eating states •thus, need to define a state for each philosopher –2nd insight: if one of my neighbors is eating, and I’m hungry, ask them to signal() me when they’re done •thus, states of each philosopher are: thinking, hungry, eating •thus, need condition variables to signal() waiting hungry philosopher(s) –Also, need to Pickup() and Putdown() chopsticks
  • 15. Monitor-based Solution to Dining Philosophers Problem monitor DiningPhilosoper { status state[5]; // thinking, hungry, eating condition self[5]; void pickup(int i) ; // Pickup Chopstick void putdown(int i) ; // Put down chopsticks void test(int i) ; void init() { for (int i = 0; i < 5; i++) state[i] = thinking; } }
  • 16. Monitor-based Solution to Dining Philosophers Problem Cont.. void pickup(int i) { state[i] = hungry; //indicate that I’m hungry test[i]; //set state to eating in test() only if my left and right neighbors are not eating if (state[i] != eating) self[i].wait(); //if unable to eat, wait to be signaled } void putdown(int i) { state[i] = thinking; // test left and right neighbors test((i+4) % 5); //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); }
  • 17. Monitor-based Solution to Dining Philosophers Problem Cont.. •signal() has no effect during Pickup(), but is important to wake up waiting hungry philosophers during Putdown() •Execution of Pickup(), Putdown() and test() are all mutually exclusive, i.e. only one at a time can be executing •Verify that this monitor-based solution is –deadlock-free –mutually exclusive in that no 2 neighbors can eat simultaneously