You are on page 1of 3

Department: Computer Engineering Program: B.

E (CE)

Assignment 3
CE-303 Operating Systems

Announced date:29/05/2021 Due Date: 5/06/2021 Total Marks = 5


Marks Obtained =
Teacher Name: Huma Hasan Rizvi, Khaliq Khan Zada, Najamul Islam Farooqui

Complex Engineering Problem


Course
Blooms
Sr. No Learning PLOs Complex Problem Solving
Taxonomy
Outcomes
WP1 (Depth of Knowledge
Analysis and PLO_2
C4 required) WP3 ( Depth of
CLO_2 Evaluation of (Problem
(Analyzing) Analysis)
Analysis required) WP7
Algorithms
( Interdependence)

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.

In other words, the following synchronization constraints apply:


 Customers invoke the following functions in order: enterShop, sitOnSofa,
sitInBarberChair, pay, exitShop.
 Barbers invoke cutHair and acceptPayment.
 Customers cannot invoke enterShop if the shop is at capacity.
 If the sofa is full, an arriving customer cannot invoke sitOnSofa until one of the
customers on the sofa invokes sitInBarberChair.
 If all three barber chairs are busy, an arriving customer cannot invoke
sitInBarberChair until one of the customers in a chair invokes pay.
 The customer has to pay before the barber can acceptPayment.
 The barber must acceptPayment before the customer can exitShop.

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()

You might also like