In: Computer Science
C++ ONLY WITH COMMENTS
Question:
Producer / Consumer
Create a program where the application accepts 2 arguments form the command line. The first one is a number where it is a producer of threads and the second one is number of consumer threads. You are allowed to use vector as a buffer and if so then please consider it as the buffer infinite. The producers will create the widgets and will put them on the buffer however the consumer will eat and then remove them from the buffer.
#include<iostream.h>
#include<pthread.h>
#include<semaphore.h>
#include<unistd.h>
using namespace std;
#define BUFFER_SIZE 10;
int buffer[BUFFER_SIZE];
int index=0;
sem_t full , empty;
pthread_mutex_t mutex;
void* produce ( void* arg)
{
while (1)
{ sleep(1);
sem_wait(&empty);
pthread_mutex_lock(&mutex);
int item = rand()%100;
buffer[index++] = item;
cout<<"Produced"<<item<<endl;
pthread_mutex_unlock(&mutex);
sem_post(&full);
}
}
void* consume( void* arg)
{
while (1)
{ sleep(1)
sem_wait(&full);
pthread_mutex_lock(&mutex);
int item = buffer[--index];
cout<<"Consumed"<<item<<endl;
pthread_mutex_unlock(&mutex);
sem_post(&empty);
}
}
int main()
{
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);
}