#include #include #include #include #define BUFFER_SIZE 5 // Circular buffer int buffer[BUFFER_SIZE]; int in = 0; // Index to insert item int out = 0; // Index to remove item pthread_mutex_t mutex; pthread_cond_t buffer_not_empty, buffer_not_full; void *producer(void *arg) { int item = 0; while (1) { // Produce item printf("Produced item %d\n", item); buffer[in] = item; in = (in + 1) % BUFFER_SIZE; item++; sleep(1); // Simulate some processing time } pthread_exit(NULL); } void *consumer(void *arg) { while (1) { // Consume item int item = buffer[out]; printf("Consumed item %d\n", item); out = (out + 1) % BUFFER_SIZE; sleep(2); // Simulate some processing time } pthread_exit(NULL); } int main() { pthread_t producer_thread, consumer_thread; pthread_mutex_init(&mutex, NULL); pthread_cond_init(&buffer_not_empty, NULL); pthread_cond_init(&buffer_not_full, NULL); // Create producer thread pthread_create(&producer_thread, NULL, producer, NULL); // Create consumer thread pthread_create(&consumer_thread, NULL, consumer, NULL); // Join threads (never reached) pthread_join(producer_thread, NULL); pthread_join(consumer_thread, NULL); pthread_mutex_destroy(&mutex); pthread_cond_destroy(&buffer_not_empty); pthread_cond_destroy(&buffer_not_full); return 0; }