CS 563 Concurrent Programming
Lecture 4: Message Passing (1)
Message Passing
Copyright By PowCoder代写 加微信 powcoder
Abstract notions representing physical communication possibilities Many different names: message queues, pipes, mailboxes, … Primitives:
send msg to dest
receive msg [from source]
Unbounded queue of messages
chan name(id1: type1; …; idN: typeN)
Primitives
send name(expr1, …, exprN)
Types and number of fields must match Effect:
Evaluate the expressions and produce a message M Atomically append M to the end of the named channel
➡ send is nonblocking (asynchronous)
Primitives
receive name(var1, …, varN)
Again, types and number of fields must match
Wait for a message on the named channel
Atomically remove first message and put the fields of the message into the variables
➡ receive is blocking (synchronous)
chan ch(int)
process A:
send ch(1)
send ch(2)
process B:
receive ch(x)
receive ch(y)
‣ x will contain 1 and y will contain 2
‣ Order of messages from SAME source is the order of the
chan ch1(int), ch2(int)
process A:
send ch1(1)
send ch2(2)
process C:
send ch1(3)
send ch2(4)
What is received now?
process B:
receive ch1(x)
receive ch1(y)
process D:
receive ch2(u)
receive ch2(v)
•x will get 1 or 3 and y will get 3 or 1 •u will get 2 or 4 and v will get 4 or 2
Implementation Sketch
With shared memory
send receive
With distributed memory
kernel space
user space
Process Interaction Patterns
Filters: one way
Client/Server: two way as master/slave Interacting peers: two way as peers
Filter Process to Assemble Characters
Merge Process and Sorting Network
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com