Ancient CS 61 Content Warning!!!!!1!!!
This is not the current version of the class.
This site was automatically translated from a wiki. The translation may have introduced mistakes (and the content might have been wrong to begin with).

Bathroom Synchronization Solutions: Section 1

Stall privacy

Q1. Mutual exclusion!

Q2. The critical section is the poop_into call. Two users conflict when they have the same preferred_stall.

Q3.

mutex bathroom;
void user(int preferred_stall) {
    lock(&bathroom);
    poop_into(preferred_stall);
    unlock(&bathroom);
}

At most one user can use the bathroom at a time.

Q4.

mutex stall[K];
void user(int preferred_stall) {
    lock(&stall[preferred_stall]);
    poop_into(preferred_stall);
    unlock(&stall[preferred_stall]);
}