In: Computer Science
In Java, Modify “Producer and Consumer Problem” from lecture note so that it can use all buffer space, not “buffer_size – 1” as in the lecture note. This program should work as follows:
1. The user will run the program and will enter two numbers on the command line. Those numbers will be used for buffer size and counter limit.
2. The main program will then create two separate threads, producer and consumer thread.
3. Producer thread generates a random number through random number generator function and inserts this into buffer and prints the number. Increment counter.
4. Consumer thread goes to the buffer and takes a number in the proper order and prints it out. Increment counter.
5. After counter reaches its limit, both threads should be terminated and return to main.
6. Main program terminates.
solution:
C++ languages
having the POSIX thread(pthread) .It allows us to create multiple threads for achieving synchronisation in producer -consumer problem. It is major effective on the patform of multiprocessor or multi-core systems where threads can be implementedat kernel level and also results in the speed of execution.
c++ has pthraed.h library which is used to create thread and perform its function.
//file name : thread.cpp //library imported #include <iostream> #include <pthread.h> #include <semaphore.h> #include <random> #include <unistd.h> using namespace std; //intialisation of global value; int *buffer; int index_value=0; int counter; int count=0; sem_t full,empty; pthread_mutex_t mutex; //method to create buffer of size n; void create(int n) { buffer = new int[n]; } void intialise(int value) { counter=value; } //medthod for producer in producer consumer problem void* produce(void* arg){ //loop for counter while(1 && count<counter+1){ //sleep declared as 1; sleep(1); //semaphore opeartion when empty sem_wait(&empty); pthread_mutex_lock(&mutex); //generting item value between 1 to 100 int item = rand()%100; //buffer intialisation and index increment buffer[index_value++] = item; //count increases to counter while reaches counter count++; //message cout<<"Produced "<<item<<endl; pthread_mutex_unlock(&mutex); sem_post(&full); } return NULL; } void* consume(void* arg){ while(1 && count<counter+1){ sleep(1); sem_wait(&full); pthread_mutex_lock(&mutex); //decrement index and buffer indexdecrement int item = buffer[--index_value]; //message cout<<"Consumed "<<item<<endl; cout<<"count : "<<count<<" counter : "<<counter<<endl; pthread_mutex_unlock(&mutex); sem_post(&empty); } return NULL; } int main() { cout<<"Enter your Buffer size : "<<endl; int BUFFER_SIZE,counter_; cin>>BUFFER_SIZE; cout<<"Enter Your Counter Size : "<<endl; cin>>counter_; create(BUFFER_SIZE); intialise(counter_); pthread_t producer,consumer; sem_init(&empty,0,BUFFER_SIZE); sem_init(&full,0,0); pthread_mutex_init(&mutex,NULL); pthread_create(&producer,NULL,produce,NULL); pthread_create(&consumer,NULL,consume,NULL); pthread_exit(NULL); return 0; }
Explanation :
Steps should follow to run the program
if you have g++ compiler then for compiling in the terminal type g++ thread.cpp -pthread for compiation
other wise it will give you error pthread as a undefined refrences
output :
please give me thumb up