HW#3: Dining Philosopher Problem
Total Page:16
File Type:pdf, Size:1020Kb
HW#3: Dining Philosopher Problem
Due: March 27 Later homework penalty= X * 5%, X is the delay in days.
C/C++ programmer: using pthread semaphore Java programmer: using java monitor
Description: You will implement a deadlock free solution for dining philosopher problem. Your main thread will ask how many philosophers are going to participate this fun experiment and the number of time he will eat (N). Each philosopher will be represented by one thread and has the following behavior:
Loop (N times){
LOG(“thread I hungry”); dp.pickup(i)
LOG(“thread I eating”);
eat…wait a random period of time…
dp.putdown(i)
wait a random period of time } Figure 1: Pi structure with monitor
All LOG information must be write in one log file.
Note 1 For Java programmer, the reference code is given in page 219. It is also scanned and posted on the website.
Note 2 The semaphore based solution in page 210 (fig 7.17) is simple, but it will generate deadlock. If your implementation is based on this method, you will get at most 50% points.
Note 3 For C/C++ programmer, since pthread don’t have monitor support, you will need to modify the reference code by using semaphore. Here is some hint to do this: Use a mutex semaphore to guard the all call to pickup() and putdown() Each condition variables can be replaced by a semaphore. The wait() and signal() call of a condition variable is replaced by wait(s) and signal (s) of that semaphore. A reference thread code structure will be like this:
Shared Semaphore mutex=1;
Loop (N times){
LOG(“thread I hungry”);
Wait(mutex); pickup(i) signal(mutex);
LOG(“thread I eating”);
eat…wait a random period of time…
wait(mutex) putdown(i) signal(mutex)
wait a random period of time } Figure 2: Pi structure using semaphore