
OPERATING SYSTEMS COMPREHENSIVE EXAM, SPRING 2006
This exam contains four questions of equal value. For each question, record your answer
in a separate exam booklet. Answer all questions to the best of your ability.
1. Synchronization
Consider the following implementation of the semaphore wait and signal functions:
1. wait (s)
2. sem s;
3. {
4. if (value (s) == 0) { /* is value of s zero? */
5. putq (s, thisproc ()); /* put this proc on wait queue */
6. block (thisproc ()); /* this proc blocks and yields */
7. }
8. decr (s); /* decrement value of s */
9. }
10.
11. signal (s)
12. sem s;
13. {
14. incr (s); /* increment value of s */
15. if (! emptyq (s)) { /* if wait queue is not empty */
16. unblock (getq (s)); /* get a process and unblock it */
17. }
18. }
incr (s) increments value of semaphore s
decr (s) decrements value of semaphore s
value (s) returns value of semaphore s
putq (s, p) puts process p on semaphore s's queue of waiting processes
getq (s) removes a process from semaphores s's queue and returns it
emptyq (s) returns true if semaphore s's waiting process queue is empty
thisproc () returns the currently running process
block (p) puts process p in blocked state and yields to another process
unblock (p) puts process p in ready state
When either of these functions are called, you may assume that the process running them
will not be preempted. However, the process may voluntarily give up the CPU, as in the
wait function.
(i) There is a bug in the implementation; what is the bug? Identify using the line numbers
where are problem is, and describe how the problem may occur.
(ii) How would you fix the problem? Describe the minimal number of changes, using the
line numbers as references. (You will only get credit if you determine the absolute
minimal number of changes and what they are.)