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.

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top