[ Previous: 2.2.2 Cigarette Smokers | Up: 2.2 Classical Problems | Next: 2.3 POSIX Library ]

2.2.3 Unisex Bathroom

Scenario

Have u visited the SOC1 building recently? Not much has changed in the physical structure since SOC relocated to COM1, but a refresher to everyone, there is only one female washroom on the 8th floor. So for all you guys, who get very annoyed that you have to go all the way down to the 3rd floor to use the common room, well SOC, gratefully, has come up with a solution; UNISEX common room!!

Geek 1: Woah!!! Awesome, finally SoC is rocking it.
Geek 2: Yeah lor, we become SCHOOL OF COOLS.

Readers are we getting ideas?

Did it invoke thoughts of perhaps arranging a meeting with a special someone, when you are up all night coding?

Or did your creative side get the better of you, and you commenced planning on an intricate, artistic and clever plan to get someone to notice you?

Which would eventually lead to something like this?

Take our advice, pause all those thoughts!!

Geek 1: Wah wah, what and why??
Geek 2: Yeah, why? I was in such a nice place, a natural high. :(

Yeah well, I was equally disappointed.

We are not in the school of computing for no reason. The smart professors at SOC have devised a devious plan, such that people from the opposite sex can not use the washroom at same time.

Geek 1 & Geek 2: (Wailing)!!

So to put things in perspective we have the following situation:

As students of CS2106, you would realize that the problem could be sorted out, with the aid of semaphores. But potential problems that could arise are deadlock and starvation. Well we would deal with that in time. For now, lets get started by trying to prevent members of the opposite sex to mingle in the common room.

Solution

Problems

Problems that arise as a result of the implementation of this protocol are:

Use of Semaphores

The solution involves the use of semaphores as mentioned earlier.

Procedure #1 (Non-Solution)

The pseudo code for the above, female version (similar version for males, using male variables) is as follows:

femaleSwitch.lock(empty)
femaleMultiplex.wait()
# bathroom code here
femaleMultiplex.signal()
femaleSwitch.unlock(empty)

The problem with above solution is that the situation could go into a state of starvation. That is there could be a long line of females, and just one man, waiting. In which, case the male would not get a chance for a very long and unfair period of time, even though he might have been there before many females. This is because, in the above code we check the gender of the people in the bathroom, and allow a maximum number of individuals of the particular sex to go in until the bathroom is completely empty.

So, a solution to the above problem is implementing nextInLine. This would check who is in queue next and would bar the thread if it is of the opposite nature the thread currently being processed.

nextInLine – is a mutex, if the sex of the person (the thread) is different from the group/person inside it is 0, 1 otherwise.

Procedure #2 (Solution)

So in this manner starvation would be avoided. This procedure may not be efficient. This system could perhaps be implemented in a system with several hundred threads. If there is a frequent change in the type (gender) of thread, then it would be blocked and hence barring additional threads.

The pseudo code for the above procedure (for the male thread) is:

nextInLine.wait()
    maleSwitch.lock(empty)
nextInLine.signal()

        maleMultiplex.wait()
            # bathroom code here
        maleMultiplex.signal()

maleSwitch.unlock(empty)

Procedure #3 (Efficiency vs Fairness)

The following solution is what we think can be an improvement in efficiency over the previous one.

The above procedure minimizes the number of swapping, as compared to procedure 2. It ensures that the bathroom always runs at full capacity, for either of the sex as long as there are people in the queue. Although threads are not entered in sequence order, it is obvious that the efficiency offsets the slight unfairness.

The pseudo code is similar to procedure 2:

// nextInLine checks for sequence number, in addition. If a jump is noted, it would adjust a variable, say unfair = (n - currentlyInside). Hence, that many from the current queue would be allowed, before being barred. After unfair amount have entered, the threads from the other queue would be allowed to enter.

nextInLine.wait() // checks for sequence number, in addition
    maleSwitch.lock(empty)
nextInLine.signal()

        maleMultiplex.wait()
            # bathroom code here
        maleMultiplex.signal()

maleSwitch.unlock(empty)

References
1. Allen B. Downey (2005), The Little Book of Semaphores
2. David J. Powers (2006), The Unisex Bathroom: Fairness versus Performance

[ Previous: 2.2.2 Cigarette Smokers | Up: 2.2 Classical Problems | Next: 2.3 POSIX Library ]