Sunday, 20 September 2015

bounded buffer semaphore problem source code, operating system,

bounded buffer semaphore problem source code, operating system,
Assalam o alaikum Friends how are you all?
bounded buffer semaphore problem source code, operating system, More tutorials SEO , HTML TUTORIAL, CSS TUTORIAL, ANDROID TUTORIAL





typedef int buffer_item;
#define BUFFER_SIZE 5

#include"stdafx.h";
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
//#include "buffer.h"

#define RAND_DIVISOR 100000000
#define TRUE 1

pthread_mutex_t mutex;
sem_t full, empty;
buffer_item buffer[BUFFER_SIZE];
int counter;

pthread_t tid;      
pthread_attr_t attr; 

void *producer(void *param); 
void *consumer(void *param); 

void DATA() {

   pthread_mutex_init(&mutex, NULL);
   sem_init(&full, 0, 0);
   sem_init(&empty, 0, BUFFER_SIZE);
   pthread_attr_init(&attr);

  
   counter = 0;
}

void *producer(void *param) {
   buffer_item item;

   while(TRUE) {
      int rNum = rand() / RAND_DIVISOR;
      sleep(rNum);
      item = rand();
      sem_wait(&empty); 
      pthread_mutex_lock(&mutex);

      if(insert_item(item)) {
         fprintf(stderr, " Producer error condition\n");
      }
      else {
         cout<<("producer produced \n")<<item;
      }
      pthread_mutex_unlock(&mutex);
      sem_post(&full);
   }
}
void *consumer(void *param) {
   buffer_item item;

   while(TRUE) {
      int rNum = rand() / RAND_DIVISOR;
      sleep(rNum);
      sem_wait(&full);
      pthread_mutex_lock(&mutex);
      if(remove_item(&item)) {
         fprintf(stderr, "Consumer  error condition\n");
      }
      else {
         cout<<("consumer consumed\n")<<item;
      }
      pthread_mutex_unlock(&mutex);
      sem_post(&empty);
   }
}


int insert_item(buffer_item item) {
   if(counter < BUFFER_SIZE) {
      buffer[counter] = item;
      counter++;
      return 0;
   }
   else { 
      return -1;
   }
}


int remove_item(buffer_item *item) {
   if(counter > 0) {
      *item = buffer[(counter-1)];
      counter--;
      return 0;
   }
   else { 
      return -1;
   }
}

int main(int argc, char *argv[]) {
   int i;

   /* Verify the correct number of arguments were passed in */
   if(argc != 4) {
      fprintf(stderr, "USAGE:./main.out <INT> <INT> <INT>\n");
   }

   int mainSleepTime = atoi(argv[1]); /* Time in seconds for main to sleep */
   int numProd = atoi(argv[2]); /* Number of producer threads */
   int numCons = atoi(argv[3]); /* Number of consumer threads */
   DATA();

   for(i = 0; i < numProd; i++) {
      pthread_create(&tid,&attr,producer,NULL);
    }
   for(i = 0; i < numCons; i++) {
      pthread_create(&tid,&attr,consumer,NULL);
   }

   /* Sleep for the specified amount of time in milliseconds */
   sleep(mainSleepTime);
   printf("Exit the program\n");
   exit(0);
}



Dear Readers if you have any query/question feel free to ask me via comment box given below. Also Follow us on social media site and share that post with your friends. - See more at: http://onlinecomputercafe.blogspot.com 

useful tutorial keep it up...
EmoticonEmoticon

:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:o
:>)
(o)
:p
:-?
(p)
:-s
8-)
:-t
:-b
b-(
(y)
x-)
(h)

LinkWithin