Professional Documents
Culture Documents
Complex Engineering Problem: Assignment 3 CE-303 Operating Systems
Complex Engineering Problem: Assignment 3 CE-303 Operating Systems
E (CE)
Assignment 3
CE-303 Operating Systems
A barbershop has three chairs, three barbers, and a waiting area that can accommodate four
customers on a sofa and that has standing room for additional customers. Fire codes limit the
total number of customers in the shop to 20. A customer will not enter the shop if it is filled
to capacity with other customers. Once inside, the customer takes a seat on the sofa or stands
if the sofa is filled. When a barber is free, the customer that has been on the sofa the longest
is served and, if there are any standing customers, the one that has been in the shop the
longest takes a seat on the sofa. When a customer's haircut is finished, any barber can accept
payment, but because there is only one cash register, payment is accepted for one customer at
a time. The barbers divide their time among cutting hair, accepting payment, and sleeping in
their chair waiting for a customer.
Write code that enforces the synchronization constraints for above problem.
1
code that enforces the synchronization constraints for above problem:
CODE: (solution)
Shared Variables :
customers = 0
mutex = Semaphore(1)
mutex2 = Semaphore(1)
sofa = Semaphore(4)
customer1 = Semaphore(0)
customer2 = Semaphore(0)
payment = Semaphore(0)
receipt = Semaphore(0)
queue1 = []
queue2 = []
Customer Thread:
self.sem1 = Semaphore(0)
self.sem2 = Semaphore(0)
mutex.wait()
if customers == 20:
mutex.signal()
balk()
customers += 1
queue1.append(self.sem1)
mutex.signal()
# enterShop()
customer1.signal()
self.sem1.wait()
sofa.wait()
# sitOnSofa()
self.sem1.signal()
mutex2.wait()
queue2.append(self.sem2)
mutex2.signal()
customer2.signal()
self.sem2.wait()
sofa.signal()
2
# getHairCut()
mutex.wait()
# pay()
payment.signal()
receipt.wait()
customers -= 1
mutex.signal()
Barber Thread
customer1.wait()
mutex.wait()
sem = queue1.pop(0)
sem.signal()
sem.wait()
mutex.signal()
customer2.wait()
mutex2.wait()
sem2 = queue2.pop(0)
sem2.signal()
mutex2.signal()
# cutHair()
payment.wait()
# acceptPayment()
receipt.signal()