Communicating Sequential Processes (CSP) - PowerPoint PPT Presentation

About This Presentation
Title:

Communicating Sequential Processes (CSP)

Description:

Communicating Sequential Processes This Lecture is based on Peter Clayton s Lectures on CSP Greg Allard Thomas Welfley Communicating Sequential Processes CSP is a ... – PowerPoint PPT presentation

Number of Views:92
Avg rating:3.0/5.0
Slides: 29
Provided by: ThomasW97
Learn more at: http://www.cs.ucf.edu
Category:

less

Transcript and Presenter's Notes

Title: Communicating Sequential Processes (CSP)


1
Communicating Sequential Processes
  • This Lecture is based on Peter Claytons Lectures
    on CSP
  • Greg AllardThomas Welfley

2
Communicating 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

3
CSP 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
4
CSP 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

5
CSP 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
6
Process 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.

7
An 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.

8
An 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
9
An 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

10
Iteration
  • 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

11
Dijkstras Guarded Commands
  • CSP is partially based on a programming construct
    proposed by Dijkstra to indicate the concurrent
    execution of processes and non-determinism.

12
Dijkstras 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)

13
Guarded 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
14
Guarded 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.

15
Specifying 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?

16
Alternative 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
17
Conditional Commands (IF)
  • Same syntax as alternative commands, except there
    is no iterator ()
  • Example xgty -gt mx ygtx -gt my

18
A 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
19
Producer / 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.

20
Review Producer / Consumer with semaphores
P1 P(S) CS V(S)
P2 P(S) CS V(S)
S
v
P(S) V(S)
21
CSP Solution for Producer / Consumer
  • We need three processes. One process represents
    the producer, one represents the consumer, and
    the last represents the buffer.

22
CSP Producer
  • Producer
  • Buf ! P
  • The producer simply needs to produce a value and
    send it to the buffer when it is ready.

23
CSP 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.

24
CSP 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

25
CSP 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.

26
CSP 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)

27
CSP 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.

28
CSP Solution for Producer / Consumer Diagram
Producer
Consumer
Buf
Buf ! P
Buf ! more() Buf ? P
Pro n ? Buffer() con ? more() con !
buffer()
Write a Comment
User Comments (0)
About PowerShow.com