Application: The Consumer/Producer Problem 1. The Consumer/Producer problem was
ID: 3791381 • Letter: A
Question
Application: The Consumer/Producer Problem
1. The Consumer/Producer problem was addressed in class and is described in module 5. A solution to this problem is presented on the picture below (in pseudo-language). This particular solution uses 3 semaphores: e, s, and n. Semaphore e is used to protect access to the critical sections. Semaphore e is used to ensure that the producer will not try and produce to a full buffer. Semaphore n is used to ensure that the consumer will not try and consume from an empty buffer.
Solve the Consumer/Producer problem using semaphores. A skeleton program is provided to you: The class Consumer.java represents the consumer, and the class Producer.java represents the producer. You need not modify Consumer.java or Producer.java, however, you should declare and initialize 3 semaphores in BoundedBuffer.java. Those semaphore should be used in the removeItem(..) and addItem(...) methods of BoundedBuffer.
import java.util.Random;
public class Consumer extends Thread {
private BoundedBuffer buffer;
private final Random random = new Random(System.currentTimeMillis());
public Consumer(BoundedBuffer b) {
buffer = b;
}
public void run() {
int item;
while (true) {
try {
//random number from 0 to 9.9999....
float sleepDuration = random.nextInt(10);
Thread.sleep((int) (sleepDuration * 100));
} catch(InterruptedException e){
System.out.println(this.getName() + " got interrupted!");
}
// consume an item from the buffer
System.out.println("Consumer wants to consume.");
item = buffer.removeItem();
}
}
}
import java.util.Random;
public class Producer extends Thread {
private BoundedBuffer buffer;
private final Random random = new Random(System.currentTimeMillis());
public Producer(BoundedBuffer b) {
buffer = b;
}
public void run() {
int item;
while (true) {
try {
//random number from 0 to 9.9999....
float sleepDuration = random.nextInt(10);
Thread.sleep((int) (sleepDuration * 10));
} catch(InterruptedException e){
System.out.println(this.getName() + " got interrupted!");
}
// add an item into the buffer
System.out.println("Producer wants to produce.");
buffer.addItem(random.nextInt(10));
}
}
}
public class BoundedBuffer {
private static final int BUFFER_SIZE = 10;
private int[] buffer;
private int in, out;
private int n;
public BoundedBuffer(){
out = 0; //index of the item to be consumed next
in = 0; //index of the item to be produced next
n = 0; //number of items in the buffer
buffer = new int [BUFFER_SIZE];
}
public void addItem(int item){
//critical section below
n++;
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
System.out.println("Producer entered " + item + ". Buffer Size = " + n);
//critical section above
}
public int removeItem(){
//critical section below
n--;
int item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
System.out.println("Consumer consumed " + item + ". Buffer Size = " + n);
//critical section above
return item;
}
public static void main(String[] args){
BoundedBuffer buffer = new BoundedBuffer();
Producer pThread = new Producer(buffer);
Consumer cThread = new Consumer(buffer);
pThread.start();
cThread.start();
}
}
Please provide your code (modified BoundedBuffer) and a sample output
import java.util.Random;
public class Consumer extends Thread {
private BoundedBuffer buffer;
private final Random random = new Random(System.currentTimeMillis());
public Consumer(BoundedBuffer b) {
buffer = b;
}
public void run() {
int item;
while (true) {
try {
//random number from 0 to 9.9999....
float sleepDuration = random.nextInt(10);
Thread.sleep((int) (sleepDuration * 100));
} catch(InterruptedException e){
System.out.println(this.getName() + " got interrupted!");
}
// consume an item from the buffer
System.out.println("Consumer wants to consume.");
item = buffer.removeItem();
}
}
}
Explanation / Answer
This particular solution uses 3 semaphores: e, s, and n. Semaphore e is used to protect access to the critical sections. Semaphore e is used to ensure that the producer will not try and produce to a full buffer. Semaphore n is used to ensure that the consumer will not try and consume from an empty buffer.
semaphore e is repeated above with its meaning. so I using a mutex, empty and full with their name . you can replace terms as needed.what new I have added to bounded buffer is commented with the declaration and initailazation and all.
public class BoundedBuffer {
private static final int BUFFER_SIZE = 10;
//semapphore declare
private Semaphore mutex;
private Semaphore empty;
private Semaphore full;
private int[] buffer;
private int in, out;
private int n;
public BoundedBuffer(){
out = 0; //index of the item to be consumed next
in = 0; //index of the item to be produced next
n = 0; //number of items in the buffer
buffer = new int [BUFFER_SIZE];
// inialized
mutex = new Semaphore(1);
empty = new Semaphore(BUFFER_SIZE);
full = new Semaphore(0);
}
public void addItem(int item){
//aquired (new line)
empty.acquire();
mutex.acquire();
//critical section below
n++;
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
System.out.println("Producer entered " + item + ". Buffer Size = " + n);
//critical section above
//realsed
mutex.release();
full.release();
}
public int removeItem(){
//aquired (new line)
full.acquire();
mutex.acquire();
//critical section below
n--;
int item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
System.out.println("Consumer consumed " + item + ". Buffer Size = " + n);
//critical section above
//realsed
mutex.release();
empty.release();
return item;
}
public static void main(String[] args){
BoundedBuffer buffer = new BoundedBuffer();
Producer pThread = new Producer(buffer);
Consumer cThread = new Consumer(buffer);
pThread.start();
cThread.start();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.