cs341 Lecture Notes Spring 2002 Week 13, Thursday For today you should have... 1) started hw08 2) read the Unisex Bathroom problem 3) Read the Sections of Chapter 16 indicated in the reading notes 4) prepared for a quiz on the Fast File System paper. For next time you should 1) work on hw08 2) solve the Unisex Bathroom problem 3) work on the practice final exam and bring questions on Monday Today's topics 0) Quiz 8 1) homework 6 feedback (finally!) 2) remote file systems (notes22.txt) Homework 6 ---------- Semaphore implementation 1) make sure you protect the counter with a mutex! 2) only signal if the counter was negative 3) when you awaken, you need to check the condition variable (this one is a subtle point) void semaphore_wait (Semaphore *semaphore) { acquire (semaphore->lock); semaphore->value--; while (semaphore->value < 0) { cond_wait (semaphore->cond, semaphore->lock); } release (semaphore->lock); } void semaphore_signal (Semaphore *semaphore) { acquire (semaphore->lock); semaphore->value++; if (semaphore->value <= 0) cond_signal (semaphore->cond); release (semaphore->lock); } 4) The producer/consumer codes were good. But a few people forgot the cardinal rule: counter++ has to be protected from concurrent access. Also, it is almost never a good idea to check a condition and then act on it: if (num_cokes < CAPACITY) { mutex.wait () num_cokes++ mutex.signal () } Multiple producers could see an available space and try to fill it.