PP Assignment 2: Pub Simulation

Graphic1

The Problem

An owner of a Public House suspects that the way in which the bar is operated means that customers have to spend too much time waiting to be served, and so tend to go elsewhere instead. In order to evaluate the situation a simulation of the Bar has been commissioned. Initially this simulation will simply run with text output describing the events as they occur in the Bar, and collect a minimal amount of statistical data.

Intension of assignment

Even if valuable to the owner, the simulation is not the main purpose of this assignment - indeed, if so was the case there are much better techniques for simulating than writing a concurrent program.

The requirement of this assignment is to write a program in which synchronization and communication takes place between several concurrent processes. It is intended to force you to solve (and not simply avoid) a range of interesting synchronisation problems.


The Pub

The pub consists of a number of tables, a serving area containing a beer tap and a cupboard (holding the clean glasses, cups, milk, coffee and chocolate), a clock, a Landlord, a Barmaid, an Assistant, and a number of customers. The Pub is operated by the Landlord and Barmaid, whose job is to make and serve drinks to customers who order at the Bar in a first-come-first-served manner. Each customer may only order a single drink at one time. The Landlord and Barmaid can each serve only a single customer at a time, although others may be waiting to be served.

The Landlord and Barmaid

The Serving area

The serving area consists of the beer tap and a cupboard. The cupboard (or bar disk) contains all the ingredients, and the cups and glasses.

The Customers

The Tables

There are a number of tables in the bar that the customer use to put down their glasses and cups after finishing their drink.

The Assistant

The Assistant has the job of clearing the glasses and cups away from each table in turn, washing them and placing them back in the cupboard once they are all washed.

The Clock

The clock in the bar serves two purposes.

The Statistics

At the end of the simulation, i.e. when all other processes have terminated cleanly, the landlord should do some sanity checks of the Bar and print out some statistics on the run. The result of the sanity checks must be printed. You must


The Last Orders Problem

As anyone in the UK will tell you, last orders is a serious problem. It's a problem with the pub lab too - namely getting the program to terminate cleanly once the landlord has called for last orders. Here are some hints of how to solve the problem - and how not to solve it.

Non solution 1 - Really bad

Use a global variable that customers check before they order.

There are two major drawbacks with this solution:

Non solution 2 - just bad

This one works like the previous example, but you let the customers drink with small sips, checking once every second if last call have passed.

Once again, this is polling, and besides, this method doesn't help you understand anything more about concurrent programming - which is the main goal of the assignment. Customers are interrupted, but it's not a nice environment to have a beer if you have to check if the place is closing between every sip.

Non solution 3 - not OK

Before you start drinking, calculate the time left until last call, and don't sleep longer than that

This way is not very realistic. In concurrent programming, you hardly ever know in advance when things will happen. Using this solution, you wouldn't learn anything useful.

Possible solutions - callbacks and timeouts

A potential problem is if you assume a strict client-server relationship between landlord and customer. One way around this limitation is to ensure that every customer that comes into the pub says "Hello" to the landlord and hands over a pointer to himself (or to a personal channel) to the landlord (who stores it in some structure). When they leave, they say goodbye, so he can remove that pointer again. When last call occurs, the landlord has a means to communicate with every customer. He also knows how many are in the pub. The details of what you do depend on the structure of your solution, and the programming language you are using.

Drinking: Waiting not Sleeping!

Note that the process of drinking (for those thirsty customers) should be viewed as a timeout rather than a "sleep": a thirsty customer is waiting for a last orders call, and he times out when his drink is finished.

Not allowed! (Java)

It might be tempting to use the Thread.interrupt() method to wake sleeping processes. This is a bad idea. Firstly, we have seen what a mess people can get into with this! Secondly, a behaviour which is present in every execution of the program is not exceptional, and is usually considered bad programming style to use an exception in such cases. In summary, don't use Thread.interrupt().


Implementation

You must implement your simulation in JR. It is conceptually a better language than java for this task. Previous years' statistic (when using java has been allowed) has shown us that students make fewer errors in JR.


Demands

Documentation

The documentation is as important as the laboration itself. You will be failed if you ignore this demand. There are some specific points you should address in your documentation of Lab 2.

Code quality

Configuration

There must be a class with only constants. One example of such a class can be found here.

The following constants must be available:

Output of the program

In order to see what is happening dynamically you must have output from the Customers, the Assistant, the Landlord, the Barmaid and the Clock reporting all their major events.

Add information about which process/thread is doing the output. This way you can see if a process/thread acts for another, which is strictly forbidden, but is a common error for Java solutions (objects are not processes!). An example of such incorrect behaviour is

Thread-Landlord : 21.31: Landlord: Pelle is served!
main : 21.50: Landlord: Last orders!
Thread-Customer-8 : 21.50: Kalle is going to order beer from Landlord.

Where you can see that not only the landlord thread but also the main thread is acting for the landlord.

Note that realistic time stamps are not required, it is fine to use the java.lang.System.nanoTime() function to generate them.

You can set the name of a process like this: Thread.currentThread().setName("Landlord");. Which would allow doing output without specifying the name of who is doing the output by using a method such as this:

public static void output(String message) {
	System.out.println(
		System.nanoTime() + ": " +
		Thread.currentThread().getName() + ": " +
		message);
}

You must not


Tips


Credits


Valid HTML 4.01!