Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1.  
  2. eating = waiting = 0 // keep track of the number of threads/customers
  3. mutex = Semaphore (1) // mutex protects both counters (eating and wating)
  4. block = Semaphore (0) // incoming customers' queue (regular meaning)
  5. must_wait = False // indicates that the bar is full
  6.  
  7. # take control of the counters
  8. mutex.wait ( )
  9.  
  10. # if there are 5 people already sitting at the table
  11. if must_wait:
  12. # increment the number of customers waiting outside
  13. waiting += 1
  14. # free the semaphore for another thread to use
  15. mutex.signal ( )
  16. # take control of the queue of customers
  17. block.wait ( )
  18.  
  19. # else there's an empty seat at the table.
  20. else:
  21. # increment the number of customers eating at the bar
  22. eating += 1
  23. # figure out if customers will have to wait
  24. must_wait = (eating == 5)
  25. # free the semaphore so another thread can use
  26. mutex.signal ( )
  27.  
  28. //eat sushi
  29.  
  30. # acquire access to the eating and waiting counters
  31. mutex.wait ( )
  32.  
  33. # eating is complete so decrement the counter for people eating at the bar
  34. eating -= 1
  35.  
  36. # if no one is eating at the bar
  37. if eating == 0:
  38. # take 5 more customers or however many people are waiting if it is less
  39. n = min (5, waiting)
  40.  
  41. # set counters
  42. waiting -= n
  43. eating += n
  44.  
  45. # see if new customers will have to wait.
  46. must_wait = (eating == 5)
  47.  
  48. # release lock on queue
  49. block.signal (n)
  50.  
  51. # release lock on counters
  52. mutex.signal ( )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement