SlideShare a Scribd company logo
1 of 32
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

Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating SystemsRitu Ranjan Shrivastwa
 
Reader/writer problem
Reader/writer problemReader/writer problem
Reader/writer problemRinkuMonani
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating systemSara Ali
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignmentKarthi Keyan
 
CPU Scheduling in OS Presentation
CPU Scheduling in OS  PresentationCPU Scheduling in OS  Presentation
CPU Scheduling in OS Presentationusmankiyani1
 
Deadlock Detection in Distributed Systems
Deadlock Detection in Distributed SystemsDeadlock Detection in Distributed Systems
Deadlock Detection in Distributed SystemsDHIVYADEVAKI
 
DeadLock in Operating-Systems
DeadLock in Operating-SystemsDeadLock in Operating-Systems
DeadLock in Operating-SystemsVenkata Sreeram
 
Network lab manual
Network lab manualNetwork lab manual
Network lab manualPrabhu D
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's typesNishant Joshi
 
Travelling Salesman
Travelling SalesmanTravelling Salesman
Travelling SalesmanShuvojit Kar
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in CompilersMahbubur Rahman
 
4 greedy methodnew
4 greedy methodnew4 greedy methodnew
4 greedy methodnewabhinav108
 
Process management os concept
Process management os conceptProcess management os concept
Process management os conceptpriyadeosarkar91
 

What's hot (20)

Process synchronization in Operating Systems
Process synchronization in Operating SystemsProcess synchronization in Operating Systems
Process synchronization in Operating Systems
 
Reader/writer problem
Reader/writer problemReader/writer problem
Reader/writer problem
 
Deadlock ppt
Deadlock ppt Deadlock ppt
Deadlock ppt
 
Deadlock Prevention
Deadlock PreventionDeadlock Prevention
Deadlock Prevention
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 
CPU Scheduling in OS Presentation
CPU Scheduling in OS  PresentationCPU Scheduling in OS  Presentation
CPU Scheduling in OS Presentation
 
Operating System: Deadlock
Operating System: DeadlockOperating System: Deadlock
Operating System: Deadlock
 
Deadlock Detection in Distributed Systems
Deadlock Detection in Distributed SystemsDeadlock Detection in Distributed Systems
Deadlock Detection in Distributed Systems
 
DeadLock in Operating-Systems
DeadLock in Operating-SystemsDeadLock in Operating-Systems
DeadLock in Operating-Systems
 
Network lab manual
Network lab manualNetwork lab manual
Network lab manual
 
Semophores and it's types
Semophores and it's typesSemophores and it's types
Semophores and it's types
 
Semaphores
SemaphoresSemaphores
Semaphores
 
Travelling Salesman
Travelling SalesmanTravelling Salesman
Travelling Salesman
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
4 greedy methodnew
4 greedy methodnew4 greedy methodnew
4 greedy methodnew
 
Greedy Algorihm
Greedy AlgorihmGreedy Algorihm
Greedy Algorihm
 
Distributed Mutual exclusion algorithms
Distributed Mutual exclusion algorithmsDistributed Mutual exclusion algorithms
Distributed Mutual exclusion algorithms
 
Process management os concept
Process management os conceptProcess management os concept
Process management os concept
 
Dijkstra's Algorithm
Dijkstra's Algorithm Dijkstra's Algorithm
Dijkstra's Algorithm
 

More from .AIR UNIVERSITY ISLAMABAD

More from .AIR UNIVERSITY ISLAMABAD (6)

Risk Assessment
Risk AssessmentRisk Assessment
Risk Assessment
 
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
 
Pipelining & All Hazards Solution
Pipelining  & All Hazards SolutionPipelining  & All Hazards Solution
Pipelining & All Hazards Solution
 
Project Management System
Project Management SystemProject Management System
Project Management System
 
Interfacing With High Level Programming Language
Interfacing With High Level Programming Language Interfacing With High Level Programming Language
Interfacing With High Level Programming Language
 
Code Converters & Parity Checker
Code Converters & Parity CheckerCode Converters & Parity Checker
Code Converters & Parity Checker
 

Recently uploaded

Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AIabhishek36461
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 

Recently uploaded (20)

Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Past, Present and Future of Generative AI
Past, Present and Future of Generative AIPast, Present and Future of Generative AI
Past, Present and Future of Generative AI
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 

Dining philosopher

  • 1.
  • 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