/******************************************************************
* Memorial University of Newfoundland
* 8893 Concurrent Programming
* Illustrate the Bakery Algorithm.
*
* @version 2001.01
* @author Dennis Peters
*
* $State$
* $RCSfile$ $Date$
* $Revision$
* $Author$
*
******************************************************************/
public class Bakery
{
/******************************************************************
* @param args the command line arguments
******************************************************************/
public static void main(String[] args)
{
Turn t = new Turn(5);
Thread cs0 = new Thread(new CS(0, t, 5));
Thread cs1 = new Thread(new CS(1, t, 5));
Thread cs2 = new Thread(new CS(2, t, 5));
Thread cs3 = new Thread(new CS(3, t, 5));
Thread cs4 = new Thread(new CS(4, t, 5));
cs0.start();
cs1.start();
cs2.start();
cs3.start();
cs4.start();
try {
while (cs0.isAlive()) Thread.sleep(50);
while (cs1.isAlive()) Thread.sleep(50);
while (cs2.isAlive()) Thread.sleep(50);
while (cs3.isAlive()) Thread.sleep(50);
while (cs4.isAlive()) Thread.sleep(50);
}
catch (InterruptedException e) {}
System.out.println("Done");
System.exit(0);
}
}
/******************************************************************
* Turn -- keep track of whose turn it is.
******************************************************************/
class Turn
{
public int val[];
private int size;
Turn(int n)
{
size = n;
val = new int[n];
for (int i = 0; i < n; i++) {
val[i] = 0;
}
}
public int max()
{
int result = 0;
for (int i = 0; i < size; i++) {
if (result < val[i]) {
result = val[i];
}
}
return result;
}
}
/******************************************************************
* Process trying to do critical section
******************************************************************/
class CS
implements Runnable
{
private int n; // number of processes
private int pid; // My process id
private Turn turn; // Turn array.
/******************************************************************
* @param id process id
* @param t reference to turn array
******************************************************************/
CS(int id, Turn t, int num)
{
pid = id;
turn = t;
n = num;
}
/******************************************************************
* Method invoked by start.
******************************************************************/
public void run()
{
try {
for (int r = 1; r <= 20; r++) {
Thread.sleep((int)Math.round(Math.random()*10));
// Entry protocol
System.out.println(pid + " trying to enter...");
turn.val[pid] = 1;
turn.val[pid] = turn.max() + 1;
for (int j = 0; j < n; j++) {
if (j != pid) {
while (turn.val[j] != 0 &&
(turn.val[pid] > turn.val[j] ||
(turn.val[pid] == turn.val[j] && pid > j)))
Thread.sleep((int)Math.round(Math.random()*1));
}
}
// Critical section
System.out.println(pid + " is IN CS.");
Thread.sleep((int)Math.round(Math.random()*10));
// Exit protocol
turn.val[pid] = 0;
System.out.println(pid + " is OUT of CS.");
// noncritical section
Thread.sleep((int)Math.round(Math.random()*10));
}
}
catch (InterruptedException e) {}
}
}
/******************************************************************
* REVISION HISTORY
*
* $Log$
*
******************************************************************/