PRODUCER CONSUMER #include #include #define BUFFER_SIZE 5 #define NUM_ITEMS 10 int buffer[BUFFER_SIZE]; int count = 0; int in = 0; int out = 0; void produce() { if (count < BUFFER_SIZE) { int item = rand() % 100; buffer[in] = item; in = (in + 1) % BUFFER_SIZE; count++; printf("Produced: %d\n", item); } else { printf("Buffer is full! Cannot produce.\n"); } } void consume() { if (count > 0) { int item = buffer[out]; out = (out + 1) % BUFFER_SIZE; count--; printf("Consumed: %d\n", item); } else { printf("Buffer is empty! Cannot consume.\n"); } } int main() { int producedCount = 0; int consumedCount = 0; int choice; while (producedCount < NUM_ITEMS || consumedCount < NUM_ITEMS) { printf("\nSelect an operation:\n1. Produce\n2. Consume\nChoice: "); scanf("%d", &choice;); switch (choice) { case 1: if (producedCount < NUM_ITEMS) { produce(); producedCount++; } else { printf("All items produced! Cannot produce more.\n"); } break; case 2: if (consumedCount < NUM_ITEMS) { consume(); consumedCount++; } else { printf("All items consumed! Cannot consume more.\n"); } break; default: printf("Invalid choice! Please select 1 to produce or 2 to consume.\n"); } } printf("Production and consumption completed.\n"); return 0; }