如果数组的所有值都相等,您如何检查Promela?
我希望这段代码是原子的,并且如果它们是可执行的(繁忙等待,直到所有都相同)。
有没有办法使用for循环? (数组的长度作为参数给出)
答案 0 :(得分:2)
你可以尝试类似下面的代码片段(假设数组非空):
#define N 3
int arr[N];
proctype Test(int length) {
int i, val;
bool result = true;
do
:: atomic {
/* check for equality */
val = arr[0];
for (i : 1 .. length-1) {
if
:: arr[i] != val ->
result = false;
break
:: else -> skip
fi
}
/* Leave loop if all values are equal,
else one more iteration (active waiting).
Before the next entrance into the atomic
block, other proctypes will have the chance
to interleave and change the array values */
if
:: result -> break;
:: else -> result = true
fi
}
od
/* will end up here iff result == true */
}
init {
arr[0] = 2;
arr[1] = 2;
arr[2] = 2;
run Test(N);
}
原子块内的代码不会阻塞,因此可以连续执行。
/ edit(2019-01-24):在原子语句之后的条件块的else部分中将result
设置为true。否则,如果最初的值不相等,则检查将永远不会成功。