<?xml version='1.0' encoding='utf-8'?><!DOCTYPE nta PUBLIC '-//Uppaal Team//DTD Flat System 1.0//EN' 'http://www.docs.uu.se/docs/rtmv/uppaal/xml/flat-1_0.dtd'><nta><declaration>
//This model corresponds to the incorrect solution to the infinite-buffer
//Producer/Consumer Problem Using Binary Semaphores discussed by Stallings
//in Figure 5.9 (Operating Systems, Fifth edition).

//Model constructed by Frits Vaandrager on 15/3/06
//Ideas from Martijn Hendriks, Koen Vermeer, Justus Freijzer, and Bart Kerkhoff
//have been used in this model.

const int M = 2;  //Number of semaphores
const int N = 2;  //Number of processes

//Names of semaphores
const int s = 0;
const int delay = 1;

//Names of processes
const int producer = 0;
const int consumer = 1;

chan semWait[M][N];
chan semSignal[M][N];
chan semGo[M][N];

//The integer variable from the program, which intends to denote the number of products
//that are produced but not consumed
int n = 0;</declaration><template><name x="5" y="5">Consumer</name><declaration>
void take(){}

void consume(){}</declaration><location id="id0" x="320" y="288"></location><location id="id1" x="320" y="96"></location><location id="id2" x="512" y="96"></location><location id="id3" x="704" y="96"></location><location id="id4" x="704" y="224"></location><location id="id5" x="704" y="352"></location><location id="id6" x="512" y="352"></location><location id="id7" x="320" y="352"></location><location id="id8" x="320" y="192"></location><init ref="id0"/><transition><source ref="id7"/><target ref="id0"/><label kind="guard" x="328" y="312">n==0</label></transition><transition><source ref="id7"/><target ref="id1"/><label kind="guard" x="240" y="312">n != 0</label><nail x="288" y="352"/><nail x="288" y="96"/></transition><transition><source ref="id1"/><target ref="id2"/><label kind="synchronisation" x="344" y="72">semWait[s][consumer]!</label></transition><transition><source ref="id2"/><target ref="id3"/><label kind="synchronisation" x="536" y="72">semGo[s][consumer]?</label></transition><transition><source ref="id3"/><target ref="id4"/><label kind="assignment" x="712" y="144">take()</label></transition><transition><source ref="id4"/><target ref="id5"/><label kind="assignment" x="712" y="272">n--</label></transition><transition><source ref="id5"/><target ref="id6"/><label kind="synchronisation" x="536" y="328">semSignal[s][consumer]!</label></transition><transition><source ref="id6"/><target ref="id7"/><label kind="assignment" x="376" y="328">consume()</label></transition><transition><source ref="id8"/><target ref="id1"/><label kind="synchronisation" x="328" y="152">semGo[delay][consumer]?</label></transition><transition><source ref="id0"/><target ref="id8"/><label kind="synchronisation" x="328" y="240">semWait[delay][consumer]!</label></transition></template><template><name x="5" y="5">Producer</name><declaration>//Insert local declarations of clocks, variables and constants.

void produce(){}

void append(){}



</declaration><location id="id9" x="-800" y="-160"></location><location id="id10" x="-832" y="-352"></location><location id="id11" x="-640" y="-352"></location><location id="id12" x="-448" y="-352"></location><location id="id13" x="-832" y="-224"></location><location id="id14" x="-448" y="-96"></location><location id="id15" x="-640" y="-96"></location><location id="id16" x="-832" y="-96"></location><init ref="id10"/><transition><source ref="id16"/><target ref="id9"/><label kind="guard" x="-808" y="-128">n==1</label></transition><transition><source ref="id16"/><target ref="id13"/><label kind="guard" x="-880" y="-128">n != 1</label></transition><transition><source ref="id10"/><target ref="id11"/><label kind="assignment" x="-776" y="-376">produce()</label></transition><transition><source ref="id11"/><target ref="id12"/><label kind="synchronisation" x="-608" y="-376">semWait[s][producer]!</label></transition><transition><source ref="id9"/><target ref="id13"/><label kind="synchronisation" x="-808" y="-216">semSignal[delay][producer]!</label></transition><transition><source ref="id13"/><target ref="id10"/><label kind="synchronisation" x="-824" y="-288">semSignal[s][producer]!</label></transition><transition><source ref="id12"/><target ref="id14"/><label kind="synchronisation" x="-440" y="-240">semGo[s][producer]?</label></transition><transition><source ref="id14"/><target ref="id15"/><label kind="assignment" x="-576" y="-120">append()</label></transition><transition><source ref="id15"/><target ref="id16"/><label kind="assignment" x="-736" y="-120">n++</label></transition></template><template><name>Semaphore</name><parameter>const int id,  const int queue_size, const int init_val</parameter><declaration>//The value of the semaphore
bool value = init_val;
//The queue of the semaphore
// -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]&gt;=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;
}

bool emptyQueue ( )
{
  return (queue[0]==-1);
}</declaration><location id="id17" x="384" y="64"><committed/></location><location id="id18" x="608" y="320"><committed/></location><location id="id19" x="352" y="96"><name x="288" y="64">overflow</name></location><location id="id20" x="480" y="192"></location><init ref="id17"/><transition><source ref="id17"/><target ref="id20"/><label kind="assignment" x="408" y="72">initialize()</label></transition><transition><source ref="id20"/><target ref="id20"/><label kind="select" x="256" y="192">p:int[0,N-1]</label><label kind="guard" x="256" y="208">emptyQueue()</label><label kind="synchronisation" x="256" y="224">semSignal[id][p]?</label><label kind="assignment" x="256" y="240">value=1</label><nail x="352" y="192"/><nail x="352" y="224"/></transition><transition><source ref="id20"/><target ref="id18"/><label kind="select" x="552" y="104">p:int[0,N-1]</label><label kind="guard" x="552" y="120">value==1</label><label kind="synchronisation" x="552" y="136">semWait[id][p]?</label><label kind="assignment" x="552" y="152">value=0,
q=p</label><nail x="608" y="192"/></transition><transition><source ref="id20"/><target ref="id19"/><label kind="select" x="240" y="112">p:int[0,N-1]</label><label kind="guard" x="240" y="128">value==0 &amp;&amp; fullQueue()</label><label kind="synchronisation" x="240" y="144">semWait[id][p]?</label></transition><transition><source ref="id18"/><target ref="id20"/><label kind="synchronisation" x="512" y="208">semGo[id][q]!</label></transition><transition><source ref="id20"/><target ref="id18"/><label kind="select" x="360" y="280">p:int[0,N-1]</label><label kind="guard" x="360" y="296">not emptyQueue()</label><label kind="synchronisation" x="360" y="312">semSignal[id][p]?</label><label kind="assignment" x="360" y="328">q=headQueue(),
deQueue()</label><nail x="480" y="320"/></transition><transition><source ref="id20"/><target ref="id20"/><label kind="select" x="480" y="-8">p:int[0,N-1]</label><label kind="guard" x="480" y="8">value==0 &amp;&amp; not fullQueue()</label><label kind="synchronisation" x="480" y="24">semWait[id][p]?</label><label kind="assignment" x="480" y="40">enQueue(p)</label><nail x="480" y="64"/><nail x="512" y="64"/></transition></template><system>//Insert process assignments.


Sem_s = Semaphore(s,1,1);
Sem_delay = Semaphore(delay,1,0);

//Edit system definition.
system Consumer,Producer,Sem_s,Sem_delay;</system></nta>