2 CSP Overview Models distributed computing Idealistic send/receive primitives: send/receive onlyno shared variables (across outer processes)Idealistic send/receiveno bufferingsending a value from one process and the receiving of that value in another process appears externally as one event
3 CSP Synchronous Message Passing Send !ex: R ! (x*y + c)to process R send the value of expression x*y + csender names the receiving process Rwaits until receiver R is ready to receiveReceive ?ex: S ? vreceiver names the sender process Snames the receptacle va variable declared to be a compatible typewaits until sender S is ready to sendDeadlock is possible
4 G1 S1  G2 S2  ... Gn Sn Gi must not have sends Gi can include at the tail a receiveExample: val > 0; Y?P() QSuppose val = 0. The above input is not attempted.Suppose val > 0, but Y is not ready to send. Then, we are not committed to perform this input.Suppose val > 0, and Y is ready to send. Then, we perform this input, and execute Q.
5 Additional CSP Notation Comments:=assignmentC1; C2Sequential composition: C2 after C1C1 || C2Simult parallel; not C1; C2 or C2;C1C2 || C1Same as C1 || C2if G1 S1  G2 S2  ... Gn Sn fiIf statement; Hoare used [ …];  is fat bardo G1 S1  G2 S2  ... Gn Sn odLoop; Hoare used *[ … ]Multiple Gi can be true; non-determinismSkipDo-nothing (no-op) statement;Also used to end declarations
6 Small Set of IntegersDesign a server that can provide the abstraction of a Small Set of IntegersMake it as distributed as possibleMake it as symmetric as possibleSimplifying AssumptionsFinite, say 100 integersany/all integers can be elementsquery “have you got x” replied with Booleanrequest “insert x”, no repliesif duplicate, no problemif ran out of room, problem … but silently discard
7 Small Set of Integers: Design An array S of 102 processesS(i: )each process holds an integer or is empty-handedS(i) has an integer impliesS(i-1) also has an integerinteger of S(i-1) < integer of S(i)S(101) is a sinkS(0) is the “receptionist”
8 Small Set of IntegersS(i: ) :: do n: integer; S(i-1)?has(n) S(0)!false  n: integer; S(i-1)?insert(n) do m: integer; S(i-1)?has(m) if m <= n S(0)!(m = n)  m > n S(i+1)!has(m) fi  m: integer; S(i-1)?insert(m) if m < n S(i+1)!insert(n); n := m  m = n skip  m > n S(i+1)!insert(m) od od
9 Small Set of Integers: S(i) do n: integer; S(i-1)?has(n) S(0)!false  n: integer; S(i-1)?insert(n) do m: integer; S(i-1)?has(m) if m <= n S(0)!(m = n)  m > n S(i+1)!has(m) fi  m: integer; S(i-1)?insert(m) if m < n S(i+1)!isrt(n); n := m  m = n skip  m > n S(i+1)!insert(m) odEach S(i) starts out empty handedAny has(whatever) is replied with false.The first inserted value is saved in nAfter the first insert, the process spends its life in the second loop.Note: we have no breaks or exits.
10 Small Set of Integers: has(m) do n: integer; S(i-1)?has(n) S(0)!false  n: integer; S(i-1)?insert(n) do m: integer; S(i-1)?has(m) if m ≤ n S(0)!(m = n)  m > n S(i+1)!has(m) fi  m: integer; S(i-1)?insert(m) if m < n S(i+1)!isrt(n); n := m  m = n skip  m > n S(i+1)!insert(m) odprocesses are arranged in a “row”0 index at left, 101 at rightS(i-1)?has(m) (black color) impliesS(i) is not empty handedS(i-1) does not have mn is the number S(i) is holdingelements are sorted from l-to-rm = n: we have itm < n: no one else has m eitherreply to the receptionist S(0)
11 Small Set of Integers: insert(m) do n: integer; S(i-1)?has(n) S(0)!false  n: integer; S(i-1)?insert(n) do m: integer; S(i-1)?has(m) if m <= n S(0)!(m = n)  m > n S(i+1)!has(m) fi  m: integer; S(i-1)?insert(m) if m < n S(i+1)!isrt(n); n := m  m = n skip  m > n S(i+1)!insert(m) odno S(j) has m (j < i)Case m = nrequest to insert a duplicate elementdo nothingCase m > nto be inserted m is higherask S(i+1) to insert mCase m < nthe number n held by S(i) is higherask neighbor S(i+1) to hold nS(i) now holds m
12 Small Set of Integers: S(0) and S(101) S(101) is a sinkS(101) ::doS(100)?has(m) S(0)!false S(100)?insert(m) skipodS(0) is the “receptionist”S(0) ::Client ?has(n) S(1)!has(n); if (i: l.. 100) S(i)? b Client ! b fi Client ? insert(n) S(1)?insert(n)
13 Small Set of Integers: Questions Does “distributed” mean “concurrent” and/or “parallel”How do we delete?Can we redesign this into a lossless set of integers?
16 Sieve of Eratosthenes Print all primes less than 10000 Design 2, 3, 5, 7, 11, 13, …DesignUse an array SIEVE of processesSIEVE(i) filters multiples of i-th primesqrt(10000) processes are neededSIEVE(0) and SIEVE(101)
17 Sieve of Eratosthenes [SIEVE(i: 1..100):: SIEVE( i - 1)?p; print ! p; mp := p;do SIEVE(i - 1)? m do m > mp --> mp := mp + p od;if m = mp --> skip m < mp --> SIEVE(i + l)!mfiod|| SIEVE(0):: print!2; n := 3; do n < > SIEVE(1)!n; n := n + 2 od|| SIEVE(101):: do SIEVE(100)?n --> print ! n od|| print:: do (i: ) SIEVE(i)?n --> .“print-the-number” n od]
18 Shared Variables Duality: variables v. processes Provide a “shared variable” V as a process VUser processes do:V ! exp equiv of V := expV ? u equiv of u := V, u local to this processsemaphores, by intention, are shared variables.
19 Semaphores in CSP S:: val: integer; val := 0; /* or any +int */ do (i:I..100)X(i)?V() val := val + 1II (i:l..100) val > 0; X(i)?P() val := val - 1odWithin the loop: (unnamed) processesthese 200 processes share the integer valyet there is no mutex problem here; why?Array X of 100 client processes can doS!P() or S!V()
20 Buffered Message Passing CSP send/receive have no buffering == Synchronous Message PassingA Buffer process can be inserted between the (original) sender and (original) receiver.This gives a Semi Asynchronous Message Passing.Sends do not block (until the Buffer becomes full)
22 Fairness[ X:: Y!stop() ll Y:: c := true; do c; X?stop() c := false  c n := n + 1 od ]Will/Must this terminate?We (programmers) should not assume fairness in the implementation
23 Process Algebras mathematical theories of concurrency Events on, off, valve.open, valve.close, mouse?(x,y), screen!bitmapPrimitive processesSTOP (communicates nothing),SKIP (represents successful termination)Event Prefix: e PDeterministic ChoiceNondeterministic ChoiceInterleavingInterface ParallelHidinghas an entire book by Hoare
24 CSP Implementations Occam is based on CSP Machine Lang of Transputer CPU, 1990sAndrews book, Section 10.xCSP in Java == JCSP Software Release atCommunicating Process Architectures Conference == CPACSP in Jython and Python, CPA 2009Limbo, Newsqueak are prog langs from Bell Labs, included in the Inferno OS, 2004Concurrent Event-driven Programming in occam-π for the Arduino, CPA 2011Experiments in Multi-core and Distributed Parallel Processing using JCSP, CPA 2011LUNA: Hard Real-Time, Multi-Threaded, CSP-Capable Execution Framework, CPA 2011
25 CSP ReferencesC. A. R. Hoare, ``Communicating Sequential Processes,'' Communications of the ACM, 1978, Vol. 21, No. 8,has an entire book by Hoare.For now, do not read it (!!)Andrews, Chapter on Synchronous Message Passing.U of Kent, CSP for Java (JCSP),