lecture8

# lecture8 - A Classic Problem - Dining Philosophers The...

This preview shows pages 1–3. Sign up to view the full content.

AClassic Problem - Dining Philosophers The Dining Philosophers problem is a classic OS problem that’susuallu stated in very non-OS terms: There are N philosphers sitting around a circular table eating spaghetti and discussing philos- phy. The problem is that each philosopher needs 2 forks to eat, and there are only N forks, one between each 2 philosophers. Design an algorithm that the philosophers can followthat insures that none starves as long as each philosopher eventually stops eating, and such that the maximum number of philosophers can eat at once. Whydescribe problems this way? Well, the analogous situations in computers are sometimes so technical that the yobscure creative thought. Thinking about philosophers makes it easier to think abstractly .And manyo fthe early students of this field were theoreticians who likeabstract problems. There are a bunch of named problems - Dining Philosophers, Drinking Philiosophers, Byzantine Generals, etc. Here’sanapproach to the Dining Phils 1 that’ssimple and wrong: void philosopher() { while(1) { sleep(); get_left_fork(); get_right_fork(); eat(); put_left_fork(); put_right_fork(); } } If every philosopher picks up the left fork at the same time, noone gets to eat - ever. Some other suboptimal alternatives: •P ick up the left fork, if the right fork isn’ tavailable for a giventime, put the left fork down, wait and try again. (Big problem if all philosophers wait the same time - we get the same fail- ure mode as before, but repeated.) Even if each philosopher waits a different random time, an unluckyphilosopher may starve(in the literal or technical sense). •R equire all philosophers to acquire a binary semaphore before picking up anyforks. This guarantees that no philosopher starves (assuming that the semaphore is fair) but limits paral- lelism dramatically .(FreeBSD has a similar problem with multiprocessor performance).

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
-2- #define N 5 /* Number of philosphers */ #define RIGHT(i) (((i)+1) %N) #define LEFT(i) (((i)==N) ? 0 : (i)+1) typedef enum { THINKING, HUNGRY, EATING } phil_state; phil_state state[N];
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/28/2008 for the course CS 402 taught by Professor Tedfaber during the Spring '05 term at USC.

### Page1 / 6

lecture8 - A Classic Problem - Dining Philosophers The...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online