-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprodcons.java
404 lines (367 loc) · 16.9 KB
/
prodcons.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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package assign4;
/**
*
* @author Ajinkya
*/
///
import java.util.Scanner;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock;
class prodcons{
//***********************MAIN***************************/
public static int MAX=5; // buffersize
public static final int cnum=9; //total consumers for servicing
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_BLACK = "\u001B[30m";
public static final String ANSI_RED = "\u001B[31m";
public static final String ANSI_GREEN = "\u001B[32m";
public static final String ANSI_YELLOW = "\u001B[33m";
public static final String ANSI_BLUE = "\u001B[34m";
public static final String ANSI_PURPLE = "\u001B[35m";
public static final String ANSI_CYAN = "\u001B[36m";
public static final String ANSI_WHITE = "\u001B[37m";
public static int service_time[]=new int[cnum]; //initializing array for service time
public static void printQ(int[] Q,int front,int rear){
int f=front;int r=rear;
while(f!=r){
System.out.print(Q[f]+" ");
f+=1;
f=f%MAX;
}
}
public static void main(String args[]) throws InterruptedException
{
Scanner sc=new Scanner(System.in);
int choice = 0;int z=0;
while(choice!=1 && choice!=2) // Total service personnel required
{
if(z==0)
{
System.out.println("Enter the number of Service personnel required(1/2): ");
choice=sc.nextInt();
z=1;
}
else
{
System.out.println(ANSI_RED+"Please enter correct value(either 1 or 2)!!"+ANSI_RESET);
System.out.println("Enter the number of Service personnel required(1/2): ");
choice=sc.nextInt();
}
}
sc.close();
for(int i=0;i<cnum;i++) // Generating random servicing time required by each customer
service_time[i]=getRandomNumberInRange(1, 5);
int max=MAX-1;
System.out.println(ANSI_BLUE+"Waiting Queue length: "+ANSI_RESET+max);
System.out.println(ANSI_RED+"Total customers: "+cnum+ANSI_RESET);
//System.out.println(ANSI_BLUE+"Customer No.\t"+"Service time\t"+"Service Personnel\t"+"Curr queue\t"+"Remarks\t"+ANSI_RESET);
System.out.println(ANSI_YELLOW+"-----------------Waiting Queue is Empty, Now the Counter is CLOSED---------------"+ANSI_RESET);
switch(choice)
{
case 1: System.out.println("SERVICING WITH SINGLE PERSONNEL");
System.out.println(ANSI_BLUE+"Customer No.\t"+"Service time\t"+"Service Personnel\t"+"Curr queue\t"+"Remarks"+ANSI_RESET);
final prodcons pc1 = new prodcons();
Thread p1 = new Thread(new Runnable() // Creating producer thread for 1 service personnel case
{
@Override
public void run()
{
try
{
pc1.producer1();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
Thread c1 = new Thread(new Runnable() // Creating consumer thread for 1 service personnel case
{
@Override
public void run()
{
try
{
pc1.consumer1();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
//System.out.println(ANSI_GREEN+"Customer 1 arrives and wakes up the Service Personnel"+ANSI_RESET);
System.out.println(ANSI_GREEN+"\t\t"+"\t\t"+"\t\t\t"+"--"+"\t\t"+"Customer 1 arrives and wakes up the Service Personnel"+ANSI_RESET);
p1.start();
c1.start();
p1.join();
c1.join();
break;
case 2: System.out.println("SERVICING WITH 2 SERVICE PERSONNEL");
System.out.println(ANSI_BLUE+"Customer No.\t"+"Service time\t"+"Service Personnel\t"+"Curr queue\t"+"Remarks"+ANSI_RESET);
final prodcons pc2 = new prodcons();
Thread p2 = new Thread(new Runnable() // Creating producer thread for 2 service personnel case
{
@Override
public void run()
{
try
{
pc2.producer2();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
Thread c21 = new Thread(new Runnable() // Creating consumer thread for 2 service personnel case
{
@Override
public void run()
{
try
{
pc2.consumer21();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
Thread c22 = new Thread(new Runnable() // Creating consumer thread for 2 service personnel case
{
@Override
public void run()
{
try
{
pc2.consumer22();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
}
});
//System.out.println(ANSI_GREEN+"Customer 1 arrives and wakes up the Service Personnel"+ANSI_RESET);
System.out.println(ANSI_GREEN+"\t\t"+"\t\t"+"\t\t\t"+"--"+"\t\t"+"Customer 1 arrives and wakes up the Service Personnel"+ANSI_RESET);
p2.start();
c21.start();
c22.start();
p2.join();
c21.join();
c22.join();
break;
default: System.out.println(ANSI_RED+"Please restart the program as wrong input is given."+ANSI_RESET);
}// Switch ends
System.out.println(ANSI_YELLOW+"--------------------------------COUNTER IS CLOSED--------------------------------"+ANSI_RESET);
}
// Function for Random number generation
private static int getRandomNumberInRange(int min, int max) {
if (min >= max) {
throw new IllegalArgumentException("max must be greater than min");
}
return (int)(Math.random() * ((max - min) + 1)) + min;
}
/////////////////////////////////////////////////////////
public static final int FULL = 0;
public static final int EMPTY=MAX;
public static int last=0,flag=0,slept=0,front=0,rear=0;
static Semaphore semFull = new Semaphore(FULL); // Semaphore for checking how many slots are FULL
static Semaphore semEmp = new Semaphore(EMPTY); // Semaphore for checking how many slots are EMPTY
ReentrantLock lock = new ReentrantLock(); // MUTEX LOCK
int queue[]=new int[MAX];
//**************Consumer Method for 1 service personnel case***********************//
public void consumer1() throws InterruptedException
{
int item=0;
do
{
semFull.acquire();
/************************MUTEX region************************/
synchronized (this)
{
if(front==rear)
{
//System.out.println(ANSI_RED+"Waiting queue is empty, Service personnel sleeps"+ANSI_RESET);
System.out.println(ANSI_RED+"\t\t"+"\t\t"+"\t\t\t"+"--"+"\t\t"+"Waiting queue is empty, Service personnel sleeps"+ANSI_RESET);
slept=1;
}
else
{
item=queue[front];
//System.out.println(ANSI_GREEN+"Customer "+item+" is being serviced having service time "+service_time[item-1]+"sec"+ANSI_RESET);
System.out.print(ANSI_GREEN+item+"\t\t"+service_time[item-1]+" sec\t\t"+"\t1\t\t");
printQ(queue,front,rear);
System.out.println("\t\tBeing serviced"+ANSI_RESET);
front=(front+1)%MAX;
if(last==1 && item==cnum-1)
flag=1;
Thread.sleep(service_time[item-1]*1000);
}
}
/********************MUTEX region ENDS here********************/
semEmp.release();
}while(item!=cnum && flag!=1);
}
//*******1st Consumer Method for 2 Service personnel case*******/
public void consumer21() throws InterruptedException
{
int item=0;
do
{
semFull.acquire();
/*************************MUTEX region*************************/
lock.lock();
if(front==rear)
{
//System.out.println(ANSI_RED+"Waiting Queue is empty and Service Personnel sleeps"+ANSI_RESET);
System.out.println(ANSI_RED+"\t\t"+"\t\t"+"\t\t\t"+"--"+"\t\t"+"Waiting queue is empty, Service personnel sleeps"+ANSI_RESET);
slept=1;
lock.unlock();
/********************MUTEX region ENDS here********************/
}
else
{
item=queue[front];
//System.out.println(ANSI_GREEN+"Customer "+item+" is being serviced by service personnel 1 for time "+service_time[item-1]+"sec"+ANSI_RESET);
System.out.print(ANSI_GREEN+item+"\t\t"+service_time[item-1]+" sec\t\t"+"\t1\t\t");
printQ(queue,front,rear);
System.out.println("\t\tBeing serviced"+ANSI_RESET);
front=(front+1)%MAX;
lock.unlock();
/****************ORRR MUTEX region ENDS here********************/
if(item==cnum)
flag=1;
Thread.sleep(service_time[item-1]*1000);///////service_time[item]
}
semEmp.release();
}while(item!=cnum && flag!=1);
flag=1;
}
//**************2nd Consumer Method for 2 Service personnel case***********************//
public void consumer22() throws InterruptedException
{
int item=0;
do
{
semFull.acquire();
/*************************Mutex region*************************/
lock.lock();
if(front==rear)
{
//System.out.println(ANSI_RED+"Waiting queue is empty, Service personnel sleeps"+ANSI_RESET);
System.out.println(ANSI_RED+"\t\t"+"\t\t"+"\t\t\t"+"--"+"\t\t"+"Waiting queue is empty, Service personnel sleeps"+ANSI_RESET);
lock.unlock();
slept=1;
/********************MUTEX region ENDS here********************/
}
else
{
item=queue[front];
//System.out.println(ANSI_CYAN+"Customer "+item+" is being serviced by service personnel 2 for time "+service_time[item-1]+"sec"+ANSI_RESET);
System.out.print(ANSI_CYAN+item+"\t\t"+service_time[item-1]+" sec\t\t"+"\t2\t\t");
printQ(queue,front,rear);
System.out.println("\t\tBeing serviced"+ANSI_RESET);
front=(front+1)%MAX;
lock.unlock();
/******************ORRR MUTEX region ENDS here******************/
if(item==cnum)
flag=1;
Thread.sleep(service_time[item-1]*1000);
}
semEmp.release();
}while(item!=cnum && flag!=1);
flag=1;
}
//********************Producer Class for 1 Service personnel case***********************//
public void producer1() throws InterruptedException
{
for(int i=1;i<=cnum;i++)
{
semEmp.acquire();
/***************Mutex Region***********/
synchronized (this)
{
// consumer thread waits while list
// is empty
if ((front==0 && rear==MAX-1)||(rear==(front-1)%MAX))
{
//System.out.println(ANSI_RED+"Queue is full, Customer "+i+" leaves"+ANSI_RESET);
System.out.print(ANSI_RED+"\t\t"+"\t\t"+"\t\t\t");
printQ(queue,front,rear);
System.out.println("\t\tQueue is full, Customer "+i+" leaves"+ANSI_RESET);
}
else
{
queue[rear]=i;
rear=(rear+1)%MAX;
if(slept==1)
{
//System.out.println(ANSI_PURPLE+"Customer "+ i +" arrives and wakes up service personnel"+ANSI_RESET);
System.out.println(ANSI_PURPLE+"\t\t"+"\t\t"+"\t\t\t"+"--"+"\t\t"+"Customer "+ i +" arrives and wakes up service personnel"+ANSI_RESET);
slept=0;
}
else
{
//System.out.println(ANSI_BLUE+"Customer "+i +" is added to the queue"+ANSI_RESET);
System.out.print(ANSI_BLUE+"\t\t"+"\t\t"+"\t\t\t");
printQ(queue,front,rear);
System.out.println("\t\tCustomer "+ i +" is added to the queue"+ANSI_RESET);
}
}
}//mutex ends
semFull.release();
}
semFull.release();
}
//********************Producer Class for 2 Service personnel case***********************//
public void producer2() throws InterruptedException
{
for(int i=1;i<=cnum;i++)
{
semEmp.acquire();
/*************************MUTEX region*************************/
lock.lock();
// consumer thread waits while queue
// is empty
if ((front==0 && rear==MAX-1)||(rear==(front-1)%MAX))
{
//System.out.println(ANSI_RED+"Queue is full, Customer "+i+" leaves"+ANSI_RESET);
System.out.print(ANSI_RED+"\t\t"+"\t\t"+"\t\t\t");
printQ(queue,front,rear);
System.out.println("\t\tQueue is full, Customer "+i+" leaves"+ANSI_RESET);
}
else
{
queue[rear]=i;
rear=(rear+1)%MAX;
if(slept==1)
{
//System.out.println(ANSI_PURPLE+"Customer "+ i +" arrives and wakes up service personnel"+ANSI_RESET);
System.out.println(ANSI_PURPLE+"\t\t"+"\t\t"+"\t\t\t"+"--"+"\t\t"+"Customer "+ i +" arrives and wakes up service personnel"+ANSI_RESET);
slept=0;
}
else
{
//System.out.println(ANSI_BLUE+"Customer "+i +" is added to the queue"+ANSI_RESET);
System.out.print(ANSI_BLUE+"\t\t"+"\t\t"+"\t\t\t");
printQ(queue,front,rear);
System.out.println("\t\tCustomer "+ i +" is added to the queue"+ANSI_RESET);
}
}
lock.unlock();
/********************MUTEX region ENDS here********************/
semFull.release();
}
semFull.release();
}
}