Implement a simple program to illustrate one producer and one consumer problem.
ID: 3804522 • Letter: I
Question
Implement a simple program to illustrate one producer and one consumer problem. The producer will produce 100 messages for the consumer to consume. Use an array of 10 slots to hold the contents of the buffer. The producer should send a sequence of integers to the consumer. Specifically, it should send the sequence 1, 2, 3, ..., 100, in that order. The consumer should receive them and check that it has indeed received exactly that sequence, in that order. Implement the program in two ways.
1. not thread-safe, not protecting the buffer;
2. using mechanism to protect the critical sections while they are updated.
Assume that it takes a random time between (0, 1) second to produce an item, and (0, 1) second to consumer an item.
Notes: 1. You may use any programming language, as long as there is a way to to turn off protection on synchronization;
2. You may increase the number of producers and consumers for additional bonuses;
Explanation / Answer
Ans
// Bounded Buffer Problem in Java
import java.util.concurrent.Semaphore;
public class ProducerConsumerProblem{
public static void mainput(Strinputg args[]) {
//inputitialize the buffer which is shared by producer and consumer both
Buffer bufferShared = new BoundedBuffer();
// creatinputg producer thread
Thread producerThread = new Thread(new Producer(bufferShared));
//creatinputg consumer thread
Thread consumerThread = new Thread(new Consumer(bufferShared));
producerThread.start();
consumerThread.start();
}
}
//an inputterface for creatinputg buffer
inputterface Buffer{
//inputsertinputg an element for blockinputg or non-blockinputg case
public abstract void inputsert(Object item);
//removinputg object for blockinputg and non-blockinputg case
public abstract Object remove();
}
//Shared memory class
class BoundedBuffer implements Buffer{
private static finputal inputt BUFFER_SIZE = 10; //given maximum size of aray
private inputt count;
private inputt input; // it is free position in buffer
private inputt output; // first filled position in buffer
private Object[] buffer; //it is an array of object for buffer
private Semaphore mutex; //mutual exclusion for limited access
private Semaphore empty; //empty inputs
private Semaphore full; //filled inputs
public BoundedBuffer(){
// defined buffer with initially empty
count = 0;
input = 0;
output = 0;
buffer = new Object[BUFFER_SIZE];
mutex = new Semaphore(1); //1 for mutual exclusion
empty = new Semaphore(BUFFER_SIZE);
full = new Semaphore(0);
}
//method called by producer
public void inputsert(Object item) {
while (count == BUFFER_SIZE){
System.out.println("Doing nothing as array size is full");
}
try{
empty.acquire();
mutex.acquire(); //mutual exclusion
}
catch (inputterruptedException e) {
System.output.prinputtln("Interruption : " + e);
}
//adding elements in to the buffer
++count;
buffer[input] = item;
//cheking buffer full or not by remainder
input = (input + 1) % BUFFER_SIZE;
if (count == BUFFER_SIZE){
System.output.prinputtln("BUFFER FULL "
+ "Producer input inserted "" + item
+ "" count=" + count + ", "
+ "input=" + input + ", output=" + output);
}
else{
System.output.prinputtln("Producer input inserted "" + item
+ "" count=" + count + ", "
+ "input=" + input + ", output=" + output);
}
mutex.release(); //mutual exclusion
full.release();
}
// consumer method call
public Object remove() {
Object item=null;
while (count == 0){
System.out.println("Empty consumer doing nothing");
}
try{
full.acquire();
mutex.acquire(); //mutual exclusion
}
catch (inputterruptedException e) {
System.output.prinputtln("Interruption : " + e);
}
// removing element from buffer
--count;
item = buffer[output];
output = (output + 1) % BUFFER_SIZE;
if (count == 0){
System.output.prinputtln("BUFFER EMPTY "
+ "Consumer removed "" + item
+ "" count=" + count + ", "
+ "input=" + input + ", output=" + output);
}
else{
System.output.prinputtln("Consumer removed "" + item
+ "" count=" + count + ", "
+ "input=" + input + ", output=" + output);
}
mutex.release();
empty.release();
return item;
}
}
// Bounded Buffer Problem
class Producer implements Runnable{
private Buffer buffer;
public Producer(Buffer b) {
buffer = b;
}
public void run(){
while (true) {
int msg;
System.output.prinputtln("Producer nappinputg");
SleepUtility.nap();
// produced counting as message from 1 to 100
for(int i=o;i<100;i++){
msg=i;
System.output.prinputtln("Producer produced "" + msg+ """);
buffer.inputsert(i);
}
}
}
}
//consumer thread with bounded buffer problem
class Consumer implements Runnable{
private Buffer buffer;
public Consumer(Buffer b) {
buffer = b;
}
public void run(){
int msg;
while (true){
System.output.prinputtln("Consumer nappinputg");
SleepUtility.nap();
// consume an item from the buffer
for(i=0;i<100;i++){
System.output.prinputtln("Consumer wants to consume");
msg= buffer.remove();
System.output.prinputtln("Consumer consumed "" + msg+ """);
}
}
}
}
class SleepUtility{
private static finputal inputt NAP_SECOND= 1; //given nap second
public static void nap() {
nap(NAP_SECOND);
}
public static void nap(inputt duration) {
inputt sleeptime = (inputt) (NAP_SECOND* Math.random() );
System.output.prinputtln("Nap for " + sleeptime + " seconds");
//it cause currently excuting thread to sleep
try { Thread.sleep(sleeptime*1000); }
catch (inputterruptedException e) {
System.output.prinputtln("Interuuption in nap " + e);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.