Advertisement
ZergRushA

23.11.2022-potoki

Nov 23rd, 2022
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.37 KB | None | 0 0
  1. import threading
  2. import time
  3. import random
  4.  
  5.  
  6. barber_wakeup = 1
  7. customer_semaphore = threading.Semaphore(0)
  8. barber_semaphore = threading.Semaphore(0)
  9. mutex = threading.Semaphore(1)
  10.  
  11. class Barber:
  12.     def cut_hair(self, customer):
  13.         for _ in range(3):
  14.             print("\nBarber is cutting hair of {0}".format(customer))
  15.             time.sleep(2)
  16.         print("\n{0} is done and leaving barber shop".format(customer))
  17.  
  18.  
  19. class Customer:
  20.     def __init__(self, name):
  21.         self.name = name
  22.    
  23.     def get_hair_cut(self, customer):
  24.         for _ in range(3):
  25.             print("\nCustomer {0} is having haircut".format(customer))
  26.             time.sleep(2)
  27.    
  28.     def leave(self, customer):
  29.         print("\nWaiting room if full, customer {0} is leaving".format(customer))
  30.  
  31.  
  32. class BarberShop:
  33.     waiting_cust = []
  34.    
  35.     def __init__(self, barber, total_chairs):
  36.         self.barber = barber
  37.         self.total_chairs = total_chairs
  38.         print("Total seats: {0}".format(total_chairs))
  39.    
  40.     def barber_thread(self):
  41.         bt = threading.Thread(target=self.barber_working)
  42.         bt.start()
  43.    
  44.     def entry(self, customer):
  45.         print("\nCustomer {0} has entered".format(customer))
  46.         mutex.acquire()
  47.    
  48.         if len(self.waiting_cust) < self.total_chairs:
  49.             print("\nCustomer {0} founds an empty seat".format(customer))
  50.             self.waiting_cust.append(customer)
  51.            
  52.             global barber_wakeup
  53.            
  54.             while barber_wakeup:
  55.                 customer_semaphore.release()
  56.                 print("\nCustomer {0} wakes up the barber".format(customer))
  57.                 barber_wakeup = 0
  58.            
  59.             print("\nCustomer {0} sits on a waiting chair".format(customer))
  60.             mutex.release()
  61.            
  62.             print("\nCustomer {0} is waiting to be called by barber".format(customer))
  63.             barber_semaphore.acquire()
  64.             Customer.get_hair_cut(self, customer)
  65.        
  66.         else:
  67.             mutex.release()
  68.             Customer.leave(customer)
  69.    
  70.    
  71.     def barber_working(self):
  72.         while True:
  73.             if len(self.waiting_cust) == 0:
  74.                 global barber_wakeup
  75.                 print("Barber is sleeping")
  76.                 barber_wakeup = 1
  77.                 customer_semaphore.acquire()
  78.            
  79.             if len(self.waiting_cust) > 0:
  80.                 mutex.acquire()
  81.                
  82.                 cust = self.waiting_cust[0]
  83.                 print("\nBarber calls {0}".format(cust))
  84.                
  85.                 del self.waiting_cust[0]
  86.                 barber_semaphore.release()
  87.                 mutex.release()
  88.                
  89.                 self.barber.cut_hair(cust)
  90.    
  91.  
  92.  
  93. customer_list = []
  94. barber = Barber()
  95. barber_shop = BarberShop(barber, 3)
  96. barber_shop.barber_thread()
  97.  
  98. customer_list.append(Customer("Кирилл"))
  99. customer_list.append(Customer("Артем"))
  100. customer_list.append(Customer("Артур"))
  101. customer_list.append(Customer("Данила"))
  102. customer_list.append(Customer("Владислав"))
  103. customer_list.append(Customer("VanDarkholm"))
  104. customer_list.append(Customer("Vasya"))
  105.  
  106.  
  107. while len(customer_list) != 0:
  108.     c = customer_list.pop()
  109.    
  110.     t = threading.Thread(target = barber_shop.entry, args=(c.name,))
  111.     time.sleep(random.randint(1, 5))
  112.    
  113.     t.start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement