
CS 471: Operating Systems
In-Class Exam 2
Name:________________________
April 20 2006
Instructions: This exam is open book, open notes. The work you turn in must be your own. To receive credit
you must sign the honor pledge at the end of the exam.
1. (10 pts) Consider the bakery algorithm for critical sections. n processes have the shared data:
boolean choosing[n];
int number[n];
which are initialized to false and 0 respectively. Each process has a unique number, i, each executes the
following code:
do {
choosing[ i ] = true;
number[ i ] = max( number[0], number[1], …, number[n – 1] ) + 1;
choosing[ i ] = false;
for ( j = 0; j < n; j++ ) {
while ( choosing[ j ] ) ;
while ( (number[ j ] != 0 ) &&
( ( number[ j ], j ) < ( number[ i ], i ) ) ;
}
critical section
number[ i ] = 0;
remainder section
} while ( 1 );
Why must the second while loop check both the number array and the values of i and j? Hint: Part of you
answer should explain why two processes can sometimes have the same value for their number[i].
2. (10 pts) Explain why the semaphore calls wait and signal must be provided as OS calls rather than as
user written functions.
1