-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBankers.c
More file actions
55 lines (50 loc) · 1.01 KB
/
Bankers.c
File metadata and controls
55 lines (50 loc) · 1.01 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
#include <stdio.h>
#include <stdlib.h>
int buffer;
int quantity=0;
int mutex=1;
int signal(int n){
return ++n;
}
int wait(int n){
return --n;
}
void produce(){
if(quantity==buffer) {
printf("Buffer Full\n");
return;
}
if(mutex){
mutex = wait(mutex);
quantity++;
printf("Producer Produces Item %d!\n", quantity);
mutex = signal(mutex);
}
}
void consume(){
if(quantity==0) {
printf("Buffer Empty\n");
return;
}
if(mutex){
mutex = wait(mutex);
printf("Consumer Consumes Item %d!\n", quantity);
quantity--;
mutex = signal(mutex);
}
}
int main(){
int choice;
printf("Enter buffer size: ");
scanf("%d", &buffer);
printf("\n1.Produce\n2.Consume\n3.Quit\n");
do{
printf("Enter Choice: ");
scanf("%d", &choice);
switch (choice){
case 1: produce(); break;
case 2: consume(); break;
default: break;
}
} while (choice!=3);
}