The code you're working on has several issues, both in its design and specific coding mistakes. I'll outline these issues and provide guidance on how to fix them:
-
Initialization of condition variables and mutex: Your code doesn't initialize the mutex and condition variables. Before using them, they must be initialized either statically or dynamically.
To initialize them statically:
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t producer = PTHREAD_COND_INITIALIZER;
pthread_cond_t consumer = PTHREAD_COND_INITIALIZER;
Alternatively, for dynamic initialization, you should use pthread_mutex_init(), pthread_cond_init(), etc., before using these in your threads, typically at the beginning of your main() function.
-
Incorrect Usage of Threads: In your main function, you're directly calling producerFunc and consumerFunc like regular functions, not using them as threads. You need to create threads that will execute these functions.
Example of creating threads:
pthread_t producers[prod], consumers[con];
for(int i = 0; i < prod; ++i) {
pthread_create(&producers[i], NULL, producerFunc, NULL);
}
for(int i = 0; i < con; ++i) {
pthread_create(&consumers[i], NULL, consumerFunc, NULL);
}
And don't forget to join them at the end of your main() function:
for(int i = 0; i < prod; ++i) {
pthread_join(producers[i], NULL);
}
for(int i = 0; i < con; ++i) {
pthread_join(consumers[i], NULL);
}
-
Logical Error in Producer and Consumer Functions: The logic used to produce and consume items is flawed. Specifically, you're using count in your conditions (while(count == 10) and while(count == 0)), but you never actually modify count anywhere.
You likely meant to increment count when an item is produced and decrement it when an item is consumed.
- In
producerFunc: Add count++ after placing an item in the buffer.
- In
consumerFunc: Add count-- after consuming an item from the buffer.
-
Buffer Index Management is Incorrect: You're using sum as both the index to insert/consume an item and as a counter for total produced items, but you aren't managing it properly. This might work for very simple scenarios, but it’s misleading because sum isn't an accurate representation of the available items in the buffer.
You should use count to keep track of the number of items in the buffer. If sum is intended to be the next index for insertion, it should only be modified in producerFunc, and you should not use it for removing items in consumerFunc.
-
Include Headers Correctly: Your code includes standard headers, which is good. Verify that all functions you use are covered by these headers to ensure portability and compliance with your development environment.
-
Printing the Buffer: The logic for printing the buffer at the end of the main() function is statically printing 10 items regardless of how many items were actually produced or consumed. You might want to loop through the buffer based on count to print the actual contents.
-
Synchronization Logic: Your current implementation might not fully demonstrate the producer-consumer problem because you're only operating with one item at a time. Consider scenarios with multiple items being produced/consumed to better utilize the synchronization mechanisms.
Fixing your program involves addressing these points. Focus on proper initialization, correct usage of threads, fixing the logical errors in your synchronization logic, and accurately managing the index and counter for the items in the buffer.