The producer places characters from a text file into the buffer one character at a time; it must wait if the buffer is full. The consumer pulls characters out of the buffer one at a time and prints them to the screen; it must wait if the buffer is empty. Test your solution with a multi-character buffer and with multiple producers and consumers. Of course, with multiple producers or consumers, the output will illustrate the concurrency provided by threads.
The code to allocate and release machines is as follows:
int allocate() { // Returns index of available machine int i; P(nfree); // Wait until a machine is available for (i=0; i < NMACHINES; i++) if (available[i] != 0) { available[i] = 0; return i; } } release(int machine) { // Releases machine available[machine] = 1; V(nfree); // Signal a machine is free }
The available array is initialized to all ones, and nfree is initialized to NMACHINES.