Philosophers
by bhagenlo
written for version 10
In our case, the problem statement goes the following:
- There are N philosophers at a round table, as well as N forks. Each fork lies in between two philosophers.
- Philosophers alternately eat, sleep and think, in that order.
- To be able to eat, philosophers have to pick up both the forks left and right to them.
- When they've finished eating, they have to put them down again.
So far, that sounds like a project where one can come up with any ideas/algorithms for figuring out cooperation between agents, then test them against each other, and think about which one to use when.
Sadly, that is not the case. The problem statement goes on:
- Philosophers don't speak with each other.
- Philosophers don't know whether another philosopher is about to die.
And gone is the open space for creativity. In exchange for that, you learn about threads, mutexes, and how to create proper abstractions. Which, at least, is quite nice too.
Before even thinking about how to handle a problem as complex as the Dining Philosophers (concurrent problems always are – our brains are just not good at this by nature), it's super useful to first create working tools. And that's what we're gonna do. To find those, we start out by finding out what exactly the important functions from the subject do. You'll definitely need to manage time, threads and mutexes. (If you don't know what the latter two are, now is the time ;)) And we start out with time: This function returns a Clearly, you don't want to work with this. We're interested in the µs that passed since the start of our simulation. So, I'd strongly advise you to create a tool (-> write a function) that returns a proper UTC timestamp (in µs) to you. Next, threads: Again, find out what the three of them (create, join and detach) do. Create a small working examples with (N) threads in which: And if you, for some case, found out that you'd rather use wrapper around those functions, please do so. Finally, mutexes: And again. Create some working examples, and write a wrapper when you see fit.#Prerequisites
#1.
gettimeofday()
timeval
struct, which looks like that:struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
#2.
pthread_*
#3.
pthread_mutex_*
Now, finally. You're able to start working on our algorithm. Have fun! :) But keep in mind that:#During
There shouldn't be that much to be done here. Grab yourself the eval sheet (or a tester), and off you go :)#Cleaning Up
Now that you're done, I can say it out loud: If you want or need to write a concurrent program, then:#Aftercare
#Pointers