The local laundromat has just entered the computer age. As each customer enters, he or she puts coins into slots at one of several stations and types in the number of washing machines he/she will need. The stations are connected to a central computer that automatically assigns available machines and outputs tokens that identify the machines to be used. The customer puts laundry into the machines and inserts each token into the machine indicated on the token. When a machine finishes its cycle, it informs the computer that it is available again.
The computer maintains a boolean array available[NMACHINES] to represent if corresponding machines are available (NMACHINES is a constant indicating how many machines there are in the laundromat), and a semaphore nfree that indicates how many machines are available. The code to allocate and release machines is as follows.
The available array is initialized to all true, and nfree is initialized to NMACHINES. For each machine that the user has requested from a station, the station invokes the allocate procedure before issuing a token.
int allocate() /* Returns index of
available machine.*/
{
void release(int machine) /* Releases
machine */
{
After a few weeks of operation, two types of problems have started appearing: