Question

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.

Était-ce utile?

La solution

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.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top