| 1. The dining philosophers problem:
This example will illustrate the correspondance between the OOP paradigm, the formal description techniques and validation.
The goal of the problem is to create a program that simulates the behavior of 5 philosophers gathered around a table to think and eat. Each philosopher thinks for a while, then eats then thinks again, and so on, independently of the others. Each philosopher has one fork, but needs two forks to eat. Thus the philosophers decide to share their forks. When a philosopher wants to eat, he takes the fork on his left, when it is available, then the fork on his right, eats, and then puts both forks back.
After the philosophers have thought and eaten several times, it may happen that all philosophers simultaneously decide to eat. They take their left fork, and find the right fork already taken by their right neighbor. In this case, if nothing has been foreseen, the philosophers will stay in that situation (deadlock) indefinitely. The program should avoid that situation.
The descriptions below are based on a free interpretation of the OMT models. This example puts the emphasis on the dynamic, and the functional models, which are less well understood than the object model.
Object model:
The philosophers have no data, only a behavior, described below.
The forks have only one piece of data, the boolean indicating when the fork is available, and two operations that act upon it.
201 class Fork {
202 int available;
203 public:
204 void get() { }
205 void put() { }
206 };
Dynamic model:
The graphs of Figure 4 model the behavior of a fork and the one of a philosopher. The first delay in the philosopher's graph represents the time during which the philosopher thinks. The second the delay during which he eats. Fi.get represents the action of taking the fork number i . |