|
|
||||||
|
|
This article is dedicated to the famous dining philosophers' problem, which was proposed by Edward Dijkstra in 1965. The problem is a classical demonstration of deadlock problems in the concurrent programming.
Suppose there is a circular table with a bowl of spaghetti in the center. Five philosophers sit around a table; they spend their lives alternately thinking and eating. In order to eat, a philosopher needs to have a fork in his left hand and a fork in his right hand. The problem is that there are only five forks on the table, one between each plate on the table. A solution must be proposed in which each philosopher will not starve and in which there will never be a situation of deadlock. Philosophers all act concurrently, and will attempt to eat immediately when they stop thinking. This is a Dijkstra's original formulation of the problem; since most philosophers (as well as most programmers) can eat spaghetti with a single fork, modern writers use the Chinese food and chopsticks instead of spaghetti and forks. The diners must cooperate to remain alive. Each right fork is someone else's left one, so if each philosopher first acquires his right fork, holding it greedily until he can acquire the other chopstick, all philosophers will starve. This is a classical circular-wait deadlock. There are several non-deadlocking solutions; but let's make a program that demonstrates the deadlock. In the next articles we'll improve the program by adding deadlock-preventing logic. Program example.The source code contains three classes and three modules: Main.pas contains TForm1 class that implements the application's main form; ForksUnit.pas contains TForks class - the fork manager that serves philosopher's request and distributes forks among philosophers; Philosopher.pas contains TPhilosopher class that represents a philosopher's lifecycle. Each philosopher is an individual working thread, the thread executes the following actions cyclically: thinking, waiting for the left fork, taking the left fork when it is free, waiting for the right fork, taking the right fork, eating. The philosopher status can be represented if a form of a picture that is changed depending on the status. For the simplicity the philosopher can be represented by the check box: The fork can be represented by a radio button: You can see the program's screenshot bellow
Full source code is available at http://www.suite101.com/files/topics/7240/files/p10.zip . The program is written for Borland Delphi 4, but this code must work under Delphi 2/3/4/5/6. I would like to answer any questions that you may have.
Go To Page: 1
The copyright of the article The Dining Philosophers Problem. Part 1. in Delphi Programming is owned by . Permission to republish The Dining Philosophers Problem. Part 1. in print or online must be granted by the author in writing.
|
|||||
|
|
||||||