In the No-name restaurant, the wine cellar lies at the end of a long narrow tunn
ID: 3870181 • Letter: I
Question
In the No-name restaurant, the wine cellar lies at the end of a long narrow tunnel, which, unfortunately, is not illuminated. After a collision between two waiters (who entered the tunnel from opposite ends), which resulted in three broken bottles of Gevrey Chambertin 1995 (priced at $300 a bottle), the manager installed red lights and the switches that control them at both entrances and specified the following protocol: before entering the tunnel a waiter is supposed to check if the red light is off. If so, the waiter switches it on and then enters the tunnel. Upon exiting the tunnel, the waiter switches the light off. (Either switch turns on- or off both lights simultaneously). The manager was very proud of this ingenious solution; however, on the second day of its operation, a collision occurred in the tunnel… Please explain how this could happen and suggest a working solution using the semaphore and mutex (you can install additional lights and switches).
Explanation / Answer
This solution works only for two waiters.i.e Binary semaphore. For example if a waiter entered into the tunnel by switching on the red light and some other waiter needs to enter on one end sees the red light and after some time if some other waiter needs to serve comes to other end sees the red light and wait for the light to turn off. And after first waiter exit the tunnel he swithches off the light and both the waiters at the end will see the light gets turned off so at the same time they will switch on the light and enter into the tunnel which leads to collision. In order to avoid this condition we can place some more lights let's say we can install some extra green lights and if the red light is on the waiter who waits should switch on the green lightso the waiter at the other end will know that someone is waiting at the other end and simiarly he should also switch on other green light to indicate him that he is also waiting. So after red light gets turned off ,one of the waiters should switch off the green light based on their priority and then switch on the red light and then gets inside the tunnel. This solution is based on counting semaphore i.e green lights show how many waiters are waiting in the end.