0% found this document useful (0 votes)
36 views29 pages

SPIN Examples

Uploaded by

Benjamin Huynh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views29 pages

SPIN Examples

Uploaded by

Benjamin Huynh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 29

Flavio Lerda Carnegie Mellon University SPIN Examples

SPIN Examples

Bug Catching 1 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion
• Peterson’s solution to the mutual exclusion
problem

Bug Catching 2 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion
• Peterson’s solution to the mutual exclusion
problem

Bug Catching 3 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion
• Peterson’s solution to the mutual exclusion
problem
flag0=1

Bug Catching 4 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion
• Peterson’s solution to the mutual exclusion
problem
flag0=1

turn=0

Bug Catching 5 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion
• Peterson’s solution to the mutual exclusion
problem
flag0=1

turn=0

flag1 == 0 || turn == 1

Bug Catching 6 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion
• Peterson’s solution to the mutual exclusion
problem
flag0=1

turn=0

flag1 == 0 || turn == 1

flag1 != 0 && turn != 1

Bug Catching 7 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion
• Peterson’s solution to the mutual exclusion
problem
flag0=1

turn=0

flag1 == 0 || turn == 1

flag1 != 0 && turn != 1

Bug Catching 8 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion
• Peterson’s solution to the mutual exclusion
problem
flag0=1

turn=0

flag1 == 0 || turn == 1

flag1 != 0 && turn != 1

Critical
Section

Bug Catching 9 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion
• Peterson’s solution to the mutual exclusion
problem
flag0=1

turn=0 flag0=0

flag1 == 0 || turn == 1

flag1 != 0 && turn != 1

Critical
Section

Bug Catching 10 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion
• Peterson’s solution to the mutual exclusion
problem
flag0=1

turn=0 flag0=0

flag1 == 0 || turn == 1

flag1 != 0 && turn != 1

Critical
Section

Bug Catching 11 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion in SPIN

proctype mutex0() {

flag0=1

turn=0 flag0=0

flag1 == 0 || turn == 1

} flag1 != 0 && turn != 1

Critical
Section
Bug Catching 12 15-398
Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion in SPIN


bool turn;

proctype mutex0() {

flag0=1

turn=0 flag0=0

flag1 == 0 || turn == 1

} flag1 != 0 && turn != 1

Critical
Section
Bug Catching 13 15-398
Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion in SPIN


bool turn;
bool flag[2];
proctype mutex0() {

flag0=1

turn=0 flag0=0

flag1 == 0 || turn == 1

} flag1 != 0 && turn != 1

Critical
Section
Bug Catching 14 15-398
Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion in SPIN


bool turn;
bool flag[2];
proctype mutex0() {

flag[0] = 1;
flag0=1

turn=0 flag0=0

flag1 == 0 || turn == 1

} flag1 != 0 && turn != 1

Critical
Section
Bug Catching 15 15-398
Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion in SPIN


bool turn;
bool flag[2];
proctype mutex0() {

flag[0] = 1;
flag0=1
turn = 0;

turn=0 flag0=0

flag1 == 0 || turn == 1

} flag1 != 0 && turn != 1

Critical
Section
Bug Catching 16 15-398
Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion in SPIN


bool turn;
bool flag[2];
proctype mutex0() {

flag[0] = 1;
flag0=1
turn = 0;
(flag[1] == 0 || turn == 0);

turn=0 flag0=0

flag1 == 0 || turn == 1

} flag1 != 0 && turn != 1

Critical
Section
Bug Catching 17 15-398
Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion in SPIN


bool turn;
bool flag[2];
proctype mutex0() {

flag[0] = 1;
flag0=1
turn = 0;
(flag[1] == 0 || turn == 0);
/* critical section */ turn=0 flag0=0

flag1 == 0 || turn == 1

} flag1 != 0 && turn != 1

Critical
Section
Bug Catching 18 15-398
Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion in SPIN


bool turn;
bool flag[2];
proctype mutex0() {

flag[0] = 1;
flag0=1
turn = 0;
(flag[1] == 0 || turn == 0);
/* critical section */ turn=0 flag0=0
flag[0] = 0;
flag1 == 0 || turn == 1

} flag1 != 0 && turn != 1

