#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define BUFFER_SIZE 3
int buffer[BUFFER_SIZE];
int in = 0, out = 0;
bool isBufferEmpty() {
return in == out;
}
bool isBufferFull() {
return (in + 1) % BUFFER_SIZE == out;
}
void produce(int item) {
buffer[in] = item;
in = (in + 1) % BUFFER_SIZE;
}
int consume() {
int item = buffer[out];
out = (out + 1) % BUFFER_SIZE;
return item;
}
void producer() {
int item;
printf("Enter item to produce: ");
scanf("%d", &item);
if (isBufferFull()) {
printf("Buffer is full, cannot produce.\n");
return;
}
produce(item);
printf("Produced item %d.\n", item);
}
void consumer() {
if (isBufferEmpty()) {
printf("Buffer is empty, cannot consume.\n");
return;
}
int item = consume();
printf("Consumed item %d.\n", item);
}
int main() {
int choice;
while (true) {
printf("\n1. Produce\n2. Consume\n3. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1: producer(); break;
case 2: consumer(); break;
case 3: exit(0);
default: printf("Invalid choice.\n"); break;
}
}
return 0;
}