ece çalıkuş
ece çalıkuş

Reputation: 139

Producer-Consumer Implementation

I need to implement producer-consumer problem in my project. N consumers and M producers will be created. A producer will use publish(v) call to reach v data to consumer. A consumer will use get_data(v) call to get a copy of data v. I really don't know how to implement it. Please help me.

I am going to use C to implement it. I will create n process for consumers and m process for producers. If a producer publish a data, other producers can not do it until all consumers get it. I will use semaphores and shared memory to exchange data.

I found something which does similar job. But it is using threads but i need process instead. How can i change this.

#include <pthread.h>
#include <stdio.h>
#include <semaphore.h>

#define BUFF_SIZE 4
#define FULL 0
#define EMPTY 0
char buffer[BUFF_SIZE];
int nextIn = 0;
int nextOut = 0;

sem_t empty_sem_mutex; //producer semaphore
sem_t full_sem_mutex; //consumer semaphore

void Put(char item)
{
int value;
sem_wait(&empty_sem_mutex); //get the mutex to fill the buffer

buffer[nextIn] = item;
nextIn = (nextIn + 1) % BUFF_SIZE;
printf("Producing %c ...nextIn %d..Ascii=%d\n",item,nextIn,item);
if(nextIn==FULL)
{
  sem_post(&full_sem_mutex);
  sleep(1);
}
sem_post(&empty_sem_mutex);

}

 void * Producer()
{
  int i;
  for(i = 0; i < 10; i++)
{
  Put((char)('A'+ i % 26));
}
}

void Get()
{
int item;

sem_wait(&full_sem_mutex); // gain the mutex to consume from buffer

item = buffer[nextOut];
nextOut = (nextOut + 1) % BUFF_SIZE;
printf("\t...Consuming %c ...nextOut %d..Ascii=%d\n",item,nextOut,item);
if(nextOut==EMPTY) //its empty
{
  sleep(1);
}

sem_post(&full_sem_mutex);
}

void * Consumer()
{
int i;
for(i = 0; i < 10; i++)
{
  Get();
}
}

int main()
{
  pthread_t ptid,ctid;
  //initialize the semaphores

  sem_init(&empty_sem_mutex,0,1);
  sem_init(&full_sem_mutex,0,0);

  //creating producer and consumer threads

   if(pthread_create(&ptid, NULL,Producer, NULL))
   {
  printf("\n ERROR creating thread 1");
  exit(1);
    }

 if(pthread_create(&ctid, NULL,Consumer, NULL))
  {
  printf("\n ERROR creating thread 2");
  exit(1);
   }

 if(pthread_join(ptid, NULL)) /* wait for the producer to finish */
  {
  printf("\n ERROR joining thread");
  exit(1);
  }

  if(pthread_join(ctid, NULL)) /* wait for consumer to finish */
   {
  printf("\n ERROR joining thread");
  exit(1);
}

  sem_destroy(&empty_sem_mutex);
  sem_destroy(&full_sem_mutex);

  //exit the main thread

    pthread_exit(NULL);
  return 1;
  }

Upvotes: 2

Views: 6090

Answers (1)

Tsvetomir Dimitrov
Tsvetomir Dimitrov

Reputation: 648

I'd suggest you to make a plan and start reading. For example:

  1. Read about how to create and manage threads. Hint: pthread.
  2. Think how will the threads communicate - usually they use common data structure. Hint: message queue
  3. Think how to protect the data structure, so both threads can read and write safely. Hint: mutexes.
  4. Implement consumer and producer code.

Really, if you want more information you have to work a bit and ask more specific questions. Good luck!

Upvotes: 3

Related Questions