forked from avin-madhu/Operating-System-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducerConsumer.c
98 lines (91 loc) · 1.89 KB
/
producerConsumer.c
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Producer Consumer Problem
#include <stdio.h>
int mutex = 0; // for mutual exclusion
int full = 0;// false initially
int empty = 1; // true initally
int bufferSize;
int items=0; // to keep count of the number of items
void producer()
{
mutex = 1;
empty = 0;
items++;
printf("\nproducer produced item-%d", items);
if(items == bufferSize)
{
full = 1; // buffer is full
}
mutex = 0;
}
void consumer()
{
mutex = 1;
full = 0;
printf("\nconsumer consumed item-%d \n", items);
items--;
if(items == 0)
{
empty = 1; // buffer is full
}
mutex = 0;
}
int main()
{
printf("Enter the buffer size: ");
scanf("%d", &bufferSize);
int choice;
do
{
printf("\n\n SELECT OPERATION\n-----------------------------\n1) produce an item\n2) consume an item\n3) Check if full\n4) Check if empty\n5) Exit");
printf("\n-----------------------------\nEnter your choice: ");
scanf("%d", &choice);
if (choice == 1&& mutex == 0)
{
if(full == 0)
{
producer();
}
else
{
printf("\n The buffer is full!\n");
}
}
else if (choice == 2 && mutex == 0)
{
if(empty == 0)
{
consumer();
}
else
{
printf("\n The buffer is empty!\n");
}
}
else if (choice == 3)
{
if(full == 1)
{
printf("\n The buffer is full!\n");
}
else
{
printf("\n The buffer has more space..\n");
}
}
else if (choice == 4)
{
if(empty == 1)
{
printf("\n The buffer is empty!\n");
}
else
{
printf("\n The buffer has more items..\n");
}
}
else if(choice == 5)
{
printf("\n Exiting code...\n\n");
}
}while(choice != 5);
}