Name: Page 5 of 10 Consider the following slightly mis

Name:__________________________________
Page 5 of 10
Question 2: Synchronization (15 points)
Consider the following slightly mis-guided solution to Homework 2 with 4 philosophers. Note that it is
not exactly C – the monitor mutex is implicit, and the syntax is a bit different.
monitor {
bool fork_busy[4] = {F,F,F,F};
condition C_left, C_right;
get_forks(int phil#) {
int left = phil#
int right = (phil#+1) % 4
if fork_busy[left]
wait(C_left)
fork_busy[left] = True
if fork_busy[right]
wait(C_right)
fork_busy[right] = True
}
g1
g2
g3
g4
g5
g6
release_forks(int phil#) {
int left = phil#
int right = (phil#+1) % 4
fork_busy[left] = False
fork_busy[right] = False
signal(C_left)
signal(C_right)
}
r1
r2
r3
r4
}
The following events occur at the indicated times:
time 0: philosopher 0 calls get_forks()
time 1: philosopher 1 calls get_forks()
time 2: philosopher 2 calls get_forks()
time 3: philosopher 0 calls release_forks()
Give the sequence in which the numbered lines of pseudo-code are executed. Your response should
specify a series of events, where each event specifies:
•
•
•
•
time
thread (philosopher) number
line (g1-g6 or r1-r6)
calls to wait() should be listed twice – once when the thread enters wait(), and once when it
returns - e.g. “g2 (enter)” or “g2 (return)”