在经典的Peterson算法中,在进入临界区之前检查2个标志flag1和flag2以及转弯变量。如果我先检查转弯然后检查标志,那么这个工作会不会发生?
答案 0 :(得分:0)
是的,如果您先检查turn
,然后在flag[0]
中的条件内检查flag[1]
或while()
,它就会有效。
原因是只有在两个条件都为真时才执行忙等待。
作为证明,我写了一个小型C程序,模拟两个进程,它们之间有随机切换。
对于关键部分,我在流程0中使用这段代码:
global ^= 0x5555;
global ^= 0x5555;
global++;
,这在过程1中:
global ^= 0xAAAA;
global ^= 0xAAAA;
global++;
两个进程各执行此部分1000次。如果两者的关键部分之间存在竞争条件,global
可能与模拟结束时的2000不同。
代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef enum
{
InstrNop,
InstrHalt,
InstrSetVarNum,
InstrJumpVarZero,
InstrJumpVarNonzero,
InstrJump,
InstrIncVar,
InstrDecVar,
InstrXorVarNum,
} tInstr;
int ExecuteInstruction(unsigned* Vars, const unsigned* Program, unsigned* Position)
{
switch (Program[*Position])
{
default:
case InstrHalt:
return 0;
case InstrNop:
(*Position)++;
break;
case InstrSetVarNum:
Vars[Program[*Position + 1]] = Program[*Position + 2];
(*Position) += 3;
break;
case InstrXorVarNum:
Vars[Program[*Position + 1]] ^= Program[*Position + 2];
(*Position) += 3;
break;
case InstrJumpVarZero:
if (Vars[Program[*Position + 1]] == 0)
(*Position) = Program[*Position + 2];
else
(*Position) += 3;
break;
case InstrJumpVarNonzero:
if (Vars[Program[*Position + 1]] != 0)
(*Position) = Program[*Position + 2];
else
(*Position) += 3;
break;
case InstrJump:
(*Position) = Program[*Position + 1];
break;
case InstrIncVar:
Vars[Program[*Position + 1]]++;
(*Position) += 2;
break;
case InstrDecVar:
Vars[Program[*Position + 1]]--;
(*Position) += 2;
break;
}
return 1;
}
typedef enum
{
VarGlobal,
VarCnt0,
VarCnt1,
VarFlag0,
VarFlag1,
VarTurn,
VarIdxMax
} tVarIdx;
unsigned Vars[VarIdxMax];
#define USE_PETERSON 01
#define SWAP_CHECKS 01
const unsigned Program0[] =
{
// cnt0 = 1000;
InstrSetVarNum, VarCnt0, 1000,
// 3:
#if USE_PETERSON
// flag[0] = 1;
InstrSetVarNum, VarFlag0, 1,
// turn = 1;
InstrSetVarNum, VarTurn, 1,
// 9:
// while (flag[1] == 1 && turn == 1) {}
#if SWAP_CHECKS
InstrJumpVarZero, VarTurn, 17,
InstrJumpVarZero, VarFlag1, 17,
#else
InstrJumpVarZero, VarFlag1, 17,
InstrJumpVarZero, VarTurn, 17,
#endif
InstrJump, 9,
// 17:
#endif
// Critical section starts
// global ^= 0x5555;
// global ^= 0x5555;
// global++;
InstrXorVarNum, VarGlobal, 0x5555,
InstrXorVarNum, VarGlobal, 0x5555,
InstrIncVar, VarGlobal,
// Critical section ends
#if USE_PETERSON
// flag[0] = 0;
InstrSetVarNum, VarFlag0, 0,
#endif
// cnt0--;
InstrDecVar, VarCnt0,
// if (cnt0 != 0) goto 3;
InstrJumpVarNonzero, VarCnt0, 3,
// end
InstrHalt
};
const unsigned Program1[] =
{
// cnt1 = 1000;
InstrSetVarNum, VarCnt1, 1000,
// 3:
#if USE_PETERSON
// flag[1] = 1;
InstrSetVarNum, VarFlag1, 1,
// turn = 0;
InstrSetVarNum, VarTurn, 0,
// 9:
// while (flag[0] == 1 && turn == 0) {}
#if SWAP_CHECKS
InstrJumpVarNonzero, VarTurn, 17,
InstrJumpVarZero, VarFlag0, 17,
#else
InstrJumpVarZero, VarFlag0, 17,
InstrJumpVarNonzero, VarTurn, 17,
#endif
InstrJump, 9,
// 17:
#endif
// Critical section starts
// global ^= 0xAAAA;
// global ^= 0xAAAA;
// global++;
InstrXorVarNum, VarGlobal, 0xAAAA,
InstrXorVarNum, VarGlobal, 0xAAAA,
InstrIncVar, VarGlobal,
// Critical section ends
#if USE_PETERSON
// flag[1] = 0;
InstrSetVarNum, VarFlag1, 0,
#endif
// cnt1--;
InstrDecVar, VarCnt1,
// if (cnt1 != 0) goto 3;
InstrJumpVarNonzero, VarCnt1, 3,
// end
InstrHalt
};
void Simulate(void)
{
unsigned pos0 = 0, pos1 = 0;
while (Program0[pos0] != InstrHalt ||
Program1[pos1] != InstrHalt)
{
int cnt;
cnt = rand() % 50;
while (cnt--)
if (!ExecuteInstruction(Vars, Program0, &pos0))
break;
cnt = rand() % 50;
while (cnt--)
if (!ExecuteInstruction(Vars, Program1, &pos1))
break;
}
}
int main(void)
{
srand(time(NULL));
Simulate();
printf("VarGlobal = %u\n", Vars[VarGlobal]);
return 0;
}
输出(ideone):
VarGlobal = 2000
现在,与Wikipedia中的检查顺序相同的程序,我将SWAP_CHECKS
定义为0:输出(ideone):
VarGlobal = 2000
最后,为了证明当Peterson的算法被禁用时存在竞争条件,我将USE_PETERSON
定义为0:输出(ideone):
VarGlobal = 1610