Critical
Section
Bug Catching 19 15-398
Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion in SPIN


bool turn;
bool flag[2];
proctype mutex0() {
again:
flag[0] = 1;
flag0=1
turn = 0;
(flag[1] == 0 || turn == 0);
/* critical section */ turn=0 flag0=0
flag[0] = 0;
flag1 == 0 || turn == 1
goto again;
} flag1 != 0 && turn != 1

Critical
Section
Bug Catching 20 15-398
Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion in SPIN


bool turn, flag[2]; Active process:
automatically creates instances of processes

active [2] proctype user()


{ _pid:
assert(_pid == 0 || __pid == 1); Identifier of the process
again:
flag[_pid] = 1;
assert:
Checks that turn
there= are
_pid;
only
(flag[1 - _pid] == 0 || turn == 1 - _pid);
at most two instances with
identifiers 0 and 1
/* critical section */

flag[_pid] = 0;
goto again;
}

Bug Catching 21 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion in SPIN


bool turn, flag[2];
ncrit:
byte ncrit;
Counts the number of
Process in the critical section
active [2] proctype user()
{
assert(_pid == 0 || __pid == 1);
again:
flag[_pid] = 1;
turn = _pid;
(flag[1 - _pid] == 0 || turn == 1 - _pid);

ncrit++;
assert(ncrit == 1); /* critical section */
ncrit--;
assert:
Checks that there are always
flag[_pid] = 0;
at most one process in the
goto again;
critical section
}

Bug Catching 22 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Mutual Exclusion in SPIN


bool turn, flag[2];
LTL Properties:
bool critical[2];

[] (critial[0] || critical[1])
active [2] proctype user()
{
[] <> (critical[0])
assert(_pid == 0 || __pid == 1);
[] <> (critical[1])
again:
flag[_pid] = 1;
[] (critical[0] ->
turn = _pid;
(critial[0] U
(flag[1 - _pid] == 0 || turn == 1 - _pid); (!critical[0] &&
((!critical[0] &&
critical[_pid] = 1; !critical[1]) U critical[1]))))
/* critical section */ [] (critical[1] ->
critical[_pid] = 0; (critial[1] U
(!critical[1] &&
flag[_pid] = 0; ((!critical[1] &&
goto again; !critical[0]) U critical[0]))))
}

Bug Catching 23 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Alternating Bit Protocol


• Two processes want to communicate
• They want acknowledge of received
messages
• Sending window of one message
• Each message is identified by one bit
• Alternating values of the identifier

Bug Catching 24 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Alternating Bit Protocol


Sender Receiver

msg0

ack0

msg1

ack1

msg0

ack0

msg1

Bug Catching 25 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Alternating Bit Protocol


Sender Receiver

msg0

ack1

msg0

ack0

Bug Catching 26 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Alternating Bit Protocol


Sender Receiver

msg0

msg0

ack0

msg1

ack1

Bug Catching 27 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Sender Process
active proctype Sender()
{
do
:: ::
if if
:: receiver?msg0; :: receiver?msg1;
:: skip :: skip
fi; fi;
do do
:: sender?ack0 -> break :: sender?ack1 -> break
:: sender?ack1 :: sender?ack0
:: timeout -> :: timeout ->
if if
:: receiver!msg0; :: receiver!msg1;
:: skip :: skip
fi; fi;
od; od;
od;
}

Bug Catching 28 15-398


Flavio Lerda Carnegie Mellon University SPIN Examples

Receiver Process
active proctype Receiver() mtype = { msg0, msg1, ack0, ack1 }
{ chan sender = [1] of { mtype };
do chan receiver = [1] of { mtype };
::
do
:: receiver?msg0 ->
sender!ack0; break;
:: receiver?msg1 ->
server!ack1
od

do
:: receiver?msg1 ->
sender!ack1; break;
:: receiver?msg0 ->
server!ack0
od
od
}

Bug Catching 29 15-398

You might also like