code a version of the “resource hierarchy solution” for dining philosophers.

computer science

Description

We discussed both the pthreads library and the dining philosophers problem in class. We also implemented, and discussed, an “arbitrator solution”. You will find with this homework a pthreads implementation of a bad dining philosophers solution that will deadlock. This bad solution is in the file bad_philosophers.c. You will also find with this homework a pthread implementation of a working dining philosophers problem that uses an arbitrator to prevent deadlocks. This solution is in the file good_philosophers1.c. You may modify either of these programs to address any of the questions in this homework. You may also start from scratch if you so choose.

 

1)      By yourself, code a version of the “resource hierarchy solution” for dining philosophers. You MAY turn in a fixed/modified version of the bad_philosophers.c code of you can start from scratch. Note that the resource hierarchy solution is discussed in the book and in hundreds of online tutorials. The solution is:

 

Each philosopher runs in an independent thread and each thread is defined as follows:

 

While (1)

 

{ think for some period of time;

when the fork next to me with the smallest number is available, pick it up; when the fork next to me with the largest number is available, pick it up; eat for some period of time;

put one fork down;

put the other fork down;

}

 

Note that the “high” numbered and the “low” numbered fork many NOT always have the same “right fork/left fork” mapping for all the philosopher. The key here is NOT doing left first and then right first or right first and then left first. Rather, it is doing LOWEST NUMBER first and HIGHEST NUMBER second.


Related Questions in computer science category