ºÝºÝߣ

ºÝºÝߣShare a Scribd company logo
Dining philosopher
Group members:
ROLL NO:
ROLL NO:
ROLL NO:
ROLL NO:
ROLL NO:
Group No 02
University of Azad Jammu & Kashmir
Neelum Campus
BSCS V Semester, 2014-18
Operating System
Topic: Dining Philosophers
Presented By:
Mansoor Bashir
Dining Philosopher’s Problem
• Philosophers eat/think
• Eating needs two forks
• Pick one fork at a time
Dining Philosophers Problem
Five philosophers who spend their lives
just thinking and eating.
Only five chopsticks are available to the
philosophers
Dining Philosophers Problem
Each philosopher thinks. When he
becomes hungry, he sits down and
picks up the two chopsticks that are
closest to him and eats.
After a philosopher finishes eating, he
puts down the chopsticks and starts to
think.
Dining-Philosophers Problem
Dining-Philosophers Problem
Shared data : semaphore chopstick[5];
// Initialized to 1
Dining-Philosophers Problem
Philosopher i
do {
wait(chopstick[i])
wait(chopstick[(i+1)% 5])
eat
signal(chopstick[i]);
signal(chopstick[(i+1)% 5])
think
} while (1);
Possibility of Deadlock
If all philosophers become
hungry at the same time and
pick up their left chopstick, a
deadlock occurs.
17 December 2018
Possible Solutions
 Allow at most four philosophers
to be sitting simultaneously at
the table.
 Allow a philosopher to pick up
his/her chopsticks only if both
chopsticks are available (to do
this she must pick them up in a
critical section)
17 December 2018
Possible Solutions
Use an asymmetric solution; that is,
an odd philosopher picks up first
her left chopstick, whereas an even
philosopher picks up her right
chopstick and then her left
chopstick.
17 December 2018
Possibility of
Starvation
 Two philosophers who are fast
eaters and fast thinkers, and
lock the chopsticks before
others every time.
17 December 2018
Critical Regions
A critical region is a section of
code that is always executed
under mutual exclusion.
17 December 2018
Critical Regions
 They consist of two parts:
1.Variables that must be
accessed under mutual
exclusion.
2.A new language statement
that identifies a critical region
in which the variables are
accessed.
Dining philosophers
Who eats?
Types of Semaphores
 Counting semaphore – integer value
can range over an unrestricted integer
domain.
 Binary semaphore – integer value
cannot be > 1; can be simpler to
implement.
Implementing a Counting Semaphore
 Data structures
binary-semaphore S1, S2;
int C;
 Initialization
S1 = 1
S2 = 0
C = initial value of semaphore S
wait(S1);
C--;
if (C < 0) {
signal(S1);
wait(S2);
}
signal(S1);
Implementing a Counting Semaphore
wait(S):
wait(S1);
C++;
if (C <= 0)
signal(S2);
else
signal(S1);
Implementing a Counting Semaphore
signal(S):
i. Deterministic algorithms
ii. Randomized algorithm
Algorithms
Two Algorithm for dining Philosophers:
Two goals to achieve in solving the problem:
Deadlock free -- if at any time there is a hungry
philosopher, then eventually some philosopher will
eat.
Lockout free -- every hungry philosopher eventually
gets to eat.
The configuration of philosophers and sticks for the
case of n = 5 is illustrated below:
1. Deterministic algorithms:
As is proven in D. Lehman and M. O. Rabin's paper in
1981, no fully distributed and symmetric deterministic
algorithm for dining philosophers is possible. We can
prove it using the following example by introducing the
role of an adversary.
There can be an evil adversary, who contrives to
produce deadlock. For example, the adversary can
come up with the following "clever" strategy:
 All n philosophers become hungry at the same
moment
 They each pick up their left fork simultaneously
 because of the symmetry and the fact that each philosopher's
