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]);
}