13.07.2015 Views

An Operating Systems Vade Mecum

An Operating Systems Vade Mecum

An Operating Systems Vade Mecum

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Dining philosophers 129Figure 4.6 shows each philosopher as a process and each chopstick as a resource.Each philosopher executes the following program.1 procedure Philosopher(Which : 0..4);2 var3 LeftChopstick, RightChopstick : ResourceDescriptor;4 begin5 { initialize Chopstick resource variables }6 LeftChopstick.Kind := Chopstick(Which);7 RightChopstick.Kind := Chopstick((Which+1) mod 5);8 loop { think — eat loop }9 Think; { occupy some finite amount of time in thought. }10 GetResource(LeftChopstick);11 GetResource(RightChopstick);12 Eat; { for a finite amount of time }13 ReleaseResource(LeftChopstick);14 ReleaseResource(RightChopstick);15 end; { think — eat loop }16 end Philosopher;Deadlock can occur if every philosopher tries to get chopsticks at once and each gets asfar as line 11. Each has asked for a left chopstick, and the allocator might grant all theserequests. Now every philosopher is stuck at line 11, because each right chopstick issomeone else’s left chopstick; thus each is in use. Figure 4.7 shows the resource graph inthis situation. A clever allocation policy might have prevented this disaster by keepingone chopstick free, which would have been more conservative.The dining philosophers are also subject to starvation. Let’s say that philosophers2 and 4 are currently eating. Philosophers 1 and 5 are engaged in thinking great thoughtsand will not be eating for a long time. Now philosopher 3 requests both chopsticks (oneat a time or both together). Philosopher 3 is blocked, since the chopsticks are held byphilosopher 2 and philosopher 4. Figure 4.8 shows the resource graph. Later,12534Figure 4.6 Dining philosophers

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!