Question 1: Give examples of process definitions P and Q in CCS for

Question 1: Give examples of process definitions P and Q in CCS for which:
a). P=Q but P ~ Q
Answer:
P = .τ..Halt,
Q =..Halt+.τ..Halt
Obviously, P ~ Q, because,
.
 Halt, but there is not a descendent of Q such that Q can reach by action
Q 
.. On the other hand, P=Q, because, all decedents of P and Q are consistent with
observation congruence property.
b). P≈Q but P≠Q
Answer:
P= τ..Halt, Q=.Halt
P≈Q [ref. Class note.]
But, P≠Q, since: P
 .


Halt, but there is not Q’ such that
Q  Q'
Question 2. Synchronous message passing requires both the sender and receiver to
block until the communication is performed.
a). If the communication consists of writing to and reading from a global variable int
buffer; give an implementation in java or pthread of synchronous communication
between 2 threads.
Answer: Java codes are attached.
public class SynchMP1{
private int buffer=0;
private boolean empty=true;
public static void main(String[] args){
SynchMP1 obj = new SynchMP1();
Thread wt = new SynchWriter(obj);
Thread rt = new SynchReader(obj);
wt.start();
rt.start();
}
public int getBuffer() { return buffer;}
public void setBuffer(int b){ buffer=b;}
public boolean isEmpty(){ return empty;}
public void setEmpty(boolean e){ empty=e;}
}
class SynchWriter extends Thread{
SynchMP1 synObj = null;
public SynchWriter(SynchMP1 obj){ synObj = obj;}
public void run() {
try{
while(true){
synchronized(synObj){
synObj.setBuffer(1);
synObj.setEmpty(false);
System.out.println("---writer writes buffer = "+1);
synObj.notify();
while(!synObj.isEmpty()) synObj.wait();//wait for reader reads.
}
System.out.println("---Writer leaves.");
}
}catch(Exception e){ e.printStackTrace(System.out);}
}
}
class SynchReader extends Thread{
SynchMP1 synObj = null;
public SynchReader(SynchMP1 obj){ synObj = obj;}
public void run(){
try{
while(true){
synchronized(synObj){
while(synObj.isEmpty()) synObj.wait();//wait for writer write.
int buffer =synObj.getBuffer();
synObj.setEmpty(true);
System.out.println("===reader reads buffer = "+buffer);
synObj.notify();
}
System.out.println("===reader leaves");
}
}catch(Exception e){ e.printStackTrace(System.out);}
}
}
b). Codes are attached.
import java.io.*;
import java.util.Hashtable;
import java.util.StringTokenizer;
public class SynchMP2 {
public static void main(String[] args){
Hashtable buffers = new Hashtable();
String s = readInput(args[0]);
// s contains all strings separated by " "
StringTokenizer str = new StringTokenizer(s, "||");
while(str.hasMoreTokens()){
Thread t = new Process(buffers, str.nextToken());
t.start();
}
}
private static String readInput(String fileName){
String s=null;
try {
FileInputStream fis = new FileInputStream(fileName);
int n;
while ((n = fis.available()) > 0) {
byte[] b = new byte[n];
int result = fis.read(b);
if (result == -1) break;
s = new String(b);
System.out.println(s);
} // end while
fis.close();
} // end try
catch (FileNotFoundException e) {
System.err.println("Could not find file stringDB.txt");
}
catch (IOException e) {
//System.err.println(e);
}
return s;
}
}
//-------------------- Class Process for every thread ------------------------------------------class Process extends Thread{
String tID = null;
Buffer buffer = null;
Hashtable buffers = null;
String cmds = null;
public Process(Hashtable ibuffers, String str){
buffers = ibuffers;
StringTokenizer st = new StringTokenizer(str, "::");
if(st.hasMoreTokens()) tID = st.nextToken();
if(st.hasMoreTokens()) cmds = st.nextToken();
buffer = new Buffer();
buffers.put(tID, buffer);
}
public void write(String tID, String data){
try{
synchronized(buffer){
//while(!buffer.isEmpty()) buffer.wait();
buffer.setData(data);
buffer.setRecvID(tID);
buffer.setEmpty(false);
System.out.println("---Thread "+this.tID+" writes to "+
tID+" data "+data);
buffer.notify();
while(!buffer.isEmpty()) buffer.wait();
}
System.out.println("---Thread "+this.tID+ " finishes writing and leaves.");
}catch(Exception e){ e.printStackTrace(System.out);}
}
public String read(String tID){
String data = null;
try{
Buffer target = (Buffer)buffers.get(tID);
synchronized(target){
while(target.isEmpty()|| target.getRecvID().compareTo(this.tID)!=0){
target.wait();
}
data =target.getData();
target.setEmpty(true);
System.out.println("===Thread "+this.tID+" reads from "+
tID+" data "+data);
target.notify();
}
System.out.println("===Thread "+this.tID+ " finishes reading and leaves.");
}catch(Exception e){ e.printStackTrace(System.out);}
return data;
}
public void run(){
StringTokenizer st = new StringTokenizer(cmds, ";");
while(st.hasMoreTokens()){
String cmd = st.nextToken();
int index = cmd.indexOf('!');
if(index!=-1){
write(cmd.substring(0,index),cmd.substring(index+1));
}
index = cmd.indexOf('?');
if(index!=-1){
String s = read(cmd.substring(0,index));
}
}
}
}
class Buffer {
String data;
String recvID;
boolean empty;
public Buffer(String d, String rID, boolean e){
data = d; recvID = rID; empty = e;
}
public Buffer() { data = null; empty = true;}
public void setData(String d) { data = d;}
public void setRecvID(String rID) { recvID = rID;}
public void setEmpty(boolean e) { empty = e;}
public String getData() { return data;}
public String getRecvID() { return recvID;}
public boolean isEmpty(){ return empty;}
}
//------------------------- Output below -----------------------------------------------------[mimi] [~/623] java SynchMP2 input.txt
A::B!w;C!x;C?y;B!z||B::A?s;C!t;A?u||C::B?d;A?e;A!f
---Thread A writes to B data w
===Thread B reads from A data w
===Thread B finishes reading and leaves.
---Thread B writes to C data t
---Thread A finishes writing and leaves.
---Thread A writes to C data x
===Thread C reads from B data t
===Thread C finishes reading and leaves.
===Thread C reads from A data x
---Thread B finishes writing and leaves.
===Thread C finishes reading and leaves.
---Thread A finishes writing and leaves.
---Thread C writes to A data f
===Thread A reads from C data f
===Thread A finishes reading and leaves.
---Thread C finishes writing and leaves.
---Thread A writes to B data z
===Thread B reads from A data z
===Thread B finishes reading and leaves.
---Thread A finishes writing and leaves.
[mimi] [~/623]
Question 3. Strongly determine.
a). Show that strong determinacy is preserved by strong bisimulation
Answer:
Assume, P ~ Q, and P is strongly determined.