behavior is strictly deterministic, they have no choice but to put
down their sticks and try again later still precisely at the same
time.
By repeating this cycle constantly, the adversary will be able to
bring deadlock into this problem, thus makes any deterministic
algorithms fail to work.
Continue….
2. Randomized algorithm:
 Fork Available[i] is a Boolean variable for each Pi - Pi+1 pair,
which indicates whether the fork between them is available.
 The subtractions and additions are to be interpreted modulo n
We toss a coin when the hungry philosopher decides whether
to pick up the left fork or the right one first.
This randomization prevents any evil adversary's scheme. We
can show that the algorithm is deadlock free.
Continue….
The proof is based on that the coin tosses made by
philosophers are independent random events. Thus,
even if the adversary scheduler tries to bring on
deadlock, a combination of tosses will finally arise that
enables some philosopher to obtain two sticks. As the
index number (0,1,...i) attached to a philosopher is for
naming only, the algorithm is totally symmetric.
Continue….
However, this algorithm is not lockout free. There can
exist a very greedy philosopher Pi, and he tries to
prevent his neighbor from eating by always beating him
in the race of picking up the stick. Therefore, we need
some more parameters to improve this algorithm. We
can add two variables for each pair of philosophers, one
lets Pi to inform Pi+1 of his desire to eat, and vice versa.
The other shows which of the two eats last. In this way,
we can achieve both the deadlock free and lockout free
Continue….
17 December 2018
Monitor
Dining Philosophers Example
monitor dp
{
enum {thinking, hungry, eating} state[5];
condition self[5];
void pickup(int i) // Following slides
void putdown(int i) // Following slides
void test(int i) // Following slides
void init() {
for (int i = 0; i < 5; i++)
state[i] = thinking;
}
}
17 December 2018
Dining Philosophers
void pickup(int i) {
state[i] = hungry;
test(i);
if (state[i] != eating)
self[i].wait();
}
17 December 2018
void putdown(int i) {
state[i] = thinking;
// test left and right
// neighbors
test((i+4) % 5);
test((i+1) % 5);
}
Dining Philosophers
17 December 2018
void test(int i) {
if ((state[(i+4)%5]!= eating)
&& (state[i] == hungry) &&
(state[(i+1)%5]!= eating))
{
state[i] = eating;
self[i].signal();
}
}
Dining Philosophers
THANK
YOU

More Related Content

What's hot (20)

