Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- eating = waiting = 0 // keep track of the number of threads/customers
- mutex = Semaphore (1) // mutex protects both counters (eating and wating)
- block = Semaphore (0) // incoming customers' queue (regular meaning)
- must_wait = False // indicates that the bar is full
- # take control of the counters
- mutex.wait ( )
- # if there are 5 people already sitting at the table
- if must_wait:
- # increment the number of customers waiting outside
- waiting += 1
- # free the semaphore for another thread to use
- mutex.signal ( )
- # take control of the queue of customers
- block.wait ( )
- # else there's an empty seat at the table.
- else:
- # increment the number of customers eating at the bar
- eating += 1
- # figure out if customers will have to wait
- must_wait = (eating == 5)
- # free the semaphore so another thread can use
- mutex.signal ( )
- //eat sushi
- # acquire access to the eating and waiting counters
- mutex.wait ( )
- # eating is complete so decrement the counter for people eating at the bar
- eating -= 1
- # if no one is eating at the bar
- if eating == 0:
- # take 5 more customers or however many people are waiting if it is less
- n = min (5, waiting)
- # set counters
- waiting -= n
- eating += n
- # see if new customers will have to wait.
- must_wait = (eating == 5)
- # release lock on queue
- block.signal (n)
- # release lock on counters
- mutex.signal ( )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement