I am trying to solve the Prod-Cons problem with TWO BUFFERS. I have the program
ID: 3547178 • Letter: I
Question
I am trying to solve the Prod-Cons problem with TWO BUFFERS.
I have the program working with ONE BUFFER right now, however I am having trouble getting it converted over to work with two buffers. If someone could help me with what the code should look like so It will work with two buffers i would greatly appreciate it!
My code so far:
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <semaphore.h>
/* Create Buffer */
/* buffer.h */
typedef int buffer_item;
buffer_item buffer[5];
#define BUFFER_SIZE 5
//#include "buffer.h"
/* Semaphore and Mutex lock */
sem_t cEmpty;
sem_t cFull;
pthread_mutex_t mutex;
/* Threads */
pthread_t tid; /* Thread ID */
pthread_attr_t attr; /* Thread attributes */
void *producer(void *param);
void *consumer(void *param);
void init();
/* Progress Counter */
int cg;
main(int argc, char *argv[])
{
/* Variables */
int argarray[3], c1;
/* Argument counter checks */
if(argc != 4)
{
fprintf(stderr, "usage: main [sleep time] [# of producer threads] [# of consumer threads] ");
return -1;
}
/* Get args from command line and change them into integers */
argarray[0] = atoi(argv[1]); /* How long to sleep before ending */
argarray[1] = atoi(argv[2]); /* Producer threads */
argarray[2] = atoi(argv[3]); /* Consumer threads */
/* Error check */
if(argarray[1]<1)
{
fprintf(stderr, "argument 2 must be > 0 ");
return -1;
}
if(argarray[2]<1)
{
fprintf(stderr, "argument 3 must be > 0 ");
return -1;
}
init();
/* Do actual work from this point forward */
/* Create the producer threads */
for(c1=1; c1<=argarray[1]; c1++)
{
pthread_create(&tid, &attr, producer, NULL);
printf("Creating producer #%d ", c1);
}
/* Create the consumer threads */
for(c1=1; c1<=argarray[2]; c1++)
{
pthread_create(&tid, &attr, consumer, NULL);
printf("Creating consumer #%d ", c1);
}
/* Ending it */
sleep(argarray[0]);
printf("Production complete. ");
exit(0);
}
void init()
{
int c2;
pthread_mutex_init(&mutex, NULL); /* Initialize mutex lock */
pthread_attr_init(&attr); /* Initialize pthread attributes to default */
sem_init(&cFull, 0, 0); /* Initialize full semaphore */
sem_init(&cEmpty, 0, BUFFER_SIZE); /* Initialize empty semaphore */
cg = 0; /* Initialize global counter */
for(c2=0;c2<BUFFER_SIZE;c2++) /* Initialize buffer */
{
buffer[c2] = 0;
}
}
void *producer(void *param)
{
/* Variables */
buffer_item item;
while(1)
{
sleep(rand());
item = (rand()); /* Generates random item */
sem_wait(&cEmpty); /* Lock empty semaphore if not zero */
pthread_mutex_lock(&mutex);
if(insert_item(item))
{
fprintf(stderr, "Producer error.");
}
else
{
printf("Producer produced %d ", item);
}
pthread_mutex_unlock(&mutex);
sem_post(&cFull); /* Increment semaphore for # of full */
}
}
void *consumer(void *param)
{
buffer_item item;
while(1)
{
sleep(rand());
sem_wait(&cFull); /* Lock empty semaphore if not zero */
pthread_mutex_lock(&mutex);
if(remove_item(&item))
{
fprintf(stderr, "Consumer error.");
}
else
{
printf("Consumer consumed %d ", item);
}
pthread_mutex_unlock(&mutex);
sem_post(&cEmpty); /* Increments semaphore for # of empty */
}
}
int insert_item(buffer_item item)
{
if(cg < BUFFER_SIZE) /* Buffer has space */
{
buffer[cg] = item;
cg++;
return 0;
}
else /* Buffer full */
{
return -1;
}
}
int remove_item(buffer_item *item)
{
if(cg > 0) /* Buffer has something in it */
{
*item = buffer[(cg-1)];
cg--;
return 0;
}
else /* Buffer empty */
{
return -1;
}
}
Explanation / Answer
#include<stdio.h> // using semaphore
#include<pthread.h>
#include<semaphore.h>
void* producer(void *arg);
void* consumer(void *arg);
char buff[20];
sem_t full,empty;
int main(){
pthread_t pid,cid;
sem_init(&empty,0,1);
sem_init(&full,0,0);
pthread_create(&pid,NULL,producer,NULL);
pthread_create(&cid,NULL,consumer,NULL);
pthread_join(pid,NULL);
pthread_join(cid,NULL);
}
void* producer(void*arg){
int run=1;
while(run){
sem_wait(&empty);
printf(" Enter Mes to be add into buffer:");
scanf("%s",buff);
if(strncmp(buff,"end",3)==0){
run=0;
}
sem_post(&full);
}
return NULL;
}
void* consumer(void *arg){
int run=1;
while(run){
sem_wait(&full);
printf(" Consumed item is %s ",buff);
if(strncmp(buff,"end",3)==0){
run=0;
}
sem_post(&empty);
}
return NULL;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.