Np cooks theorem
Np cooks theoremNp cooks theorem
Np cooks theorem
Narayana Galla
Ìý
Semaphore
SemaphoreSemaphore
Semaphore
Arafat Hossan
Ìý
Deadlock
DeadlockDeadlock
Deadlock
Rajandeep Gill
Ìý
SCHEDULING ALGORITHMS
SCHEDULING ALGORITHMSSCHEDULING ALGORITHMS
SCHEDULING ALGORITHMS
Dhaval Sakhiya
Ìý
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
Vaibhav Khanna
Ìý
Load Balancing In Distributed Computing
Load Balancing In Distributed ComputingLoad Balancing In Distributed Computing
Load Balancing In Distributed Computing
Richa Singh
Ìý
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
Ìý
Branch and bound
Branch and boundBranch and bound
Branch and bound
Nv Thejaswini
Ìý
Register transfer and micro-operation
Register transfer and micro-operationRegister transfer and micro-operation
Register transfer and micro-operation
Nikhil Pandit
Ìý
Bankers algorithm
Bankers algorithmBankers algorithm
Bankers algorithm
AAQIB PARREY
Ìý
First-Come-First-Serve (FCFS)
First-Come-First-Serve (FCFS)First-Come-First-Serve (FCFS)
First-Come-First-Serve (FCFS)
nikeAthena
Ìý
Dead Lock in operating system
Dead Lock in operating systemDead Lock in operating system
Dead Lock in operating system
Ali Haider
Ìý
Distributed concurrency control
Distributed concurrency controlDistributed concurrency control
Distributed concurrency control
Binte fatima
Ìý
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
Ankit Katiyar
Ìý
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
Ìý
Reasoning in AI
Reasoning in AIReasoning in AI
Reasoning in AI
Gunjan Chhabra
Ìý
Pthread
PthreadPthread
Pthread
Gopi Saiteja
Ìý
(Radhika) presentation on chapter 2 ai
(Radhika) presentation on chapter 2 ai(Radhika) presentation on chapter 2 ai
(Radhika) presentation on chapter 2 ai
Radhika Srinivasan
Ìý
Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)
Mukesh Chinta
Ìý
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMI. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHM
vikas dhakane
Ìý
Np cooks theorem
Np cooks theoremNp cooks theorem
Np cooks theorem
Narayana Galla
Ìý
SCHEDULING ALGORITHMS
SCHEDULING ALGORITHMSSCHEDULING ALGORITHMS
SCHEDULING ALGORITHMS
Dhaval Sakhiya
Ìý
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
Vaibhav Khanna
Ìý
Load Balancing In Distributed Computing
Load Balancing In Distributed ComputingLoad Balancing In Distributed Computing
Load Balancing In Distributed Computing
Richa Singh
Ìý
Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
Ritu Ranjan Shrivastwa
Ìý
Branch and bound
Branch and boundBranch and bound
Branch and bound
Nv Thejaswini
Ìý
Register transfer and micro-operation
Register transfer and micro-operationRegister transfer and micro-operation
Register transfer and micro-operation
Nikhil Pandit
Ìý
Bankers algorithm
Bankers algorithmBankers algorithm
Bankers algorithm
AAQIB PARREY
Ìý
First-Come-First-Serve (FCFS)
First-Come-First-Serve (FCFS)First-Come-First-Serve (FCFS)
First-Come-First-Serve (FCFS)
nikeAthena
Ìý
Dead Lock in operating system
Dead Lock in operating systemDead Lock in operating system
Dead Lock in operating system
Ali Haider
Ìý
Distributed concurrency control
Distributed concurrency controlDistributed concurrency control
Distributed concurrency control
Binte fatima
Ìý
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
Ankit Katiyar
Ìý
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
Ìý
(Radhika) presentation on chapter 2 ai
(Radhika) presentation on chapter 2 ai(Radhika) presentation on chapter 2 ai
(Radhika) presentation on chapter 2 ai
Radhika Srinivasan
Ìý
Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)Process scheduling (CPU Scheduling)
Process scheduling (CPU Scheduling)
Mukesh Chinta
Ìý
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMI. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHM
vikas dhakane
Ìý

More from .AIR UNIVERSITY ISLAMABAD (6)

Risk Assessment
Risk AssessmentRisk Assessment
Risk Assessment
.AIR UNIVERSITY ISLAMABAD
Ìý
Mission ,Vision statement, and strategies of Unicef
Mission ,Vision statement, and strategies of UnicefMission ,Vision statement, and strategies of Unicef
Mission ,Vision statement, and strategies of Unicef
.AIR UNIVERSITY ISLAMABAD
Ìý
Pipelining & All Hazards Solution
Pipelining  & All Hazards SolutionPipelining  & All Hazards Solution
Pipelining & All Hazards Solution
.AIR UNIVERSITY ISLAMABAD
Ìý
Project Management System
Project Management SystemProject Management System
Project Management System
.AIR UNIVERSITY ISLAMABAD
Ìý
Interfacing With High Level Programming Language
Interfacing With High Level Programming Language Interfacing With High Level Programming Language
Interfacing With High Level Programming Language
.AIR UNIVERSITY ISLAMABAD
Ìý
Code Converters & Parity Checker
Code Converters & Parity CheckerCode Converters & Parity Checker
Code Converters & Parity Checker
.AIR UNIVERSITY ISLAMABAD
Ìý

Recently uploaded (20)

