-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path12a.c
More file actions
44 lines (39 loc) · 876 Bytes
/
12a.c
File metadata and controls
44 lines (39 loc) · 876 Bytes
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
// Producer-Consumer problem
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char const *argv[])
{
int buffer[10], bufsize = 10, in = 0, out = 0, produce, consume, ch = 0;
while (ch != 3)
{
printf("\n");
printf("\n1. Produce\n2. Consume\n3. Exit");
printf("\nEnter your choice:");
scanf("%d", &ch);
switch (ch)
{
case 1:
if ((in + 1) % bufsize == out)
printf("\nBuffer is full!\n");
else
{
printf("\nEnter the value:");
scanf("%d", &produce);
buffer[in] = produce;
in = (in + 1) % bufsize;
}
break;
case 2:
if (in == out)
printf("\nBuffer is empty!\n");
else
{
consume = buffer[out];
printf("\nThe consumed value is %d", consume);
out = (out + 1) % bufsize;
}
break;
}
}
return 0;
}