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
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.
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):
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