-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5_Producer_Consumer.c
More file actions
63 lines (57 loc) · 1.44 KB
/
5_Producer_Consumer.c
File metadata and controls
63 lines (57 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <stdio.h>
#include <stdlib.h>
#define size 5
int mutex = 1 , full = 0 ,empty = size , buffer[size];
void producer() {
int item;
if (mutex == 1 && empty != 0) {
--mutex;
++full;
--empty;
printf("\nEnter the item to produce: "); scanf("%d", &item);
buffer[full - 1] = item;
++mutex;
} else {
printf("\nBuffer is full!");
}
}
void consumer() {
if (mutex == 1 && full != 0) {
--mutex;
++empty;
printf("\nConsumer consumes item %d", buffer[full - 1]);
--full;
++mutex;
} else {
printf("\nBuffer is empty!");
}
}
void displayBuffer() {
printf("\nItems in Buffer:\n");
for (int i = 0; i < full; i++) {
printf("%d ", buffer[i]);
}
printf("\n");
}
int main() {
int n, item;
printf("\n1. Producer\n2. Consumer\n3. Display Buffer\n4. Exit");
while (1) {
printf("\nEnter your choice:");
scanf("%d", &n);
switch (n) {
case 1: if (full < size) producer(item);
else printf("\nBuffer is full!");
break;
case 2: consumer();
break;
case 3: displayBuffer();
break;
case 4: printf("Exiting The Program .");
exit(0);
break;
default:printf("Invalid choice!");
}
}
return 0;
}