Producer Consumer- Operating Systems

In this project we are going to resolve the producer-consumer problem with limited buffer using threads. The produces produces an object and adds it to buffer while the customer consumes it, then removes it from the buffer. The problem arises in the fact that the producer and the consumer should not be allowed to access the shared buffer at the same time, as this will generate problems.
Solving this problem can be implemented in different ways in this case will use pthreads. For this solution, we will need two traffic lights, counter for empty, full and a mutex lock. Producer and consumer will be executed as two independent threads. Buffer shall be a fixed-size array type buffer_item (defined using typedef). He will be manipulated as a circular row using two functions for adding and removing objects. Both thread will alternate between a time when they will manipulate the buffer and a time where they are in "sleep". 
As an output the program will have messages that appear when creating or consuming objects.
To implement the producer and the consumer we will use two separate threads, the producer will produce objects which are added to the buffer and the consumer will consume these items, then remove them from the buffer. If the buffer is full the producer must wait until there is a place, and the consumer must wait if the buffer does not have any element.          Work start by creating two files that we will need buffer.h, which will serve as a header that will be imported into the main file and file producer-consumer.c that will have the code in the C language where it will be the solution problem.
buffer.h
/* 
 * Header for producer-consumer.c
 * buffer.h
 */

typedef int buffer_item;
#define BUFFER_SIZE 5
This code sets buffer.h header. In the first line of this code we use typedef key phrase that names a new type of data t established by us, to be called buffer_item which will hold all the elements of buffer. Then by #define decide the size of the buffer that will be 5 will therefore contain 5 elements.
prodhues-konsumator.c
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include "buffer.h"
// Global variales
buffer_item buffer[BUFFER_SIZE];
pthread_mutex_t mutex;
sem_t full, empty;
int counter, in, out;
// Function prototype
int insert_item(buffer_item item);
int remove_item(buffer_item *item);
void *consumer(void *param);
void *producer(void *param);
void inicializo()
{
  pthread_mutex_init(&mutex, NULL);
  sem_init(&empty, 0, BUFFER_SIZE); // All of buffer is empty
  sem_init(&full, 0, 0);
  count = in = out = 0;
}
int insert_item(buffer_item item)
{
  int success;
  sem_wait(&empty);
  pthread_mutex_lock(&mutex);
  // Add elements to buffer
  if( counter<BUFFER_SIZE)
  {
    buffer[in] = item;
    in = (in + 1) % BUFFER_SIZE;
    count++;
    success = 0;
   }
  else
    success = -1;
  pthread_mutex_unlock(&mutex);
  sem_post(&full);
  return success;
}



//Remove elements from buffer
int remove_item(buffer_item *item)
{
  int success;
  sem_wait(&full);
  pthread_mutex_lock(&mutex);
 
  // remove
  if( counter> 0){
    *item = buffer[out];
    out = (out + 1) % BUFFER_SIZE;
    count--;
    success = 0;
  }
  else
    success = -1;
  pthread_mutex_unlock(&mutex);
  sem_post(&empty);
  return success;

}
void *producer(void *param)
{
  buffer_item item;
  while(1)
{
    sleep(rand() % 5 + 1); 
    item = rand();
    if(insert_item(item))
      printf("Error:Producer\n");
    else
      printf("Object is produced %d\n", item);
  }
}

void *consumer(void *param)
{
  buffer_item item;
  while(1)
{
    sleep(rand() % 5 + 1);
    if(remove_item(&item))
      printf("Error:Consumer\n");
    else
      printf("Object Consumed%d\n", item);
  }
}

int main(int argc, char *argv[])
{
  if (argc != 4){
    printf("ERROR: Vendos 3 argumenta\n");
    return 0;
  }
  // Merr argumentet nga command line
  const long int sleepTime = strtol(argv[1], NULL, 0);
  const long int num_producer = strtol(argv[2], NULL, 0);
  const long int num_consumer = strtol(argv[3], NULL, 0);
  inicializo();
  pthread_t prodhuesit[numProd];
  pthread_t konsumatoret[numKons];
  for(int i = 0; i < numProd; i++)
    {
      pthread_create(&prodhuesit[i], NULL, prodhues, NULL);
      printf(“Duke krijuar prodhuesin %d \n”, i);
    }
  for(i = 0; i < numKons; i++)
    {
    pthread_create(&konsumatoret[i], NULL, konsumator, NULL);
    printf(“Duke krijuar konsumatorin %d \n”, i);
   }
  sleep(sleepTime);
  printf(“Dil nga programi”);
return 0;}

Comments

Popular posts from this blog

Configure IP address on Oracle Linux

Oracle Directory Object

TIME INTERVAL in Oracle