-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsem.xml
More file actions
86 lines (76 loc) · 11.2 KB
/
sem.xml
File metadata and controls
86 lines (76 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE nta PUBLIC '-//Uppaal Team//DTD Flat System 1.1//EN' 'http://www.it.uu.se/research/group/darts/uppaal/flat-1_1.dtd'><nta><declaration>//This model is an adaptation by Frits Vaandrager of
//a model made by Martijn Hendriks
// Number of processes in system:
const int N = 4;
// Number of semaphores:
const int M = 5;
// Names of the semaphores, ranging from 0 to M-1:
const int x = 0, y = 1, z = 2, wsem = 3, rsem = 4;
// Some counters
int readcount = 0, writecount = 0;
// The channels to synchronize with the semafores
// A call semWait(s) by process p translates to a sequence of two transitions labeled with
// semWait(s)(p)!
// semGo(s)(p)?
//A call semSignal(s) by process p translates to a transition labeled with
// semSignal(s)(p)!
chan semWait[M][N], semGo[M][N], semSignal[M][N];
</declaration><template><name>Semaphore</name><parameter>const int id, const int queue_size, const int init_val</parameter><declaration>//The value of the semaphore
int count = init_val;
//The queue of the semaphore
//value -1 denotes an empty array entry
int[-1,N-1] queue[queue_size];
//An auxiliary variable used to temporarily store process id
meta int[0,N-1] q;
void initialize ()
{
for (i : int[0,queue_size-1])
{queue[i] = -1;}
}
void deQueue ()
{
for (i : int[1,queue_size-1])
queue[i-1] = queue[i];
queue[queue_size-1] = -1;
}
void enQueue (int p)
{
int i = 0;
while (queue[i]>=0){i++;};
queue[i]=p;
}
int[0,N-1] headQueue ( )
{
return queue[0];
}
bool fullQueue ( )
{
for (i : int[0,queue_size-1])
{
if (queue[i]==-1) {return false;}
}
return true;
}</declaration><location id="id0" x="384" y="64"><committed/></location><location id="id1" x="608" y="320"><committed/></location><location id="id2" x="352" y="96"><name x="288" y="64">overflow</name></location><location id="id3" x="480" y="192"></location><init ref="id0"/><transition><source ref="id0"/><target ref="id3"/><label kind="assignment" x="408" y="72">initialize()</label></transition><transition><source ref="id3"/><target ref="id3"/><label kind="select" x="272" y="192">p:int[0,N-1]</label><label kind="guard" x="272" y="208">count>=0</label><label kind="synchronisation" x="272" y="224">semSignal[id][p]?</label><label kind="assignment" x="272" y="240">count++</label><nail x="352" y="192"/><nail x="352" y="224"/></transition><transition><source ref="id3"/><target ref="id1"/><label kind="select" x="552" y="104">p:int[0,N-1]</label><label kind="guard" x="552" y="120">count>0</label><label kind="synchronisation" x="552" y="136">semWait[id][p]?</label><label kind="assignment" x="552" y="152">count--,
q=p</label><nail x="608" y="192"/></transition><transition><source ref="id3"/><target ref="id2"/><label kind="select" x="248" y="112">p:int[0,N-1]</label><label kind="guard" x="248" y="128">count<=0 && fullQueue()</label><label kind="synchronisation" x="248" y="144">semWait[id][p]?</label></transition><transition><source ref="id1"/><target ref="id3"/><label kind="synchronisation" x="512" y="208">semGo[id][q]!</label></transition><transition><source ref="id3"/><target ref="id1"/><label kind="select" x="360" y="280">p:int[0,N-1]</label><label kind="guard" x="360" y="296">count<0</label><label kind="synchronisation" x="360" y="312">semSignal[id][p]?</label><label kind="assignment" x="360" y="328">count++,
q=headQueue(),
deQueue()</label><nail x="480" y="320"/></transition><transition><source ref="id3"/><target ref="id3"/><label kind="select" x="480" y="-24">p:int[0,N-1]</label><label kind="guard" x="480" y="-8">count<=0 && not fullQueue()</label><label kind="synchronisation" x="480" y="8">semWait[id][p]?</label><label kind="assignment" x="480" y="24">count--,
enQueue(p)</label><nail x="480" y="64"/><nail x="512" y="64"/></transition></template><template><name x="5" y="5">Reader</name><parameter>const int pid</parameter><location id="id4" x="-456" y="-40"></location><location id="id5" x="-600" y="-128"></location><location id="id6" x="-456" y="-128"></location><location id="id7" x="-344" y="-64"></location><location id="id8" x="-344" y="-16"></location><location id="id9" x="-336" y="128"><name x="-288" y="120">cs</name></location><location id="id10" x="-336" y="320"></location><location id="id11" x="-336" y="384"></location><location id="id12" x="-584" y="376"></location><location id="id13" x="-680" y="376"></location><location id="id14" x="-584" y="304"></location><location id="id15" x="-680" y="304"></location><location id="id16" x="-704" y="240"></location><location id="id17" x="-704" y="184"></location><location id="id18" x="-720" y="128"></location><location id="id19" x="-720" y="64"></location><location id="id20" x="-736" y="0"></location><location id="id21" x="-736" y="-64"></location><location id="id22" x="-736" y="-128"></location><init ref="id22"/><transition><source ref="id4"/><target ref="id5"/><label kind="synchronisation" x="-616" y="-40">semSignal[wsem][pid]!</label><nail x="-600" y="-40"/></transition><transition><source ref="id6"/><target ref="id4"/><label kind="guard" x="-504" y="-104">readcount == 0</label></transition><transition><source ref="id5"/><target ref="id22"/><label kind="synchronisation" x="-720" y="-152">semSignal[x][pid]!</label></transition><transition><source ref="id6"/><target ref="id5"/><label kind="guard" x="-568" y="-152">readcount != 0</label></transition><transition><source ref="id7"/><target ref="id6"/><label kind="assignment" x="-336" y="-128">readcount--</label><nail x="-344" y="-128"/></transition><transition><source ref="id8"/><target ref="id7"/><label kind="synchronisation" x="-336" y="-48">semGo[x][pid]?</label></transition><transition><source ref="id9"/><target ref="id8"/><label kind="synchronisation" x="-336" y="32">semWait[x][pid]!</label></transition><transition><source ref="id10"/><target ref="id9"/><label kind="synchronisation" x="-328" y="224">semSignal[z][pid]!</label></transition><transition><source ref="id11"/><target ref="id10"/><label kind="synchronisation" x="-320" y="344">semSignal[rsem][pid]!</label></transition><transition><source ref="id13"/><target ref="id11"/><label kind="synchronisation" x="-552" y="432">semSignal[x][pid]!</label><nail x="-680" y="456"/><nail x="-328" y="456"/></transition><transition><source ref="id12"/><target ref="id13"/><label kind="synchronisation" x="-656" y="384">semGo[wsem][pid]?</label></transition><transition><source ref="id14"/><target ref="id12"/><label kind="synchronisation" x="-576" y="328">semWait[wsem][pid]!</label></transition><transition><source ref="id15"/><target ref="id14"/><label kind="guard" x="-672" y="272">readcount == 1</label></transition><transition><source ref="id15"/><target ref="id13"/><label kind="guard" x="-776" y="328">readcount != 1</label></transition><transition><source ref="id16"/><target ref="id15"/><label kind="assignment" x="-776" y="264">readcount++</label></transition><transition><source ref="id17"/><target ref="id16"/><label kind="synchronisation" x="-808" y="200">semGo[x][pid]?</label></transition><transition><source ref="id18"/><target ref="id17"/><label kind="synchronisation" x="-816" y="144">semWait[x][pid]!</label></transition><transition><source ref="id19"/><target ref="id18"/><label kind="synchronisation" x="-848" y="80">semGo[rsem][pid]?</label></transition><transition><source ref="id20"/><target ref="id19"/><label kind="synchronisation" x="-864" y="16">semWait[rsem][pid]!</label></transition><transition><source ref="id21"/><target ref="id20"/><label kind="synchronisation" x="-840" y="-48">semGo[z][pid]?</label></transition><transition><source ref="id22"/><target ref="id21"/><label kind="synchronisation" x="-840" y="-112">semWait[z][pid]!</label></transition></template><template><name x="5" y="5">Writer</name><parameter>const int pid</parameter><location id="id23" x="-616" y="-120"></location><location id="id24" x="-528" y="-48"></location><location id="id25" x="-528" y="-120"></location><location id="id26" x="-440" y="-120"></location><location id="id27" x="-440" y="-32"></location><location id="id28" x="-440" y="48"></location><location id="id29" x="-440" y="168"><name x="-400" y="160">cs</name></location><location id="id30" x="-440" y="264"></location><location id="id31" x="-736" y="256"></location><location id="id32" x="-648" y="168"></location><location id="id33" x="-648" y="88"></location><location id="id34" x="-736" y="168"></location><location id="id35" x="-736" y="88"></location><location id="id36" x="-736" y="16"></location><location id="id37" x="-736" y="-56"></location><location id="id38" x="-736" y="-128"></location><init ref="id38"/><transition><source ref="id24"/><target ref="id23"/><label kind="synchronisation" x="-672" y="-48">semSignal[rsem][pid]!</label><nail x="-608" y="-48"/></transition><transition><source ref="id23"/><target ref="id38"/><label kind="synchronisation" x="-744" y="-160">semSignal[y][pid]!</label></transition><transition><source ref="id25"/><target ref="id24"/><label kind="guard" x="-576" y="-96">writecount == 0</label></transition><transition><source ref="id25"/><target ref="id23"/><label kind="guard" x="-624" y="-144">writecount != 0</label></transition><transition><source ref="id26"/><target ref="id25"/><label kind="assignment" x="-520" y="-152">writecount--</label></transition><transition><source ref="id27"/><target ref="id26"/><label kind="synchronisation" x="-432" y="-72">semGo[y][pid]?</label></transition><transition><source ref="id28"/><target ref="id27"/><label kind="synchronisation" x="-432" y="0">semWait[y][pid]!</label></transition><transition><source ref="id29"/><target ref="id28"/><label kind="synchronisation" x="-432" y="96">semSignal[wsem][pid]!</label></transition><transition><source ref="id30"/><target ref="id29"/><label kind="synchronisation" x="-568" y="216">semGo[wsem][pid]?</label></transition><transition><source ref="id31"/><target ref="id30"/><label kind="synchronisation" x="-656" y="280">semWait[wsem][pid]!</label><nail x="-736" y="312"/><nail x="-440" y="312"/></transition><transition><source ref="id34"/><target ref="id31"/><label kind="synchronisation" x="-856" y="200">semSignal[y][pid]!</label></transition><transition><source ref="id32"/><target ref="id34"/><label kind="synchronisation" x="-712" y="176">semGo[rsem][pid]?</label></transition><transition><source ref="id33"/><target ref="id32"/><label kind="synchronisation" x="-640" y="112">semWait[rsem][pid]!</label></transition><transition><source ref="id35"/><target ref="id34"/><label kind="guard" x="-832" y="112">writecount != 1</label></transition><transition><source ref="id35"/><target ref="id33"/><label kind="guard" x="-720" y="56">writecount == 1</label></transition><transition><source ref="id36"/><target ref="id35"/><label kind="assignment" x="-824" y="32">writecount++</label></transition><transition><source ref="id37"/><target ref="id36"/><label kind="synchronisation" x="-840" y="-32">semGo[y][pid]?</label></transition><transition><source ref="id38"/><target ref="id37"/><label kind="synchronisation" x="-848" y="-112">semWait[y][pid]!</label></transition></template><system>R0 = Reader(0);
R1 = Reader(1);
W0 = Writer(2);
W1 = Writer(3);
Sem_x = Semaphore(x,N-1,1);
Sem_y = Semaphore(y,N-1,1);
Sem_z = Semaphore(z,N-1,1);
Sem_wsem = Semaphore(wsem,N-1,1);
Sem_rsem = Semaphore(rsem,N-1,1);
system
R0,
R1,
W0,
W1,
Sem_x,
Sem_y,
Sem_z,
Sem_wsem,
Sem_rsem;
</system></nta>