好吧,我有这个(它是代码的一部分):
20 proctype Main(byte myID) {
21 do
22 ::int J=0;
23 int K=0;
24
25 atomic{
26 requestCS[myID]=true;
27 myNum[myID]=highestNum[myID]+1;
28 }
29
30 do
31 :: J <= NPROCS-1 ->
32 if
33 :: J != myID -> ch[J] ! request, myID, myNum[myID];
34 fi;
35 J++;
36 :: else break;
37 od;//////////////////////////////////
38
39
40 do
41 :: K <= NPROCS-2 ->
42 ch[myID] ?? reply, _, _;
43 K++;
44 :: else break;
45 od;
46
47
48 cs: critical++;
49 assert (critical==1);
50 critical--;
51 requestCS[myID]=false;
52
53 byte N;
54 do
55 :: empty(deferred[myID]) -> break;
56 deferred [myID] ? N -> ch[N] ! reply, 0, 0;
57 od;
58 od;
59 }
在/////////////它卡住(写入超时),并且没有前进的方法,例如步骤40。
它是Ricart-Agrawala算法的一部分,在这里等于:
1 #define NPROCS 2
2 int critical = 0;
3 byte myNum[NPROCS];
4 byte highestNum[NPROCS];
5 bool requestCS[NPROCS];
6 chan deferred[NPROCS] = [NPROCS] of {byte};
7 mtype={request, reply};
8 chan ch[NPROCS]=[NPROCS] of {mtype, byte, byte};
9
10 init {
11 atomic {
12 int i;
13 for (i : 0 .. NPROCS-1){
14 run Main(i);
15 run Receive(i);
16 }
17 }
18 }
19
20 proctype Main(byte myID) {
21 do
22 ::int J=0;
23 int K=0;
24
25 atomic{
26 requestCS[myID]=true;
27 myNum[myID]=highestNum[myID]+1;
28 }
29
30 do
31 :: J <= NPROCS-1 ->
32 if
33 :: J != myID -> ch[J] ! request, myID, myNum[myID];
34 fi;
35 J++;
36 :: else break;
37 od;
38
39
40 do
41 :: K <= NPROCS-2 ->
42 ch[myID] ?? reply, _, _;
43 K++;
44 :: else break;
45 od;
46
47
48 cs: critical++;
49 assert (critical==1);
50 critical--;
51 requestCS[myID]=false;
52
53 byte N;
54 do
55 :: empty(deferred[myID]) -> break;
56 deferred [myID] ? N -> ch[N] ! reply, 0, 0;
57 od;
58 od;
59 }
60
61 proctype Receive(byte myID){
62 byte reqNum, source;
63 do
64 :: ch[myID] ?? request, source, reqNum;
65
66 highestNum[myID] = ((reqNum > highestNum[myID]) -> reqNum : highestNum[myID]);
67
68 atomic {
69 if
70 :: requestCS[myID] && ((myNum[myID] < reqNum) || ((myNum[myID] == reqNum) && (myID < source))) -> deferred[myID] ! source
71 :: else -> ch[source] ! reply, 0, 0;
72 fi;
73 }
74 od;
75 }
我做错了什么?
提前谢谢
P.S。关键 - 是一个关键部分“模拟器”,因为这个算法适用于分布式系统......
答案 0 :(得分:1)
由于几个原因,验证可能会停留在第37/40行。您之前的代码是:
32 if
33 :: J != myID -> ch[J] ! request, myID, myNum[myID];
34 fi;
如果if
或J == myID
已填满且永不清空,则此ch[J]
语句将永久阻止。您可以通过向else
添加if
并小心处理“队列已满”的情况来“修复”此问题。当然,“修复”可能与您尝试建模的内容不一致。