If there exist P’ and P”, s.t. P 
P’ and P 
P” through action α,
and P is strongly determined  P’~P”
(1)




P~Q  if P 
P’ through action α, Q’ such that Q 
Q’, and
P’ ~ Q’
(2)

 Q”, and
Same for Q”, i.e. Q” such that Q 
P” ~ Q”
(3)
Since strong bisimulation is a equivalent relation which preserver the property of
symmetry, reflexive and transitive,
Based on (1), P’~P”  P” ~ P’
(4)&(2)  P” ~ Q’
(5)  Q’ ~ P”
(6)&(3)  Q’ ~ Q”
(4)
(5)
(6)




Since Q 
Q’, Q 
Q”, Q’ ~ Q”, by definition of strong determinacy, Q is
strongly determined. It is proved!
b). Show that strong determinacy is not preserved by weak bisimulation.
Answer:
Let P = a.(b+τ.c) and Q = a.c+a.(b + τ.c), then P≈Q [ref. 2]
P:
Q:
a
b
τ
a
c
a
b
c
τ
c
P is strongly determined because there is no two descendents of P such that P can reach
them by the same actions.
Now let see if Q is strongly determined.
Q is not strongly determined, because:
Let Q’=c and Q”=(b+ τ.c), Q can derive Q’ and Q” by action a. However, obviously,
Q’~Q”. So Q is not strongly determined.
It is proved.
Question 4. Prove that (~n+1 ~n )for all n
Proof:
It is easy to show that (~n+1  ~n ). Let’s take an empty action ε on a pair of processes, P
and Q, which satisfy relation ~n+1. So we have:


P
P through ε  Q, Q
Q and P~nQ
It means that the processes satisfying the relation ~n+1 also satisfy ~n. So (~n+1  ~n )




Now let’s show that there are processes that satisfy ~n but do not satisfy ~n+1 for all n
Consider the family of processes defined by:
P0 = b.Halt
Q0=c.Halt
Pi+1=a.(Pi+Qi)
Qi+1=a.Pi+a.Qi
We want to show that Pi ~i Qi ~i (Pi + Qi ) but Pi ~i+1 Qi ~i+1 (Pi + Qi ) ~i+1 Pi by induction.
Induction step 1:
When n=0, since Pi ~0 Qi always holds, so does P0 ~0 Q0~0(P0+Q0)
b
b


