Help Students To Solve Project 2: Message Passing
Help Students To Solve Project 2: Message Passing
Implement a
message system. Write the procedures for sending and receiving messages.
Message Passing
Messages can be
- sent to specific destinations
- sent on `channels'
- sent one at a time (as in CSP's rendezvous protocol)
- queued at source, dest, or in-between (e.g., email)
- point to point (one sender and one receiver)
- broadcast (sent to multiple destinations)
- acknowledged or unacknowledged
Destinations can be
- internet addresses
- process id's
- any process doing a receive()
Channels can be
- static (like programming language variables)
- dynamic (ie., they need an `open' operation)
- 1-way or 2-way
Important Example
Assumptions:
- Message are buffered
- the message buffer acts as the queue
- buffer size is N
Producer:
while (alive) {
generate an item V;
receive(consumer); // get `empty' from the consumer
send(consumer, V); // send data to the consumer
}
Consumer:
CSP processes do not share memory space, and can run on either
one or several processors
P1 P2
: :
C!x C?y
: :
If P1 does C ! x first
- P1 waits for C ? y
- both P1 and P2 continue
- y contains the value from x
If P2 does C ? y first
- P2 waits for C ! x,
- both P1 and P2 continue
- y contains the value from x
ALT
C1 ? x # wait for something on C1
C3 ! x # do this if we get something on C1
C2 ? x # wait for something on C2
C3 ! x # do this if we get something on C2
This example acts as a `merge', taking input from either C1 or
C2 at any time, and sending the value received out on C3
C!x C?y
L = x; wait(S1)
signal(S1) y = L;
wait(S2) signal(S2)
Producer:
WHILE true DO BEGIN
generate x
pchan ! x // send x to the buffer
END
Consumer:
WHILE true DO BEGIN
cchan ? y // receive y from the buffer
use y
END
Buffer:
INTEGER buf[N], in=0, out=0;
WHILE true DO
ALT
// test buffer not full and sender ready:
(in < out + N) & pchan ? buf[in mod N]
in := in + 1
Note that since semaphores are usually used with shared memory, this
example would not be not too useful for a true distributed system.
Further, in any shared memory system, there are probably easier ways
to implement semaphores.
Assumptions:
- messages are queued
- message are sent to process id's
signal() {
msg[] = [SIG, pid];
send(synch, msg);
}
wait() {
msg[] = [WAIT, pid];
send(synch, msg);
receive();
}
synch process:
while (alive) {
msg = receive();
fnx = msg[1];
pid = msg[2];
if (fnx == SIG)
s = s + 1;
if (s <= 0) {
get p from list L;
send(p, dummy);
}
}
else if (fnx == WAIT) {
s = s - 1;
if (s < 0)
add pid to L;
else
send(pid);
}
}
Q: do the synch process or signal and wait procedures
need mutual exclusion?
A: No, since the synch process only receives and processes one
message at a time.