google_developer_group_ramdeobaba_university_EXPLORE_PPT
google_developer_group_ramdeobaba_university_EXPLORE_PPTgoogle_developer_group_ramdeobaba_university_EXPLORE_PPT
google_developer_group_ramdeobaba_university_EXPLORE_PPT
JayeshShete1
Ìý
Optimization of Cumulative Energy, Exergy Consumption and Environmental Life ...
Optimization of Cumulative Energy, Exergy Consumption and Environmental Life ...Optimization of Cumulative Energy, Exergy Consumption and Environmental Life ...
Optimization of Cumulative Energy, Exergy Consumption and Environmental Life ...
J. Agricultural Machinery
Ìý
Structural QA/QC Inspection in KRP 401600 | Copper Processing Plant-3 (MOF-3)...
Structural QA/QC Inspection in KRP 401600 | Copper Processing Plant-3 (MOF-3)...Structural QA/QC Inspection in KRP 401600 | Copper Processing Plant-3 (MOF-3)...
Structural QA/QC Inspection in KRP 401600 | Copper Processing Plant-3 (MOF-3)...
slayshadow705
Ìý
15. Smart Cities Big Data, Civic Hackers, and the Quest for a New Utopia.pdf
15. Smart Cities Big Data, Civic Hackers, and the Quest for a New Utopia.pdf15. Smart Cities Big Data, Civic Hackers, and the Quest for a New Utopia.pdf
15. Smart Cities Big Data, Civic Hackers, and the Quest for a New Utopia.pdf
NgocThang9
Ìý
04 MAINTENANCE OF CONCRETE PAVEMENTS.ppt
04  MAINTENANCE OF CONCRETE PAVEMENTS.ppt04  MAINTENANCE OF CONCRETE PAVEMENTS.ppt
04 MAINTENANCE OF CONCRETE PAVEMENTS.ppt
sreenath seenu
Ìý
Taykon-Kalite belgeleri
Taykon-Kalite belgeleriTaykon-Kalite belgeleri
Taykon-Kalite belgeleri
TAYKON
Ìý
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptxRAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
JenTeruel1
Ìý
decarbonization steel industry rev1.pptx
decarbonization steel industry rev1.pptxdecarbonization steel industry rev1.pptx
decarbonization steel industry rev1.pptx
gonzalezolabarriaped
Ìý
How to Make an RFID Door Lock System using Arduino
How to Make an RFID Door Lock System using ArduinoHow to Make an RFID Door Lock System using Arduino
How to Make an RFID Door Lock System using Arduino
CircuitDigest
Ìý
How to Build a Maze Solving Robot Using Arduino
How to Build a Maze Solving Robot Using ArduinoHow to Build a Maze Solving Robot Using Arduino
How to Build a Maze Solving Robot Using Arduino
CircuitDigest
Ìý
TM-ASP-101-RF_Air Press manual crimping machine.pdf
TM-ASP-101-RF_Air Press manual crimping machine.pdfTM-ASP-101-RF_Air Press manual crimping machine.pdf
TM-ASP-101-RF_Air Press manual crimping machine.pdf
ChungLe60
Ìý
Lecture -3 Cold water supply system.pptx
Lecture -3 Cold water supply system.pptxLecture -3 Cold water supply system.pptx
Lecture -3 Cold water supply system.pptx
rabiaatif2
Ìý
Cyber Security_ Protecting the Digital World.pptx
Cyber Security_ Protecting the Digital World.pptxCyber Security_ Protecting the Digital World.pptx
Cyber Security_ Protecting the Digital World.pptx
Harshith A S
Ìý
Engineering at Lovely Professional University (LPU).pdf
Engineering at Lovely Professional University (LPU).pdfEngineering at Lovely Professional University (LPU).pdf
Engineering at Lovely Professional University (LPU).pdf
Sona
Ìý
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptxMathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
ppkmurthy2006
Ìý
GM Meeting 070225 TO 130225 for 2024.pptx
GM Meeting 070225 TO 130225 for 2024.pptxGM Meeting 070225 TO 130225 for 2024.pptx
GM Meeting 070225 TO 130225 for 2024.pptx
crdslalcomumbai
Ìý
Lectureof nano 1588236675-biosensors (1).ppt
Lectureof nano 1588236675-biosensors (1).pptLectureof nano 1588236675-biosensors (1).ppt
Lectureof nano 1588236675-biosensors (1).ppt
SherifElGohary7
Ìý
Multi objective genetic approach with Ranking
Multi objective genetic approach with RankingMulti objective genetic approach with Ranking
Multi objective genetic approach with Ranking
namisha18
Ìý
autonomous vehicle project for engineering.pdf
autonomous vehicle project for engineering.pdfautonomous vehicle project for engineering.pdf
autonomous vehicle project for engineering.pdf
JyotiLohar6
Ìý
Unit II: Design of Static Equipment Foundations
Unit II: Design of Static Equipment FoundationsUnit II: Design of Static Equipment Foundations
Unit II: Design of Static Equipment Foundations
Sanjivani College of Engineering, Kopargaon
Ìý
google_developer_group_ramdeobaba_university_EXPLORE_PPT
google_developer_group_ramdeobaba_university_EXPLORE_PPTgoogle_developer_group_ramdeobaba_university_EXPLORE_PPT
google_developer_group_ramdeobaba_university_EXPLORE_PPT
JayeshShete1
Ìý
Optimization of Cumulative Energy, Exergy Consumption and Environmental Life ...
Optimization of Cumulative Energy, Exergy Consumption and Environmental Life ...Optimization of Cumulative Energy, Exergy Consumption and Environmental Life ...
Optimization of Cumulative Energy, Exergy Consumption and Environmental Life ...
J. Agricultural Machinery
Ìý
Structural QA/QC Inspection in KRP 401600 | Copper Processing Plant-3 (MOF-3)...
Structural QA/QC Inspection in KRP 401600 | Copper Processing Plant-3 (MOF-3)...Structural QA/QC Inspection in KRP 401600 | Copper Processing Plant-3 (MOF-3)...
Structural QA/QC Inspection in KRP 401600 | Copper Processing Plant-3 (MOF-3)...
slayshadow705
Ìý
15. Smart Cities Big Data, Civic Hackers, and the Quest for a New Utopia.pdf
15. Smart Cities Big Data, Civic Hackers, and the Quest for a New Utopia.pdf15. Smart Cities Big Data, Civic Hackers, and the Quest for a New Utopia.pdf
15. Smart Cities Big Data, Civic Hackers, and the Quest for a New Utopia.pdf
NgocThang9
Ìý
04 MAINTENANCE OF CONCRETE PAVEMENTS.ppt
04  MAINTENANCE OF CONCRETE PAVEMENTS.ppt04  MAINTENANCE OF CONCRETE PAVEMENTS.ppt
04 MAINTENANCE OF CONCRETE PAVEMENTS.ppt
sreenath seenu
Ìý
Taykon-Kalite belgeleri
Taykon-Kalite belgeleriTaykon-Kalite belgeleri
Taykon-Kalite belgeleri
TAYKON
Ìý
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptxRAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
RAMSES- EDITORIAL SAMPLE FOR DSSPC C.pptx
JenTeruel1
Ìý
decarbonization steel industry rev1.pptx
decarbonization steel industry rev1.pptxdecarbonization steel industry rev1.pptx
decarbonization steel industry rev1.pptx
gonzalezolabarriaped
Ìý
How to Make an RFID Door Lock System using Arduino
How to Make an RFID Door Lock System using ArduinoHow to Make an RFID Door Lock System using Arduino
How to Make an RFID Door Lock System using Arduino
CircuitDigest
Ìý
How to Build a Maze Solving Robot Using Arduino
How to Build a Maze Solving Robot Using ArduinoHow to Build a Maze Solving Robot Using Arduino
How to Build a Maze Solving Robot Using Arduino
CircuitDigest
Ìý
TM-ASP-101-RF_Air Press manual crimping machine.pdf
TM-ASP-101-RF_Air Press manual crimping machine.pdfTM-ASP-101-RF_Air Press manual crimping machine.pdf
TM-ASP-101-RF_Air Press manual crimping machine.pdf
ChungLe60
Ìý
Lecture -3 Cold water supply system.pptx
Lecture -3 Cold water supply system.pptxLecture -3 Cold water supply system.pptx
Lecture -3 Cold water supply system.pptx
rabiaatif2
Ìý
Cyber Security_ Protecting the Digital World.pptx
Cyber Security_ Protecting the Digital World.pptxCyber Security_ Protecting the Digital World.pptx
Cyber Security_ Protecting the Digital World.pptx
Harshith A S
Ìý
Engineering at Lovely Professional University (LPU).pdf
Engineering at Lovely Professional University (LPU).pdfEngineering at Lovely Professional University (LPU).pdf
Engineering at Lovely Professional University (LPU).pdf
Sona
Ìý
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptxMathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
Mathematics behind machine learning INT255 INT255__Unit 3__PPT-1.pptx
ppkmurthy2006
Ìý
GM Meeting 070225 TO 130225 for 2024.pptx
GM Meeting 070225 TO 130225 for 2024.pptxGM Meeting 070225 TO 130225 for 2024.pptx
GM Meeting 070225 TO 130225 for 2024.pptx
crdslalcomumbai
Ìý
Lectureof nano 1588236675-biosensors (1).ppt
Lectureof nano 1588236675-biosensors (1).pptLectureof nano 1588236675-biosensors (1).ppt
Lectureof nano 1588236675-biosensors (1).ppt
SherifElGohary7
Ìý
Multi objective genetic approach with Ranking
Multi objective genetic approach with RankingMulti objective genetic approach with Ranking
Multi objective genetic approach with Ranking
namisha18
Ìý
autonomous vehicle project for engineering.pdf
autonomous vehicle project for engineering.pdfautonomous vehicle project for engineering.pdf
autonomous vehicle project for engineering.pdf
JyotiLohar6
Ìý

