这是临界区的两种算法。第一个不满足渐进要求,第二个是解决方案。我认为第二个有问题,但我的讲师不承认。在每个进程进入提醒部分后,我们需要以某种方式将轮次分配给新值吗?这样对吗?
boolean flag[2];
initially flag [0] = flag [1] = false.
flag [i] = true
//Pi ready to enter its critical section
//Process Pi
do {
flag[i] = true;
while (flag[j]) ;
critical section
flag [i] = false;
remainder section
} while ( … );
它满足互斥但不满足进步,现在通过将其更改为满足进步的需要:
int turn;
boolean flag[2];
initially flag [0] = flag [1] = false, turn = i (or j)
Process Pi
do {
flag [i] = true;
turn = j;
while (flag [j] and turn = j) ;
critical section
flag [i] = false;
remainder section
while(...);