The following algorithm attempts to enforce mutual exclusion between two processes P1 and P2 each of which runs the code below. You can assume that initially sema = 0.

while true do{
atomic{if sema = 0
then sema:= 1
else
 go to line 2}
critical section;
sema:= 0;
  }

How Can I model this code in promela/SPIN?

Thank you.

有帮助吗?

解决方案

This should be quite straightforward:

active proctype P() {
  bit sema = 0;

  do
    :: atomic {
         sema == 0 -> sema = 1
       }

       // critical section

       sema = 0
  od
}

Possibly you do not need the do loop, if in your code you only needed it for some active waiting. The atomic block is only executable if sema is set to 0, and then it executes at once. Spin has a built-in passive waiting semantics.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top