I\'ve been searching information on Peterson\'s algorithm but have come across references stating it does not satisfy starvation but only deadlock. Is this true? and if so c
As Ben Jackson suspects, the problem is with a generalized algorithm. The standard 2-process Peterson's algorithm satisfies the no-starvation property.
Apparently, Peterson's original paper actually had an algorithm for N
processors. Here is a sketch that I just wrote up, in a C++-like language, that is supposedly this algorithm:
// Shared resources
int pos[N], step[N];
// Individual process code
void process(int i) {
int j;
for( j = 0; j < N-1; j++ ) {
pos[i] = j;
step[j] = i;
while( step[j] == i and some_pos_is_big(i, j) )
; // busy wait
}
// insert critical section here!
pos[i] = 0;
}
bool some_pos_is_big(int i, int j) {
int k;
for( k = 0; k < N-1; k++ )
if( k != i and pos[k] >= j )
return true;
}
return false;
}
Here's a deadlock scenario with N = 3
:
pos[0] = 0
and step[0] = 0
and then waits.pos[2] = 0
and step[0] = 2
and then waits.pos[1] = 0
and step[0] = 1
and then waits.step[0]
and so sets j = 1
, pos[2] = 1
, and step[1] = 2
.pos[2]
is big.j = 2
. It this escapes the for loop and enters the critical section. After completion, it sets pos[2] = 0
but immediately starts competing for the critical section again, thus setting step[0] = 2
and waiting.step[0]
and proceeds as process 2 before.References. All details obtained from the paper "Some myths about famous mutual exclusion algorithms" by Alagarsamy. Apparently Block and Woo proposed a modified algorithm in "A more efficient generalization of Peterson's mutual exclusion algorithm" that does satisfy no-starvation, which Alagarsamy later improved in "A mutual exclusion algorithm with optimally bounded bypasses" (by obtaining the optimal starvation bound N-1
).