The Drinking Philosophers Problem The classical Dining Philosophers Problem problem synchronization among competing processes five philosophers (thinking, hungry, and eating) around a table five chopsticks and a bowl of rice when hungry, a philosopher tries to pick up both chopsticks and he/she may pick up only one chopstick at a time when a philosopher gets both chopsticks, he/she eats without releasing his/her chopsticks when a philosopher finishes eating, he/she puts down both chopsticks and starts thinking again solution requirements deadlock-free and starvation-free problem generalization fair conflict resolution in distributed systems Dining Philosophers Problem Drinking Philosophers Problem The Drinking Philosophers Problem-1
problem fair conflict resolution in distributed systems conflicts can be resolved only if there is some property by which one P in every set of conflicting P can be distinguished and selected for favorable treatment conflict is resolved in favor of the distinguished P to guarantee fairness, the distinguishing property must be such that the P selected for favorable treatment is not always the same a new approach by using the locations of shared resources as a distinguishing property to fairly resolve all conflicts by introducing auxiliary resources judiciously transferring resources among P ensure two invariants to avoid probabilistic algorithms distinguishability in every state of the system, at least one P in every set of conflicting P must be distinguishable from the other P of the set deadlock-free fairness conflicts should not always be resolved to the detriment of a particular P starvation-free The Drinking Philosophers Problem-2
a graph model of conflict a DS is represented by an undirected graph G vertices in G P in the DS edge (u, v) G iff there may be a conflict between u and v assign a precedence ordering to every pair of potentially conflicting P so that one P has precedence over the other G = precedence graph H such that greater precedence P lesser precedence P depth of a processor p in H the max # of edges on any (directed) path to p from a processor without any predecessors a P with no predecessor has depth 0 neighbors cannot have the same depth if H is acyclic, then the depth of a P in H is a distinguishing property by which a P can be distinguished from all P that it may conflict with an algorithm that ensures that H is acyclic in every state of the system guarantee distinguishability but not fairness an algorithm that ensures that every P with conflicts rises to the top (zero depth) in H in finite time system state will change, and hence H will change too The Drinking Philosophers Problem-3
how should H change by changing the directions of edges an algorithm to implement H, and changes to H, by using a distributed scheme, where each change in H is made locally at one P H remains acyclic at all times (distinguishability) H changes in such a manner that every conflicting P eventually rises to the top in H (fairness) each change to H be done locally at a P acyclicity rule all edges incident on P may be simultaneously (in one atomic action) redirected toward P ensure acyclicity (distinguishability) fairness rule within finite time after a conflict is resolved in favor of a processor p at depth 0, p must yield precedence to all its neighbors ensure fairness in the event that P at depth 0 is in conflict, it will redirect all incident edges toward itself in finite time summary a distributed implementation of H as well as the acyclicity and fairness rules decide whether to yield or not, in a conflict, based solely on a P s local state The Drinking Philosophers Problem-4
The Drinking Philosophers (Drinkers) Problem processors as philosophers are in one of three states tranquil, thirsty, and drinking a bottle (B) is associated with each edge a P can drink only from bottles associated with his incident edges a tranquil P may become thirsty a thirsty P needs a nonempty set of bottles that he wishes to drink from a thirsty P may need different set of bottles for different drinking sessions on holding all needed bottles, a thirsty P starts drinking a thirsty P remains thirsty until he gets all bottles he needs to drink on entering the drinking state, a P remains in that state for a finite period, after which he becomes tranquil a P may be in the tranquil state for an arbitrary period of time The Drinking Philosophers Problem-5
devise a nonprobabilistic solution satisfying fairness no P remains thirsty forever (starvation-free) symmetry all P obey precisely the same rules for acquiring and releasing bottles (no priority or any other form of externally specified static partial ordering among P or B) economy a P sends and receives a finite # of msg between state transitions concurrency does not deny the possibility of simultaneous drinking from different B by different P boundedness the # of msg in transit, at any time, between any pair of P is bounded the drinkers problem is a general paradigm for modeling conflicts between P neighboring P will be prevented from drinking simultaneously if they wish to drink from the same B e.g. exclusive write into a common file neighboring P may drink simultaneously from different B e.g. different writes into different files The Drinking Philosophers Problem-6
The Dining Philosophers (Diners) Problem the diners problem is a special case of the drinkers problem in which every thirsty P needs B associated with all its incident edges for all drinking sessions the diners problem disallows neighbors from eating simultaneously the drinkers problem allows neighbors to drink simultaneously provided that they are drinking from different B a solution satisfying Fairness, Symmetry, Economy, Concurrency, and Boundness new terms philosophers are in one of three states thinking (tranquil), hungry (thirsty), and eating (drinking) fork (F) bottle (B) The Drinking Philosophers Problem-7
An informal solution to the Diners problem a F is either clean or dirty a F being used to eat with is dirty and remains dirty until it is cleaned a clean F remains clean until it is used for eating a P cleans a F when sending it he is hygienic a F is cleaned only when it is sent an eating P does not satisfy requests for F until he has finished eating key issue which requests should a noneating P defer solution a noneating P defers requests for F that are clean and satisfies requests for F that are dirty = implementation of precedence graph H the direction of an edge between two neighbors u and v is from u to v (u has precedence over v) iff u holds the F and F is clean, or v holds the F and F is dirty, or the F is in transit from v to u the direction (from u to v) of the edge can change only when u starts eating all edges incident on an eating P are directed toward him implementation of the acyclicity rule all edges incident on a P may be simultaneously directed toward him (an atomic action) initially all F are dirty and are located at P in such a way that H is acyclic immediately upon completion of an eating session, a P yields precedence to his neighbors The Drinking Philosophers Problem-8
A Hygienic Solution to the Diners Problem a request token for each fork Boolean variables for philosopher u fork u (f) philisopher u holds fork f reqf u (f) philisopher u holds the request token for fork f dirty u (f) fork f held by philisopher u is dirty thinking u, hungry u, eating u u is thinking/hungry/eating initial condition all forks are dirty every fork f and request token for f are held by different P H is acyclic a single guarded command (R1 requesting a fork f) hungry, reqf(f), fork(f) send request token for fork f reqf(f) = false (R2 releasing a fork f) eating, reqf(f), dirty(f) send fork f dirty(f) = false fork(f) = false (R3) upon receiving a request for fork f reqf(f) = true (R4) upon receiving fork f fork(f) = true dirty(f) eating fork(f) dirty(f) The Drinking Philosophers Problem-9
Proof of Fairness LEMMA 1: H is always acyclic H is acyclic initially observation the direction of edges in H may be affected only when a F changes its status or its location to show that every change to H preserves acyclicity every transmission of a F is accompanied by a change in its status from dirty to clean this does not change the direction of any edge a F is dirtied when the philosopher u holding it, eats (eating fork(f) dirty(f)) u must be holding all F associated with edges incident upon it, and they must all be dirty u cannot then create a cycle in H because all edges upon u are directed toward it The Drinking Philosophers Problem-10
THEOREM: Every hungry philosopher eats prove by induction on depth of a hungry philosopher to show that hungry philosophers at depth k in every H eat, provided all hungry philosophers at depth smaller than k in every H eat, k 0 let u, v be neighbors and u be hungry to show that u holds or will hold the f corresponding to the edge (u, v) and will thereafter continue to hold it until u eats if u holds the f currently and holds it continuously until he eats trivial case assume v holds the f sometime before u eats next (Case 1) f is dirty If reqf u (f) holds, then u will request f (rule R1) and subsequently reqf v (f) will hold; otherwise reqf v (f) already holds. If eating v holds, then at some later time (since eating is finite), eating v holds, and all other predicates for rule R2 still hold. Therefore, rule R2 will be applied by v, and u will eventually hold a clean fork. u will not release a clean fork until u eats. (Case 2) f is clean Every F held by a nonhungry philosopher is dirty, because all F are dirty initially only hungry philosophers receive clean F, and all F held by eating philosophers are dirty Since f is clean, the philosopher v holding it must be hungry. Furthermore, because f is clean, (v, u) is an edge in H and hence depth(v) < depth(u). According to the induction hypothesis, v eats and hence dirties f. Case 1 then applies. The Drinking Philosophers Problem-11
Drinkers Problem neighboring philosophers may drink simultaneously (from different B) prevent the system from entering states in which neighboring philosophers are indistinguishable A Solution to the Drinkers Problem the solution to the diners problem demonstrates a distributed implementation of the precedence graph H acyclic and fair implement precedence graph H via special auxiliary resources issue due to the distributed implementation of H for neighbors u and v, if u does not hold the fork, we cannot determine which of u and v has precedence from the state of u alone and u must make local decision about holding on to or releasing bottles without using precedence graph H not a problem for Diners need to obtain all forks Idea use forks to implement H forks are auxiliary resources forks are not part of the drinkers problem specification philisophers want B (to drink), not F (to eat) the real resources in the Drinkers problem are bottles forks are part of the solution to the Drinkers problem The Drinking Philosophers Problem-12
idea philosopher can eat and drink simultaneously eating is an artifact of the solution to guarantee fair drinking state of a philosopher (diner s state, drinker s state) define dining characteristics of philosopher as part of the solution (D1) Thinking-to-Hungry Transition a thinking, thirsty philosopher becomes hungry (D2) Eating-to-Thinking Transition an eating, non-thirsty philosopher starts thinking hungry-to-eating occurs when a P holds all F (Diners solution) Diners problem: a philosopher can think for arbitrary time although he must eat for finite time (D3) conflict resolution rule ensure eating period is finite u sends a b to v (in response to a request from v) iff [u does not need b] or [u is not drinking and does not hold the fork] u s decision to send or hold onto a bottle depends on whether u holds the fork and does not depend on whether u or v has precedence in H e.g. u must send the b to v if u has precedence over v, but u does not hold the fork example suppose u has precedence over v, but v holds the f u not only requests the b, but also the f via Diners problem, v will yield f to u in finite time after which v will also yield b to u The Drinking Philosophers Problem-13
Algorithm for the Drinkers Problem request token (t) Boolean variables bot u (b) u holds bottle b reqb u (b) u holds t for bottle b need u (b) u needs bottle b tranquil u, thirsty u, drinking u initial conditions for Dining philosophers as before for Drinking philosophers b and its t are held by different P state transitions for Dining philosophers (D1) thinking, thirsty hungry = true (D2) eating, thirsty thinking = true other actions of the Dining philosopher remain unchanged rules for b and request token transmissions (R1 requesting a bottle b) thirsty, need(b), reqb(b), bot(b) send request token for bottle b reqb(b) = false (R2 send a bottle b) reqb(b), bot(b), [ need(b) or ( drinking and fork(f)) ] send bottle b bot(b) = false (R3) upon receiving a request for bottle b reqb(b) = true (R4) upon receiving bottle b bot(b) = true The Drinking Philosophers Problem-14
Proof of Fairness LEMMA 3: every eating period is finite if u is eating and non-thirsty thinking (stop eating) (D2) if u is eating, it holds all F if u holds a B that it needs, it will not release it until it completes drinking (R2) if u needs and does not hold a B, then it holds the request token it will request the B (R1) v will have to send the B in finite time (R2) = because v does not hold the F and v can be in drinking state only for finite time u will hold all B it needs in finite time since u drinks for finite time, u will become tranquil in finite time u will stop eating in finite time (D2) since every eating period is finite, THEOREM 2 applies = every hungry philosopher eats COROLLARY 4: every hungry philosopher starts eating in finite time THEOREM 5: every thirsty philosopher drinks in finite time when a P becomes thirsty, it is either thinking, hungry, or eating a (thirsty, thinking) P becomes hungry in finite time (D1) a hungry philosopher starts eating in finite time every philosopher who remains thirsty will eat in finite time (LEMMA 3 and D2) = eating can be terminated only by drinking every thirsty philosopher drinks in finite time The Drinking Philosophers Problem-15