Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » C / C++ IDE (CDT) » Cannot have expected output for producerconsumer (bounded-buffer) problem
Cannot have expected output for producerconsumer (bounded-buffer) problem [message #1839635] Wed, 24 March 2021 22:06 Go to next message
zak ku is currently offline zak kuFriend
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 Go to previous messageGo to next message
David VavraFriend
Messages: 1403
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

Re: Cannot have expected output for producerconsumer (bounded-buffer) problem [message #1839646 is a reply to message #1839642] Thu, 25 March 2021 06:30 Go to previous message
zak ku is currently offline zak kuFriend
Messages: 2
Registered: March 2021
Junior Member
Thanks for your suggestions.

After enabling _USE_UNIX98 I can get the results finally.
Previous Topic:Run configuration
Next Topic:[SOLVED]How to import C project into Eclipse with makefile ?
Goto Forum:
  


Current Time: Mon Nov 29 03:37:03 GMT 2021

Powered by FUDForum. Page generated in 0.01733 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top