-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBounded_Buffer_Problem.java
61 lines (60 loc) · 1.57 KB
/
Bounded_Buffer_Problem.java
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
package oslab;
import java.util.Scanner;
public class Oslab {
static int s=1,e=10,f=0, j=1,k=1;
static int wait(int s)
{
return --s;
}
static int signal(int s)
{
return ++s;
}
static void producer()
{
s=wait(s);
e=wait(e);
f=signal(f);
System.out.println("Producer produces"+j+"Item");
j++;
s=signal(s);
}
static void consumer()
{
s=wait(s);
e=signal(e);
f=wait(f);
System.out.println("Consumer Consumes" + k +"th item");
s=signal(s);
}
public static void main(String[] args) {
while(true)
{
System.out.println("1. Produces 2.Consumer 3. Exit");
Scanner inp=new Scanner(System.in);
int choice=inp.nextInt();
switch(choice)
{
case 1: if(s == 1 && e != 0)
{
producer();
}
else
{
System.out.println("Producer is waiting");
}
break;
case 2: if(s==1 && f!=0)
{
consumer();
}
else
{
System.out.println("Consumer is waiting");
}
break;
case 3: System.exit(0);
}
}
}
}