Cannot have expected output for producerconsumer (bounded-buffer) problem [message #1839635] |
Wed, 24 March 2021 22:06 |
zak ku Messages: 2 Registered: March 2021 |
Junior Member |
|
|
#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){
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;
}
}
}
}
int main(){
int prodThreads,consThreads,i;
printf("\nEnter the no of Producers: ");fflush(stdout);
scanf("%d",&prodThreads);
printf("\nEnter the no of Consumers: ");fflush(stdout);
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);
}
}
from www.eexploria.com/producer-consumer-problem-in-c/
I run it in another compiler it works fine.
Yet when I use the Eclipse IDE, the result always has the first item produced/consumed as 1000 and the rest being 0.
Console:
Enter the no of Producers: 3
Enter the no of Consumers: 4
The Producer (0) produced: [1000] Items
The Producer (1) produced: [0] Items
The Producer (2) produced: [0] Items
The Consumer (0) Consumed: [1000] Items
The Consumer (1) Consumed: [0] Items
The Consumer (2) Consumed: [0] Items
The Consumer (3) Consumed: [0] Items
Is there a way to modify it?
[Updated on: Wed, 24 March 2021 22:15] Report message to a moderator
|
|
|
Re: Cannot have expected output for producerconsumer (bounded-buffer) problem [message #1839642 is a reply to message #1839635] |
Thu, 25 March 2021 04:26 |
David Vavra Messages: 1426 Registered: October 2012 |
Senior Member |
|
|
This really has nothing to do with CDT and is off-topic for this forum.
Quote:I run it in another compiler it works fine.
Eclipse is just a fancy editor and doesn't include a compiler.
You have to use one that is installed on your system.
This is often GCC but it depends on the chosen toolchain.
So, what do you mean by "another compiler"?
Why don't you use it in your toolchain?
--
I tried it on my system and it worked
or rather I got different results
no of Producers: 4
no of Consumers: 3
The Producer (0) produced: [0] Items
The Producer (1) produced: [881] Items
The Producer (2) produced: [0] Items
The Producer (3) produced: [119] Items
The Consumer (0) Consumed: [33] Items
The Consumer (1) Consumed: [0] Items
The Consumer (2) Consumed: [967] Items
Some notes:
Even after setting the concurrency you could still execute only a single provider.
If you want to spread out the work, you probably should call pthread_yield() to give the others a chance to run.
// in producer loop
:
pthread_mutex_lock(&shared.mutex);
if (shared.producedCount < buffSize) {
shared.producedCount++;
*((int*) arg) += 1;
pthread_mutex_unlock(&shared.mutex);
pthread_yield(); // <<<<---------------------
} else {
:
Or call pthread_yield() every Nth time.
Do the same in the consumer threads.
[Updated on: Thu, 25 March 2021 05:15] Report message to a moderator
|
|
|
|
Powered by
FUDForum. Page generated in 0.03573 seconds