In computer science, there's a classic problem known as "the dining philosophers". In this problem there are a certain number of philosophers who sit at a table to eat spaghetti and think. However, to avoid making a mess, they need to use two forks. Since there are the same number of forks as philosophers, if each philosopher grabs his left fork and holds onto it until he can get his right fork and eat, all the philosophers starve. I addressed this problem for a class I'm taking. My solution isn't novel. Basically, the philosopher holds onto a fork for a random length of time and then lets go if he can't get the other fork. The cool thing is that I did it in a new language called
Orc and I made the program control a movie.
If you're interested (I know that most of my audience isn't), here's the main method of the program:
def philosopher(disp,n,fl,fr) =
getForks(disp,n,fl,fr)
>> eat(disp,n)
>> letGo(disp,n,fl,fr)
>> think(disp,n)
>> philosopher(disp,n,fl,fr)
canvas(5) > disp > disp.open() >>
( philosopher(disp,0,f1,f2) |
philosopher(disp,1,f2,f3) |
philosopher(disp,2,f3,f4) |
philosopher(disp,3,f4,f5) |
philosopher(disp,4,f5,f1) |
framerate(disp,50)
) < f1 < makeFork(0)
< f2 < makeFork(1)
< f3 < makeFork(2)
< f4 < makeFork(3)
< f5 < makeFork(4)Obviously, I've made a bunch of other functions, but this is the main idea.
4 comments:
Very neat!
Gives a whole new twist to the phrase, "Spaghetti code" though.
I'm not really sure why they need both forks to eat. Maybe the forks serve as antennae that let the philosophers focus their mental prowess in order to psycho-kinetically consume the spaghetti sauce after calling it into existence ex nihilo. Maybe it was just too much trouble to animate elbows.
very cool but needs music.
i think it's kinda yucky that they share forks. blech!
Post a Comment