CS414: HW #3 Solution (This is not the only solution; there can be many correct variations) 1. Gilbert and Shoe semaphore MUTEX = 1; semaphore BATHROOM = 1; Semaphore X, Y = 1; The Gilbert process: Repeat P(MUTEX) P(Y) P(X) V(MUTEX) V(Y) P(BATHROOM) V(X) V(BATHROOM) until false The Shoe process: Repeat P(MUTEX) P(X) P(Y) V(MUTEX) V(X) V(Y) P(BATHROOM) V(BATHROOM) until false To get magazine X, each process has to get the semaphore MUTEX first, so if the MUTEX semaphore can't be possesssed by two processes at the same time, we can guarantee the mutual exclusion of magazine X. In fact, since MUTEX is initialized to 1, only one process has the access to it at the same. Suppose these process will deadlock. Then both the Gilbert process and the Shoe process hold one semaphore and request the other one held by the other process. However, according to the pseudocode, such situation can never happen. 2. Barbershop const CHAIRS = 5; semaphore barber_free = 1; semaphore mutex = 1; int WC = 0; //number of waiting customer Barber process: while (true) P(mutex) if WC !=0 WC = WC - 1; V(mutex); snip_snip //cut hair V(barber_free); else V(mutex); Customer process: P(mutex) if (WC < CHAIRS) WC = WC + 1; V(mutex) P(barber_free) get_my_haircut //get haircut and leave else V(mutex)