Вы находитесь на странице: 1из 3

Dining Philosophers. There is a dining room containing a circular table with five chairs.

At each chair is a plate, and between each plate is a single chopstick. In the middle of the table is a bowl of spaghetti. Near the room are five philosophers who spend most of their time thinking, but who occasionally get hungry and need to eat so they can think some more. In order to eat, a philosopher must sit at the table, pick up the two chopsticks to the left and right of a plate, then serve and eat the spaghetti on the plate.

Thus, each philosopher is represented by the following pseudocode:


process P[i] while true do { THINK; PICKUP(CHOPSTICK[i], CHOPSTICK[i+ !od "]#; $%T; PUT&O'N(CHOPSTICK[i], CHOPSTICK[i+ !od "]# (

A philosopher may THINK indefinately. Every philosopher who $%Ts will eventually finish. hilosophers may PICKUP and PUT&O'N their chopsticks in either order, or nondeterministically, but these are atomic actions, and, of course, two philosophers cannot use a single CHOPSTICK at the same time. The problem is to design a protocol to satisfy the liveness condition: any philosopher who tries to EAT, eventually does. Discussion. !f course, the best thing is to go off and try to solve this problem on your own by e"ploring various protocols philosophers might use to ac#uire chopsticks. $ou are likely to #uickly encounter the samedeadlock and livelock scenarios we saw in the mutual exclusion problem, but you will #uickly see in this case that mutual e"clusion is too primitive a synchroni%ation mechanism for solving this problem. In his te"tbook Modern Operating ystems & rentice'(all, )**+, Tannenbaum gives the pseudo'code for a solution to the dining philosophers problem that is shown below. -imilar solutions are found in most operating systems te"tbooks. All of them are renditions of the original treatment by .i/kstra, which motivated the semaphore mechanism he was

introducing in his original article. A 0emph1semaphore2 is an integer or boolean value, S, with two associated atomic operations:
&O'N(S# UP(S#

wait until 3 4, then decrement 5 increment

In time'sharing systems, 6waiting6 is implemented by the operating system, which may put processes on a wait'list for later e"ecution. In hardware, 6waiting6 may be accomplished by busy'waiting or by some form of e"plicit signaling, such as token passing. Tannenbaum's Solution. This solution uses only boolean semaphors. There is one global semaphore to provide mutual e"clusion for e"ectution of critical protocols. There is one semaphore for each chopstick. In addition, a local two'phase prioriti%ation scheme is used, under which philosophers defer to their neighbors who have declared themselves 6hungry.6 All arithmetic is modulo 7.
s)ste! &ININ*+PHI,OSOPH$-S .%!e/ se!0phore, i1iti0ll) ; s["]/ se!0phore s["], i1iti0ll) 6; p4l08["]/ {THINK, HUN*-9, $%T(, i1iti0ll) THINK; 23 4or !utu0l e5clusio1 32 23 4or s)1chro1i70tio1 32 23 philosopher 4l08 32

As before, each philosopher is an endless cycle of thinking and eating.


procedure philosopher(i# { while T-U$ do { THINKIN*; t0:e+chopstic:s(i#; $%TIN*; drop+chopstic:s(i#; ( (

The t0:e+chopstic:s procedure involves checking the status of neighboring philosophers and then declaring one8s own intention to eat. This is a two'phase protocol5 first declaring the status HUN*-9, then going on to$%T.
procedure t0:e+chopstic:s(i# { &O'N(!e#; 23 critic0l sectio1 32 p4l08[i] /; HUN*-9; test[i]; UP(!e#; 23 e1d critic0l sectio1 32 &O'N(s[i]# 23 $0t i4 e10<led 32 ( =oid test(i# 23 ,et phil[i] e0t, i4 w0iti18 32 { i4 ( p4l08[i] ;; HUN*-9 >> p4l08[i? ] @; $%T >> p4l08[i+ ] @; $%T# the1 { p4l08[i] /; $%T;

( (

UP(s[i]#

!nce a philosopher finishes eating, all that remains is to relin#uish the resources'''its two chopsticks'''and thereby release waiting neighbors.
=oid drop+chopstic:s(i1t i# { &O'N(!e#; test(i? #; test(i+ #; UP(!e#; ( 23 23 23 23 critic0l sectio1 32 ,et philA o1 le4t e0t i4 possi<le 32 ,et philA o1 r8ht e0t i4 possi<le 32 up critic0l sectio1 32

The protocol is fairly elaborate, and Tannenbaum8s presentation is made more subtle by its coding style. A Simpler Solution. !ther authors, including .i/kstra, have posed simpler solutions to the dining philosopher problem than that proposed by Tannenbaum &depending on one8s notion of 6simplicity,6 of course,. !ne such solution is to restrict the number of philosophers allowed access to the table. If there are ! chopsticks but only !"# philosophers allowed to compete for them, at least one will succeed, even if they follow a rigid se#uential protocol to ac#uire their chopsticks. This solution is implemented with an integer semaphore, initiali%ed to !"#. 9oth this and Tannenbaum8s solutions avoid deadlock a situation in which all of the philosophers have grabbed one chopstick and are deterministically waiting for the other, so that there is no hope of recovery. (owever, they may still permit starvation, a scenario in which at least one hungry philosopher never gets to eat. -tarvation occurs when the asynchronous semantics may allow an individual to eat repeatedly, thus keeping another from getting a chopstick. The starving philosopher runs, perhaps, but doesn8t make progress. The observation of this fact leads to some further refinement of what $airness means. :nder some notions of fairness the solutions given above can be said to be correct.

Вам также может понравиться