-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4. Producer Consumer Problem.c
67 lines (56 loc) · 1.43 KB
/
4. Producer Consumer Problem.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
// producer consumer problem
// requires 3 semaphore variables
// 1> S = mutex = mutual exclusion is maintained
// 2> E = empty locations = initial value is n
// 3> F = Filled locations = initial value is 0
// conditions met -
// 1> producer and consumer do not access the buffer at same time
// 2> consumer should not consume items from buffer when it is empty
// 3> producer should not produce/append items into buffer when it is full
#include <stdio.h>
#include <stdlib.h>
# define SIZE 5
int mutex = 1, empty = SIZE, full = 0, item = 0;
int wait(int w){
return (--w);
}
int signal(int s){
return (++s);
}
void producer(){
mutex=wait(mutex);
full=signal(full);
empty=wait(empty);
printf("\nProducer produces the item %d",++item);
mutex=signal(mutex);
}
void consumer(){
mutex=wait(mutex);
full=wait(full);
empty=signal(empty);
printf("\nConsumer consumes item %d",item--);
mutex=signal(mutex);
}
int main(){
printf("Press 1 for Producer\nPress 2 for Consumer\nPress 3 for Exit\n");
int n;
while(1){
printf("\nEnter your choice : ");
scanf("%d",&n);
switch(n){
case 1: if((mutex==1)&&(empty!=0))
producer();
else
printf("Buffer is full!!\n");
break;
case 2: if((mutex==1)&&(full!=0))
consumer();
else
printf("Buffer is empty!!\n");
break;
case 3: exit(0);
break;
}
}
return 0;
}