gpt4 book ai didi

verification - 什么导致 Promela/SPIN 超时?

转载 作者:行者123 更新时间:2023-12-04 04:38:53 24 4
gpt4 key购买 nike

我有以下 promela 代码:

chan level = [0] of {int};

proctype Sensor (chan levelChan) {
int x;
do
:: true ->
levelChan ? x;
if
:: (x < 2) -> printf("low %d", x);
:: (x > 8) -> printf("high %d", x);
:: else -> printf("normal %d", x);
fi
od
}

init {
run Sensor(level);
int lvl = 5;
level ! lvl;
lvl = 0;
do
:: true ->
level ! lvl;
lvl++;
(lvl > 9) -> break;
od
}

我希望将级别 (0-9) 信息发送到 channel ,并根据此级别使传感器输出低|正常|高。它很简单。但是为什么 SPIN 总是说超时呢?

  0:    proc  - (:root:) creates proc  0 (:init:)
Starting Sensor with pid 1
1: proc 0 (:init:) creates proc 1 (Sensor)
1: proc 0 (:init:) 1.pml:18 (state 1) [(run Sensor(level))]
2: proc 1 (Sensor) 1.pml:6 (state 11) [(1)]
3: proc 0 (:init:) 1.pml:20 (state 2) [lvl = 5]
4: proc 0 (:init:) 1.pml:20 (state 3) [level!lvl]
4: proc 1 (Sensor) 1.pml:8 (state 2) [levelChan?x]
5: proc 1 (Sensor) 1.pml:9 (state 9) [else]
6: proc 0 (:init:) 1.pml:21 (state 4) [lvl = 0]
normal 5 8: proc 1 (Sensor) 1.pml:12 (state 8) [printf('normal %d',x)]
9: proc 0 (:init:) 1.pml:22 (state 10) [(1)]
12: proc 1 (Sensor) 1.pml:6 (state 11) [(1)]
13: proc 0 (:init:) 1.pml:24 (state 6) [level!lvl]
13: proc 1 (Sensor) 1.pml:8 (state 2) [levelChan?x]
14: proc 1 (Sensor) 1.pml:9 (state 9) [((x<2))]
low 0 15: proc 1 (Sensor) 1.pml:10 (state 4) [printf('low %d',x)]
17: proc 0 (:init:) 1.pml:25 (state 7) [lvl = (lvl+1)]
19: proc 1 (Sensor) 1.pml:6 (state 11) [(1)]
timeout
#processes: 2
19: proc 1 (Sensor) 1.pml:8 (state 2)
19: proc 0 (:init:) 1.pml:26 (state 8)
2 processes created

似乎只执行 do 循环的 1 次迭代,为什么?

最佳答案

init 进程的 do 循环中的语句按顺序运行。

do 
:: true ->
level ! lvl;
lvl++;
(lvl > 9) -> break;
od

第一次运行 do 循环时,它将通过 level channel 发送 lvl,增加 lvl(现在是1) 然后测试(lvl > 9)。这是错误的,因此会阻塞并导致超时。

要在 do 循环中有多个选项,您需要 :: 来定义每个选项的开始:

do
:: true ->
level ! lvl;
lvl++;
:: (lvl > 9) -> break;
od

然而,这仍然不会按预期执行。当 lvl 大于 9 时,do 循环的两个选项都有效,可以选择其中一个,因此当 lvl > 9 时循环不一定会中断,它可以选择继续通过 level channel 发送 lvl 并增加 lvl

第一个 do 选项和第二个 do 选项都需要有条件:

do 
:: (lvl <= 9) ->
level ! lvl;
lvl++;
:: (lvl > 9) -> break;
od

对于此示例,使用 for 循环语法可能会更好:

init {
run Sensor(level);
int lvl = 5;
level ! lvl;

for (lvl : 0..9){
level ! lvl;
}
}

请注意,此示例仍会出现由 Sensor 的 do 循环引起的超时错误,当 init 进程完成时 Sensor 将仍然尝试从 level channel 读取并超时。

关于verification - 什么导致 Promela/SPIN 超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14671018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com