Concurrent Programming with Java
Lab Manual, Version 1.0, F. Astha Ekadiyanto, 2002.

[Contents] [Next] [Previous]

Lab 6: Monitors and Thread Synchronization 2


Challenge Lab: Solving the Deadlock Problem

Once you have managed to compile all the java codes, try to run the Applet.
You could also provide the nphils parameter to alter the number of philosopher in the applet.

Try to generate a deadlock condition by reducing the Animation Speed scrollbar down to 0 (usually in the range of 100 ms would do so). In this case we just simply reduce the thinking and eating time so that all the philosophers will be busy accessing the forks. This will occasionally fall into the deadlock occurance when by chance all of the philosophers are starting to eat and holds their left and waiting for the right fork to be released by others.

In many cases, finding out whether our code may lead to deadlock or not will not be so practical as this one. It is a software bug that is very difficult to detect.

Here is the Challenge Lab. definition:

By now you will be able to detect that the code is not a deadlock free code.
Try to modify the code so that you can avoid the deadlock (mostly you would just modify the Philosopher Class and/or the Fork Class)!

Hints:

  1. The reason for deadlock is because all the philosophers should always start with their left fork before the right one. Should just any one of them behave differently, deadlock will not be possible (avoid Wait-for cycle).

  2. The deadlock is possible because the philosopher will always hold the fork while waiting for the other fork. If the philosopher is willing to put down the fork that he is holding while waiting for the other one, deadlock will also not be possible (avoid Incremental acquisition). But watch out that starvation may occur!!!

  3. The deadlock is possible because the philosopher do not have a way to communicate with others. If only there is a way to inform neighboring philosopher that they should take turns in using the fork (even if both are hungry, but can define each other's priority), deadlock can be avoided. (avoid No pre-emption).

  4. Of course among other deadlock conditions, Serially reusable resources cannot be avoided to provide mutual exclusion when accessing the forks.

This Challenge Lab. is a required Lab Task. You should submit them via email.


[Contents] [Next] [Previous]