But P0 ~1 Q0 doesn’t hold, because P0 
Halt, but there is not Q’ s.t. Q0 
Q’.
Similarly, P0 ~1 (P0+Q0) and Q0 ~1 (P0+Q0) do not hold.
Induction step 2:
Assume that Pi ~i Qi ~i (Pi + Qi ) but Pi ~i+1 Qi ~i+1 (Pi + Qi ) ~i+1 Pi, we want to show that
Pi+1 ~i+1 Qi+1 ~i+1 (Pi+1 + Qi+1 ) but Pi+1 ~i+2 Qi+1 ~i+2(Pi+1 + Qi+1 ) ~i+2 Pi+1
Now let’s see if Pi+1 ~i+1 Qi+1~i (Pi + Qi ). We know that:
Pi+1=a.(Pi+Qi)
Qi+1=a.Pi+a.Qi
a
So that Pi+1 
 (Pi + Qi).
a
a
By action a, Qi+1 
Pi or Qi 
Qi. And by the inductive assumption, we know that


Pi ~i Qi ~i (Pi + Qi ). So that by definition, Pi+1 ~i+1 Qi+1 . Similarly, we can get:
Pi+1 ~i+1 Qi+1 ~i+1 (Pi+1 + Qi+1)
Now let’s see if Pi+1 ~i+2 Qi+1 ~i+2(Pi+1 + Qi+1 ) ~i+2 Pi+1
We have deduct that:
a
a
a
Pi+1 
(Pi + Qi) and Qi+1 
Pi or Qi 
Qi.



However, by inductive assumption, we know that Pi, Qi, (Pi + Qi) do not preserve relation
~i+1 . So by definition we can conclude that Pi ~i+2 Qi
Since Pi ~i+1 Qi ~i+1 (Pi + Qi )~Pi but Pi ~i Qi ~i (Pi + Qi ) and (~n+1  ~n ), we can conclude
that (~n+1  ~n ) for all n.
It is proved.
Question 5. Describe a dataflow network schemata for a for loop using
homogeneous actors and switch and merge. Your network will receive n (n>=1) and
x and will produce fn(x)
a).Generator:
n
0
T
F
F
initially F
merge
T
merge
-1
+1
>0
>1
FTn-1
Tn-1F
b). for-loop on a function f using generator above
n
x
generator
Tn-1F
merge
FTn-1
f(x)
fn (x)
Question 6. Show that the following function is stable.



f ( x, y , z )  


if (a, b, ) 
( x, y, z ).......... A
if (b, , a) 
( x, y, z ) ...........B
if (, a, b)  ( x, y, z ) ...........C
otherwise
..................D
Answer:
To show a function f is stable, it is enough to show that for two consistent elements, d
and d’ (Consistent means that there exists a Z such that Z>=d and Z>=d’), f(dПd’)=f(d) П
f(d’), if  d Пd’.
Let’s consider all the possible cases of d, d’ and (dПd’).
Case 1: if one element belongs to category D.
Let’s say d falls into category D. It doesn’t matter in which category d’ falls, if  d Пd’,
d Пd’ must also fall into category D. Otherwise, there will be contradiction.
Let’s assume that d Пd’ falls into category C, i.e. (  , a, b)  (d Пd’)
(d Пd’)  d & (1)  (  , a, b)  d
Contradiction!
(1)
So f(d Пd’)= 
f(d) П f(d’) =  П f(d’) =  ,
So f(dПd’) = f(d) П f(d’)
Case 2: if both d and d’ belong to Case A, B or C. There are two sub-cases, i.e. case2-a
and case2-b, as discussed below.
Case 2-a: if d and d’ both belong to the same sub-domain of (x,y,z), i.e. both in A, B or C.
In this case, if  d Пd’, obviously, d Пd’ also falls in the same category as d and d’. So
So f(d Пd’)= T = T П T =f(d) П f(d’).
Case 2-b: if d and d’ fall into two different categories out of A, B or C. Let say,
(a,b,  )d and (b,  ,a)  d’.
In this case, d and d’ will never be consistent elements since there is not a Z such that
dZ and d’Z (it is because that a and b are incomparable.)
In all, we conclude that function f is stable.
Comp 623 Concurrent Programming
Assignment 2
Name:
ID:
Yi Lin
110048614
Date:
Nov 13, 2002