Producer Consumer Problem In C

1
1801

The producer-consumer problem outlines the requirement for synchronization in systems where there are many processes that share a single resource. Let us consider two processes that share a fixed-sized buffer. Here one process produces and keeps the information in a buffer whereas the other process consumes it. Both the processes work concurrently. The data which is left unconsumed is kept in the buffer itself.

But there may be situations where the producer produce and tries to put an item into a full buffer. Similarly consumer may try to consume from an empty buffer.Thus to achieve synchronization, the producer must be blocked when the buffer is full and the consumer must be blocked when the buffer is empty. It is also called as bounded buffer problem.

program-for-producer-consumerWrite a program to implement producer consumer problem.

ALGORITHM

Step 1: Start

Step 2: Define the maximum buffer size.

Step 3:Enter the number of producers and consumers.

Step 4: The producer produces the job and put it in the buffer.

Step 5:The consumer takes the job from the buffer.

Step 6:If the buffer is full the producer goes to sleep.

Step 7:If the buffer is empty then consumer goes to sleep.

Step 8:Stop

 

PROGRAM DEVELOPMENT

#include<stdio.h>

#include<unistd.h>

#include<pthread.h>

#define buffSize 1000

struct

{

pthread_mutex_t mutex;

int buff[buffSize];

int producedCount;

int consumedCount;

}shared={PTHREAD_MUTEX_INITIALIZER};

void *produce(void *arg);

void *consume(void *arg);

void main()

{

int prodThreads,consThreads,i;

printf(“\nEnter the no of Producers: “);

scanf(“%d”,&prodThreads);

printf(“\nEnter the no of Consumers: “);

scanf(“%d”,&consThreads);

int producerArr[prodThreads],consumerArr[consThreads];

pthread_t producer[prodThreads],consumer[consThreads];

pthread_setconcurrency(prodThreads+consThreads);

for(i=0;i<prodThreads;i++)

{

producerArr[i]=0;

pthread_create(&producer[i],NULL,produce,&producerArr[i]);

}

for(i=0;i<consThreads;i++)

{

consumerArr[i]=0;

pthread_create(&consumer[i],NULL,consume,&consumerArr[i]);

}

for(i=0;i<prodThreads;i++)

{

pthread_join(producer[i],NULL);

printf(“\nThe Producer (%d) produced: [%d] Items”,i,producerArr[i]);

sleep(1);

}

printf(“\n”);

for(i=0;i<consThreads;i++)

{

pthread_join(consumer[i],NULL);

printf(“\nThe Consumer (%d) Consumed: [%d] Items”,i,consumerArr[i]);

sleep(1);

}

}

void *produce(void *arg)

{

while(1)

{

pthread_mutex_lock(&shared.mutex);

if(shared.producedCount<buffSize)

{

shared.producedCount++;

*((int *)arg) += 1;

pthread_mutex_unlock(&shared.mutex);

}

else

{

pthread_mutex_unlock(&shared.mutex);

return NULL;

}

}

}

void *consume(void *arg)

{

while(1)

{

pthread_mutex_lock(&shared.mutex);

if(shared.consumedCount<shared.producedCount)

{

shared.consumedCount++;

*((int *)arg) += 1;

pthread_mutex_unlock(&shared.mutex);

}

else

{

pthread_mutex_unlock(&shared.mutex);

if (shared.consumedCount>=buffSize)

return NULL;

}

}

}

OUTPUT

Test Case

gcc -lpthread producerconsumer.c

./a.out

Enter the no of Producers: 4

Enter the no of Consumers: 3

Result

The Producer (0) produced: [252] Items

The Producer (1) produced: [415] Items

The Producer (2) produced: [136] Items

The Producer (3) produced: [197] Items

 

The Consumer (0) Consumed: [703] Items

The Consumer (1) Consumed: [260] Items

The Consumer (2) Consumed: [37] Items

 

CONCLUSION

The program was executed successfully.

1 COMMENT

LEAVE A REPLY