-
Notifications
You must be signed in to change notification settings - Fork 0
/
sleeping-barber.py
102 lines (84 loc) · 2.88 KB
/
sleeping-barber.py
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
import time
import random
import threading
class Barber():
def __init__(self):
self.barberWorking = threading.Event()
def sleep(self):
print("Barber is sleeping")
self.barberWorking.wait()
def wake(self):
self.barberWorking.set()
def cut(self, costumer):
self.barberWorking.clear()
print("Barber are cutting the hair from client {}".format(costumer.id))
time.sleep(random.randint(1, 5))
costumer.cut = True
self.cutting = False
print("Barber finished the hair cut")
class Barbershop():
def __init__(self, free_chairs, barber):
self.free_chairs = free_chairs
self.barber = barber
self.waiting_room = []
print("Barber shop is opening...")
self.mutex = threading.Lock()
working = threading.Thread(target=self.work)
working.start()
def work(self):
while True:
self.mutex.acquire()
if len(self.waiting_room) > 0:
client = self.waiting_room[0]
del self.waiting_room[0]
self.mutex.release()
self.barber.cut(client)
else:
self.mutex.release()
print("Aaah, all done, going to sleep,,,")
self.barber.sleep()
print("Barber woke up")
def receive_costumer(self, costumer):
print("Client {} arrive" .format(costumer.id))
self.mutex.acquire()
if len(self.waiting_room) == self.free_chairs:
print("Full wait room! Come back later")
self.mutex.release()
costumer.leave()
else:
self.waiting_room.append(costumer)
costumer.sit = True
print("Client {} is waiting in wait room" .format(costumer.id))
self.mutex.release()
self.barber.wake()
def have_chair(self):
for chair in self.waiting_room:
if chair is None:
return True
return False
class Costumer(threading.Thread):
def __init__(self, barbershop, id):
super().__init__()
self.id = id
self.cut = False
self.sit = False
self.barbershop = barbershop
def run(self):
while not self.cut:
if not self.sit:
self.barbershop.receive_costumer(self)
time.sleep(random.randint(1, 6))
def leave(self):
print("Costumer {} leaves the barbershop..." .format(self.id))
barber = Barber()
b = Barbershop(5, barber)
clients = []
l = random.randint(10, 30)
for i in range(l):
a = Costumer(b, i)
clients.append(a)
for i in clients:
time.sleep(random.randint(2, 5))
i.start()
for i in clients:
i.join()