Title: Communicating Sequential Processes (CSP)
1Communicating Sequential Processes
- This Lecture is based on Peter Claytons Lectures
on CSP - Greg AllardThomas Welfley
2Communicating Sequential Processes
- CSP is a formal language for describing
concurrent systems. - It was introduced by C. A. R. Hoare in 1978
- An implementation of CSP (OCAM) was used in the
T9000 Transputer
3CSP The main idea
- Something similar to input and output can be used
to allow processes to communicate - Multiple communicating processes can be present
in both a single machine and across multiple
machines
P2
P1
P3
4CSP Primitives
- Assignment primitives
- ltvariablegt ltexpressiongt
- Ex xe variable x takes on the value of
expression e - Output primitive
- ltdestination processgt ! ltexpressiongt
- Ex A ! e output the value of expression e to
process A
5CSP Primitives
- Input primitive
- ltsource processgt ? ltvariablegt
- Ex B ? x From process B, input to var x
B
A
B?x
A!e
Equivalent to
A
B
X e
6Process Interaction
- Processes interact via synchronous I/O
- When a process gets to a send, it has to wait
until the receiving process is ready to receive - When a process gets to a receive, it has to wait
until the sending process sends - Processes have to rendezvous at a point, or else
process is blocked. - Processes have to be named explicitly.
7An Example
- A B is CSP syntax that represents concurrent
execution of a process called A and a process
called B - Consider three processes process A, process B,
process C. - Process A and process B accept an integer value
from the user, square that input, and then send
it along to process C. - Process C sums the values received from A and B,
and returns it to the user.
8An Example
A xinteger user?x C!xx B y
integer user?y C!yy C x,yinteger A?x
B? y user!xy
- This can be specified as follows
A
Square input
User
Sum inputs
B
User
Square input
User
9An Example
- Here is the same example in a format that looks
more like a program
- 001 002 A 003 xinteger004 user?x005
C!xx 006 007 B 008 y integer
009 user?y 010 C!yy 011 012 C
013 x,yinteger014 A?x B?
y015 user!xy016
10Iteration
- If you want something to iterate, simply add an
asterisk in frontxinteger user?x c!xx - To make the three process example loop forever,
we do the following A xinteger user?x
C!xx B y integer user?y C!yy
C x,yinteger A?x B? y user!xy
11Dijkstras Guarded Commands
- CSP is partially based on a programming construct
proposed by Dijkstra to indicate the concurrent
execution of processes and non-determinism.
12Dijkstras Guarded Commands
- ltguardgt -gt ltcommandsgt
- The guard may be a Boolean expression or it may
be a result of I/O. - A Boolean guard may have a value of true or
false. - An I/O value results in output being received (or
not) and input succeeding (or not) - The guard is evaluated for success or fail
- Commands is just a list of commands to be
executed (if the guard is true)
13Guarded Commands Example
- Define a process that repeatedly accepts input
from the user, squares it and sends it to process
C - Without guardsxinteger user?x C!xx
- With guardsxinteger user?x -gtC ! xx
- User ? X is the guard
- C ! xx is the guarded command
Repeatedly square input
User
C
14Guarded Outcomes
- The guard succeeds because the Boolean expression
is true, and (if the guard includes I/O) the I/O
does not block. - The guard fails (the Boolean is false)
- The guard is neither true or false because the
Boolean is true and the I/O of the guard does
block.
15Specifying Alternative Commands
- Cases
- If all guards fail, the result is an error.
- If one guard succeeds, it executes its command
(or command list). - If more than 1 guard succeeds, one of the
commands (whose guard was true) is
non-deterministically chosen and executed - If none succeed, but not all fail, wait.
- ltguard1gt -gt ltguarded commands1gt
- ltguard2gt -gt ltguarded commands2gt
- ltguard3gt -gt ltguarded commands3gt
- .
- .
- .
- ltguardngt -gt ltguarded commandsngt
-
- vertical rectangle thing?
16Alternative Commands Example
- A process that takes input from different users,
squares it, and sends it to process C
xinteger user?x -gt C!xx xinteger user?x
-gt C!xx
Repeatedly square one of the inputs
User
C
User
17Conditional Commands (IF)
- Same syntax as alternative commands, except there
is no iterator () - Example xgty -gt mx ygtx -gt my
18A CSP Example
P1
- Two processes, P1, P2
- P1 waits for input from P2
- P2 sets y 5
- P2 Sends y 1 (6) to P1
- P1s x value is now 6
- P2 Waits for input from P1
- P1 Sends P2 2 x (12)
- P2s Y value is now 12
P2
P2?x
y5
xy1
x is now 6
P1!y1
y2x
P1?y
P2!2x
y is now 12
19Producer / Consumer
- We can use CSP to solve the producer / consumer
problem. - Review In the producer consumer problem, we have
a buffer shared between a producer and a
consumer. - The producer adds values to the buffer, the
consumer removes values from the buffer. - If the buffer is full, the producer must wait
until the consumer consumes a value. - Similarly, if the buffer is empty, the consumer
must wait until the producer produces a value.
20Review Producer / Consumer with semaphores
P1 P(S) CS V(S)
P2 P(S) CS V(S)
S
v
P(S) V(S)
21CSP Solution for Producer / Consumer
- We need three processes. One process represents
the producer, one represents the consumer, and
the last represents the buffer.
22CSP Producer
- The producer simply needs to produce a value and
send it to the buffer when it is ready.
23CSP Consumer
- Consumer
- /
- Tell buffer to send more!
- /
- Buf ! more()// Receive!
- Buf ? P
- The consumer is a little more complicated than
the producer. - The consumer cannot simply wait around for the
buffer to send it a value because the buffer
would have know way of knowing whether or not the
consumer was ready to receive one. - Instead, the consumer notifies the buffer that it
is waiting to consumeBuf ! more() - The consumer then waits for input and eventually
the buffer fulfills the request.
24CSP Shared Buffer
- Bufbuffer(09)
- In, out integer
- In 0 out 0
-
- inltout10 Producer n ? Buffer(in mod 10) -gt
- inin1
- outltin consumer ? more() -gt
- consumer ! buffer(out mod 10) outout 1
-
25CSP Shared Buffer
- The buffer is where the magic happens.
- Key concepts
- The buffer uses non-determinism to choose between
accepting input or sending output when both
options are equally valid - When waiting for input in a guard, the process
does not block! The guard simply evaluates as
neither true nor false until it receives the
input (it will then evaluate as true) - The buffer has a size of 10 (0 through 9 spaces)
- Utilizes 2 counters One for recording number of
items it has received, one for recording number
of items it has sent.
26CSP Shared buffer
- Both counters initialize to 0
- Two guarded blocks
- The first deals with receiving input from the
producer. - This is allowed to happen, when the producer has
sent something and there is space available in
the bufferinltout10 Producer n ? Buffer(in mod
10)
27CSP Shared Buffer
- The second deals with sending output to the
consumer. - This is allowed to happen when the buffer is not
empty and the consumer has sent the more
messageoutltin consumer ? more() - If both guards evaluate to true, then the buffer
non-deterministically chooses one to evaluate
because they are both valid choices. - If they both resolve to neither true nor false,
the buffer waits until one or both evaluate to
true.
28CSP Solution for Producer / Consumer Diagram
Producer
Consumer
Buf
Buf ! P
Buf ! more() Buf ? P
Pro n ? Buffer() con ? more() con !
buffer()