PRODUCER CONSUMER
#include <stdio.h>
#include <stdlib.h>
#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;
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}