There are multiple issues with your code both conceptually and syntactically that hinder its proper functionality as a producer-consumer problem solution using monitors (in this case, POSIX threads, mutexes, and condition variables). Here's a breakdown of the issues and recommendations:
Initialization of Mutex and Condition Variables: You have declared a mutex and condition variables but you haven't initialized them. Before using a mutex or a condition variable, you need to initialize them either statically with PTHREAD_MUTEX_INITIALIZER / PTHREAD_COND_INITIALIZER or dynamically using pthread_mutex_init / pthread_cond_init.
count variable usage: It seems that you intended count to keep track of the number of items in the buffer, but in your code, it's only initialized to 0 and not used anywhere besides the wait conditions. You should increment count when a producer adds an item and decrement it when a consumer removes an item.
Directly calling the producer and consumer functions in main: You are calling producerFunc and consumerFunc directly from main, which does not utilize multiple threads. Instead, you should create multiple threads for producers and consumers using pthread_create.
Incorrect signalling: You should signal the respective condition variable after adding/removing an item from the buffer inside the locked section to ensure proper synchronization.
sum as the index: Using sum as the index and also incrementing/decrementing it looks incorrect for managing the buffer as a circular queue (if that's your intent). It seems to mix up the role of sum between counting produced items and indexing the buffer. Use count for counting and perhaps another pair of variables for the produce and consume indices if you're implementing a circular buffer.
Here is a revised version addressing these issues (assuming a linear buffer for simplicity and skipping error checks for brevity):
c
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // for sleep function : waits for seconds
#define PROD_COUNT 8
#define CONS_COUNT 4
int count = 0;
int buffer[10]; // Assuming a simple linear buffer for demonstration
pthread_cond_t producer = PTHREAD_COND_INITIALIZER;
pthread_cond_t consumer = PTHREAD_COND_INITIALIZER;
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
void *producerFunc(void *param) {
pthread_mutex_lock(&lock);
while (count == 10) {
pthread_cond_wait(&producer, &lock);
}
buffer[count] = 1; // Simply adding to buffer, considering it not as circular for simplicity
count++;
printf("Produced, count = %d\n", count);
pthread_mutex_unlock(&lock);
pthread_cond_signal(&consumer);
return NULL;
}
void *consumerFunc(void *param) {
pthread_mutex_lock(&lock);
while (count == 0) {
pthread_cond_wait(&consumer, &lock);
}
count--;
buffer[count] = 0;
printf("Consumed, count = %d\n", count);
pthread_mutex_unlock(&lock);
pthread_cond_signal(&producer);
return NULL;
}
int main() {
pthread_t producers[PROD_COUNT], consumers[CONS_COUNT];
for(int i = 0; i < PROD_COUNT; i++) {
pthread_create(&producers[i], NULL, producerFunc, NULL);
}
for(int i = 0; i < CONS_COUNT; i++) {
pthread_create(&consumers[i], NULL, consumerFunc, NULL);
}
for(int i = 0; i < PROD_COUNT; i++) {
pthread_join(producers[i], NULL);
}
for(int i = 0; i < CONS_COUNT; i++) {
pthread_join(consumers[i], NULL);
}
for (int i = 0; i < 10; i++) printf("%d", buffer[i]);
printf("\n");
return 0;
}
This is a basic correction focusing on properly initializing and using synchronization primitives to correct execution flow. Remember, the exact synchronization pattern depends on the specific requirements of your producer-consumer problem, such as whether the buffer is a circular queue or a simplistic linear buffer, and may require additional variables or condition checks.