-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.java
50 lines (47 loc) · 1.42 KB
/
webserver.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
class webserver extends Thread
{ //Web server removes elements from the buffer
private int id;
private int num_elements;
private static Buffer buf;
semaphore mutex;
semaphore space_avail;
semaphore item_avail;
int n = 1;
/**
* Server thread which removes items from buffer
* @param i Id of server thread
* @param el Number of threads this server has to remove
* @param b Instance of buffer
* @param mutex Semaphore for mutual exclusion inside critical section
* @param space_avail Semaphore maintains number of empty spaces in buffer
* @param item_avail Semaphore maintains number of items in buffer
*/
public webserver(int i, int el, Buffer b, semaphore mutex, semaphore space_avail, semaphore item_avail)
{
id = i;
num_elements = el;
buf = b;
this.mutex = mutex;
this.space_avail = space_avail;
this.item_avail = item_avail;
}
public void run() {
while (num_elements > 0) {
try {
item_avail.P(); //Items in buffer++
mutex.P();
buf.remove(n); //Remove element from buffer
num_elements--;
mutex.V();
space_avail.V(); //emptySpace++
}
catch(InterruptedException interruptedException){
interruptedException.printStackTrace();
}
}
}
@Override
public long getId() {
return id;
}
}