1 / 28

Communicating Sequential Processes

Communicating Sequential Processes. This Lecture is based on Peter Clayton’s Lectures on CSP Greg Allard Thomas Welfley. Communicating Sequential Processes. CSP is a formal language for describing concurrent systems. It was introduced by C. A. R. Hoare in 1978

cato
Download Presentation

Communicating Sequential Processes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Communicating Sequential Processes This Lecture is based on Peter Clayton’s Lectures on CSP Greg AllardThomas Welfley Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  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 Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  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 P1 P2 P3 Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  4. CSP Primitives • Assignment primitives: • <variable> := <expression> • Ex: x:=e {variable x takes on the value of expression e} • Output primitive • <destination process> ! <expression> • Ex: A ! e {output the value of expression e to process A} Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  5. CSP Primitives • Input primitive • <source process> ? <variable> • Ex: B ? x {From process B, input to var x} B A B?x A!e Equivalent to A B X := e Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  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. Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  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. Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  8. An Example [ A :: x:integer; user?x; C!x*x || B :: y: integer; user?y; C!y*y || C :: x,y:integer; [A?x || B? y]; user!x+y] • This can be specified as follows: A Square input User Sum inputs B User Square input User Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  9. Here is the same example in a format that looks more like a program: 001 [002 A ::003 x:integer;004 user?x;005 C!x*x 006 || 007 B :: 008 y: integer; 009 user?y; 010 C!y*y 011 || 012 C :: 013 x,y:integer;014 [A?x || B? y];015 user!x+y016 ] An Example Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  10. Iteration • If you want something to iterate, simply add an asterisk in front:*[x:integer; user?x; c!x*x] • To make the three process example loop forever, we do the following:*[ A :: x:integer; user?x; C!x*x || B :: y: integer; user?y; C!y*y || C :: x,y:integer; [A?x || B? y]; user!x+y] Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  11. Dijkstra’s Guarded Commands • CSP is partially based on a programming construct proposed by Dijkstra to indicate the concurrent execution of processes and non-determinism. Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  12. Dijkstra’s Guarded Commands • <guard> -> <commands> • 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) Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  13. Guarded Commands Example • Define a process that repeatedly accepts input from the user, squares it and sends it to process C • Without guards:*[x:integer; user?x; C!x*x] • With guards:*[x:integer; user?x ->C ! x*x] • User ? X is the guard • C ! x*x is the guarded command Repeatedly square input User C Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  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. Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  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. *[<guard1> -> <guarded commands1> [] <guard2> -> <guarded commands2> [] <guard3> -> <guarded commands3> . . . [] <guardn> -> <guarded commandsn> ] [] = vertical rectangle thing? Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  16. Alternative Commands Example • A process that takes input from different users, squares it, and sends it to process C*[ x:integer; user?x -> C!x*x [] x:integer; user?x -> C!x*x ] Repeatedly square one of the inputs User C User Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  17. Conditional Commands (IF) • Same syntax as alternative commands, except there is no iterator (*) • Example:[ x>=y -> m:=x[] y>=x -> m:=y] Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  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 • P1’s x value is now 6 • P2 Waits for input from P1 • P1 Sends P2 2 * x (12) • P2’s Y value is now 12 P2 P2?x y:=5 x:=y+1 x is now 6 P1!y+1 y:=2x P1?y P2!2*x y is now 12 Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  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. Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  20. Review: Producer / Consumer with semaphores P1 P(S) CS V(S) P2 P(S) CS V(S) S v P(S) V(S) Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  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. Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  22. Producer Buf ! P The producer simply needs to produce a value and send it to the buffer when it is ready. CSP: Producer Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  23. 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 consume:Buf ! more(); The consumer then waits for input and eventually the buffer fulfills the request. CSP: Consumer Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  24. Buf::buffer:(0…9) In, out :: integer In = 0; out = 0; *[ in<out+10; Producer n ? Buffer(in mod 10) -> in:=in+1; [] out<in; consumer ? more() -> { consumer ! buffer(out mod 10) out:=out +1; } ] CSP: Shared Buffer Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  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. Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  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 buffer:in<out+10; Producer n ? Buffer(in mod 10) Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  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 message:out<in; 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. Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

  28. CSP Solution for Producer / Consumer Diagram Producer Consumer Buf Buf ! P … Pro n ? Buffer(…) … con ? more() … con ! buffer(…) … Buf ! more(); Buf ? P; Communicating Sequential Processes (CSP) COP 4020 - Summer 2006

More Related