Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/smonn/hungrythinkers
Dining philosophers with Web Workers
https://github.com/smonn/hungrythinkers
Last synced: 22 days ago
JSON representation
Dining philosophers with Web Workers
- Host: GitHub
- URL: https://github.com/smonn/hungrythinkers
- Owner: smonn
- Created: 2012-08-17T21:55:03.000Z (over 12 years ago)
- Default Branch: master
- Last Pushed: 2019-05-03T02:02:31.000Z (over 5 years ago)
- Last Synced: 2023-03-11T01:51:54.603Z (almost 2 years ago)
- Language: JavaScript
- Homepage: https://hungry-thinkers.netlify.com/
- Size: 18.6 KB
- Stars: 4
- Watchers: 0
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Dining philosophers problem.
Solution created by using Web Workers and some fancy CSS3 for displaying the state.
## Philosophers
The philosophers are observable state machines (eat-state and think-state). They also have a property indicating their hunger.
### Eat state
While in the eating state philosophers become fed, reducing hunger by 5 each tick. If hunger reaches zero, they drop their forks and begin to think.
### Think state
While in the thinking state philosophers become hungry, increasing hunger by 1 each tick. Each tick they will try to pick up both forks. If they succeed, they begin to eat. Otherwise they drop any forks they possess and continue to think. This prevents resource-hogging, allowing up to two philosophers to eat at the same time.
Another solution would be to let the philosophers keep any fork they manage to pick up. This would prevent other philosophers from snatching "their" forks, and might reduce the time they spend hungry/thinking. But it would also result in (usually) only one philosopher eating at a time as well as blocking other philosophers who might need the forks more.
## Forks
The forks are a shared resource represented as a simple array. At initialization, each philosophers is assigned two forks, one for the left hand and one for the right. The philosophers request ownership of forks and if successful keeps both forks until they are released. This is all handled by the main thread, which could be considered a resource delegator.