Consumer Producer Problem

Topics: Mutual exclusion, Lock, Monitor Pages: 5 (462 words) Published: March 7, 2013
typedef int buffer_item;
#define BUFFER_SIZE 5
// consumer-producer.c
#include "buffer.h"

#define RAND_DIVISOR 100000000 //random function setup

pthread_mutex_t mutex; //setup mutex lock

sem_t full, empty; //setup binary semaphores

buffer_item buffer[BUFFER_SIZE]; //setup buffers

int counter; //setup buffer counter

pthread_t tid; //setup thread ID
pthread_attr_t attr; //setup thread attributes

void *producer(void *param); //producer thread
void *consumer(void *param); //consumer thread
void initializeData() { //Setup locks, semaphores, and counters

pthread_mutex_init(&mutex, NULL); //setup mutex lock
sem_init(&full, 0, 0); //setup full semaphore and init to 0 sem_init(&empty, 0, BUFFER_SIZE); //setup empty semaphore and init to BUFFER_SIZE pthread_attr_init(&attr); //Get default attributes

counter = 0;

//Producer Function
void *producer(void *param) {
buffer_item item;

while(TRUE) {

int rNum = rand() / RAND_DIVISOR; //Generate random number (Kochan) sleep(rNum); //sleep for a random interval of time

item = rand(); //random number generator
sem_wait(&empty);//take empty lock
pthread_mutex_lock(&mutex);//Take mutex lock

if(insert_item(item)) {
fprintf(stderr, " Producer report error\n");
else {
printf("Producer Thread produced: %d\n", item);

pthread_mutex_unlock(&mutex); //release mutex lock
sem_post(&full); //signal full semaphore

// Consumer Function
void *consumer(void *param) {
buffer_item item;

while(TRUE) {

int rNum = rand() / RAND_DIVISOR; //Call a random number and divide by the divisor to create a random sleep period (Kochan) sleep(rNum);

/* aquire the full lock */
/* aquire the mutex lock */
if(remove_item(&item)) {
fprintf(stderr, "Consumer report error condition\n"); }
else {
printf("consumer consumed %d\n", item);

pthread_mutex_unlock(&mutex); //release signal lock
sem_post(&empty); //signal clear lock

// Add item from buffer
int insert_item(buffer_item item) {

if(counter < BUFFER_SIZE) { //While buffer is not full, add process to queue and increment counter by one buffer[counter] = item;
return 0;
else { //Buffer full error
return -1; //return error code other than 0

// Remove item from buffer
int remove_item(buffer_item *item) {

if(counter > 0) { //When the buffer is not empty, remove item and decrement the counter *item = buffer[(counter-1)];
return 0;
else { //buffer empty error
return -1; //return error code other than 0

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

int i; //loop count

if(argc != 4) { //Make sure input is successful
fprintf(stderr, "USAGE:./main.out \n"); //Standard Output Usage error }

int mainSleepTime = atoi(argv[1]); //First argv - sleep time int numProd = atoi(argv[2]); //Second argv - # of producers int numCons = atoi(argv[3]); //Third argv - # of consumers

initializeData(); //Initialize all variables

for(i = 0; i < numProd; i++) { //Create Producer threads pthread_create(&tid,&attr,producer,NULL);

for(i = 0; i < numCons; i++) { //Create Consumer threads

sleep(mainSleepTime); //sleep for time specified from argv[1]

printf("Exit program\n"); //Exit program
exit(0); //return code 0

/* Works Cited
* Kochan, Stephen G. Programming in C: [a Complete Introduction to the C Programming Language]. Indianapolis, IN: Sams,...
Continue Reading

Please join StudyMode to read the full document

You May Also Find These Documents Helpful

  • consumer Essay
  • consumer Essay
  • Essay about Sustainable Consumption: The Responsibility of Consumers and Producers
  • Tax on Producers and Consumers Essay
  • Producer vs Consumer Essay
  • consumer behaviour Research Paper
  • Producers, Consumers, Predation/Parasitism Essay
  • consumer behaviour Essay

Become a StudyMode Member

Sign Up - It's Free