gcLevel = 1;
atomic {
remove one element from list;
last = whether the list is empty;
if (last)
state = LANDING;
}
if (last) {
wait for gcLevel = 0;
UnpinAtom;
} else {
if (state == LANDING) {
gcLevel = 0;
return;
}
MarkAtom;
gcLevel = 0;
}
struct foo {
// a and b are of size= 1 bit.
unsigned a:1,
b:1;
lock a_lock; // protects field a.
lock b_lock; // protects field b.
} f; // global variable f
// Thread T1 runs the code below:
lock(f.a_lock);
f.a = 1;
unlock(f.a_lock);
// Thread T2 runs the code below:
lock(f.b_lock);
f.b = 1;
unlock(f.b_lock);
philosopher(i)
{
while(1)
{
// thinks
semwait(i);
semwait((i+1)%N);
// eats
semsignal(i);
semsignal((i+1)/N);
}
}
How would you fix VeriSoft's algorithm so the above always-starving
philosopher is not a problem?