Dining philosopher

  • 2. Group members: ROLL NO: ROLL NO: ROLL NO: ROLL NO: ROLL NO: Group No 02 University of Azad Jammu & Kashmir Neelum Campus BSCS V Semester, 2014-18 Operating System Topic: Dining Philosophers Presented By: Mansoor Bashir
  • 3. Dining Philosopher’s Problem • Philosophers eat/think • Eating needs two forks • Pick one fork at a time
  • 4. Dining Philosophers Problem Five philosophers who spend their lives just thinking and eating. Only five chopsticks are available to the philosophers
  • 5. Dining Philosophers Problem Each philosopher thinks. When he becomes hungry, he sits down and picks up the two chopsticks that are closest to him and eats. After a philosopher finishes eating, he puts down the chopsticks and starts to think.
  • 7. Dining-Philosophers Problem Shared data : semaphore chopstick[5]; // Initialized to 1
  • 8. Dining-Philosophers Problem Philosopher i do { wait(chopstick[i]) wait(chopstick[(i+1)% 5]) eat signal(chopstick[i]); signal(chopstick[(i+1)% 5]) think } while (1);
  • 9. Possibility of Deadlock If all philosophers become hungry at the same time and pick up their left chopstick, a deadlock occurs.
  • 10. 17 December 2018 Possible Solutions  Allow at most four philosophers to be sitting simultaneously at the table.  Allow a philosopher to pick up his/her chopsticks only if both chopsticks are available (to do this she must pick them up in a critical section)
  • 11. 17 December 2018 Possible Solutions Use an asymmetric solution; that is, an odd philosopher picks up first her left chopstick, whereas an even philosopher picks up her right chopstick and then her left chopstick.
  • 12. 17 December 2018 Possibility of Starvation  Two philosophers who are fast eaters and fast thinkers, and lock the chopsticks before others every time.
  • 13. 17 December 2018 Critical Regions A critical region is a section of code that is always executed under mutual exclusion.
  • 14. 17 December 2018 Critical Regions  They consist of two parts: 1.Variables that must be accessed under mutual exclusion. 2.A new language statement that identifies a critical region in which the variables are accessed.
  • 16. Types of Semaphores  Counting semaphore – integer value can range over an unrestricted integer domain.  Binary semaphore – integer value cannot be > 1; can be simpler to implement.
  • 17. Implementing a Counting Semaphore  Data structures binary-semaphore S1, S2; int C;  Initialization S1 = 1 S2 = 0 C = initial value of semaphore S
  • 18. wait(S1); C--; if (C < 0) { signal(S1); wait(S2); } signal(S1); Implementing a Counting Semaphore wait(S):
  • 19. wait(S1); C++; if (C <= 0) signal(S2); else signal(S1); Implementing a Counting Semaphore signal(S):
  • 20. i. Deterministic algorithms ii. Randomized algorithm Algorithms Two Algorithm for dining Philosophers:
  • 21. Two goals to achieve in solving the problem: Deadlock free -- if at any time there is a hungry philosopher, then eventually some philosopher will eat. Lockout free -- every hungry philosopher eventually gets to eat. The configuration of philosophers and sticks for the case of n = 5 is illustrated below:
  • 22. 1. Deterministic algorithms: As is proven in D. Lehman and M. O. Rabin's paper in 1981, no fully distributed and symmetric deterministic algorithm for dining philosophers is possible. We can prove it using the following example by introducing the role of an adversary. There can be an evil adversary, who contrives to produce deadlock. For example, the adversary can come up with the following "clever" strategy:  All n philosophers become hungry at the same moment
  • 23.  They each pick up their left fork simultaneously  because of the symmetry and the fact that each philosopher's behavior is strictly deterministic, they have no choice but to put down their sticks and try again later still precisely at the same time. By repeating this cycle constantly, the adversary will be able to bring deadlock into this problem, thus makes any deterministic algorithms fail to work. Continue….
  • 25.  Fork Available[i] is a Boolean variable for each Pi - Pi+1 pair, which indicates whether the fork between them is available.  The subtractions and additions are to be interpreted modulo n We toss a coin when the hungry philosopher decides whether to pick up the left fork or the right one first. This randomization prevents any evil adversary's scheme. We can show that the algorithm is deadlock free. Continue….
  • 26. The proof is based on that the coin tosses made by philosophers are independent random events. Thus, even if the adversary scheduler tries to bring on deadlock, a combination of tosses will finally arise that enables some philosopher to obtain two sticks. As the index number (0,1,...i) attached to a philosopher is for naming only, the algorithm is totally symmetric. Continue….
  • 27. However, this algorithm is not lockout free. There can exist a very greedy philosopher Pi, and he tries to prevent his neighbor from eating by always beating him in the race of picking up the stick. Therefore, we need some more parameters to improve this algorithm. We can add two variables for each pair of philosophers, one lets Pi to inform Pi+1 of his desire to eat, and vice versa. The other shows which of the two eats last. In this way, we can achieve both the deadlock free and lockout free Continue….
  • 28. 17 December 2018 Monitor Dining Philosophers Example monitor dp { enum {thinking, hungry, eating} state[5]; condition self[5]; void pickup(int i) // Following slides void putdown(int i) // Following slides void test(int i) // Following slides void init() { for (int i = 0; i < 5; i++) state[i] = thinking; } }
  • 29. 17 December 2018 Dining Philosophers void pickup(int i) { state[i] = hungry; test(i); if (state[i] != eating) self[i].wait(); }
  • 30. 17 December 2018 void putdown(int i) { state[i] = thinking; // test left and right // neighbors test((i+4) % 5); test((i+1) % 5); } Dining Philosophers
  • 31. 17 December 2018 void test(int i) { if ((state[(i+4)%5]!= eating) && (state[i] == hungry) && (state[(i+1)%5]!= eating)) { state[i] = eating; self[i].signal(); } } Dining Philosophers