In: Computer Science
Java Code
The producer and consumer will share an integer array with a length of 5 which is a circular buffer. The producer and consumer will also share one or two (your choice) variables to coordinate the placing and removal of items from the circular buffer.
The producer process consists of a loop that writes the loop count (a value from 0 to 99) into the shared buffer. On each pass through the loop, before the producer writes into the shared variable, it does a random wait of from one to five seconds (compute a new random wait value on each pass through the loop). The loop is to be executed 100 times. Each time through the loop, it places the loop count into the shared circular buffer if there is an available slot in the buffer, and updates the variable(s) used to control the use of the circular buffer (note that this will have to rap around as the array is only 5 items long). The producer must not clobber any item in the buffer that has not been read by the consumer. If the list is full, the producer must wait for the consumer to remove one or more items from the list before placing the next item in it. This wait is inside the main loop and must have a 1 second sleep in it. The buffer may get full because the producer may be faster than the consumer at times. When the producer puts its last entry into the buffer, it must add one more entry, a -1 which indicates to the consumer that it has completed.
The consumer process consists of a loop that reads from the shared circular buffer until the “I’m done” flag (the -1) is detected in the shared buffer indicating that the transfer is complete. On each pass through the loop, before it reads from the shared buffer, the consumer does a random wait of from two to five seconds (compute a new random value on each pass through the loop). It will only read a value from the shared circular buffer if there is one in the buffer it hasn’t read. If there is nothing to read, it must wait until there is something to read. When it has to wait it is to write “consumer waiting” into the output file. When waiting it should do so in a loop that has a 1 second sleep in it and write the “consumer waiting” message into the file each pass through this wait loop. The consumer must use the variable(s) it shares with the producer to determine which item in the circular buffer is the next item (remember the shared buffer is circular). After the consumer reads a value from the shared circular buffer, it writes the value into an output file and updates the variable(s) it shares with the producer (again note that this has to take into consideration the fact that this index will rap abound) to indicate that this entry is now available for use as the entry has been processed. When it completes, the consumer writes the phrase “Consumer done” into the output file. Note that the entry just before this should be 99 (the last item removed from the circular buffer). Do not write the -1 into the output buffer.
import java.util.concurrent.Semaphore;
class Q {
int item;
static Semaphore semCon = new Semaphore(0);
static Semaphore semProd = new Semaphore(1);
// to get an item from buffer
void get()
{
try {
// Before consumer can consume an item,
// it must acquire a permit from semCon
semCon.acquire();
}
catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
// consumer consuming an item
System.out.println("Consumer consumed item : " + item);
// After consumer consumes the item,
// it releases semProd to notify producer
semProd.release();
}
// to put an item in buffer
void put(int item)
{
try {
// Before producer can produce an item,
// it must acquire a permit from semProd
semProd.acquire();
}
catch (InterruptedException e) {
System.out.println("InterruptedException caught");
}
// producer producing an item
this.item = item;
System.out.println("Producer produced item : " + item);
// After producer produces the item,
// it releases semCon to notify consumer
semCon.release();
}
}
// Producer class
class Producer implements Runnable {
Q q;
Producer(Q q)
{
this.q = q;
new Thread(this, "Producer").start();
}
public void run()
{
for (int i = 0; i < 5; i++)
// producer put items
q.put(i);
}
}
// Consumer class
class Consumer implements Runnable {
Q q;
Consumer(Q q)
{
this.q = q;
new Thread(this, "Consumer").start();
}
public void run()
{
for (int i = 0; i < 5; i++)
// consumer get items
q.get();
}
}
// Driver class
class PC {
public static void main(String args[])
{
// creating buffer queue
Q q = new Q();
// starting consumer thread
new Consumer(q);
// starting producer thread
new Producer(q);
}
}