【问题标题】:How do you check if all values in array are equal in Promela?你如何检查数组中的所有值在 Promela 中是否相等?
【发布时间】:2014-03-23 21:08:51
【问题描述】:

如果数组的所有值都相等,你如何检查 Promela?

我希望这段代码是原子的,并且如果它们是可执行的(忙于等待,直到所有都相等)。

有没有办法使用 for 循环? (数组的长度作为参数给出)

【问题讨论】:

    标签: promela


    【解决方案1】:

    您可以尝试以下 sn-p 中的内容(假定数组非空):

    #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。否则,如果最初的值不相等,检查将永远不会成功。

    【讨论】:

    • 不应该 else 声明 result -> breakresult = true
    • @TallChuck:你说得对,我更新了我的答案。谢谢!
    猜你喜欢
    • 2013-01-27
    • 1970-01-01
    • 2015-08-24
    • 1970-01-01
    • 2012-05-20
    • 1970-01-01
    • 2018-11-27
    • 1970-01-01
    • 2019-03-04
    相关资源
    最近更新 更多