CS452 - Real-Time Programming - Winter 2013
Lecture 9 - Send/Receive/Reply
Pubilc Service Annoucements
- Due date for assignment 2
Inter-task Communication
Overview
Message passing combines synchronization and communication in one set of
primitives.
int Send( Tid tid, char *message, int mslen, char *reply, int rplen )
- blocks until Reply occurs
int Receive( Tid *tid, char *msg, int msglen )
- blocks until message is available
- Only one waiting sender is processed per Receive
- Why?
- Hint. There might be tasks that are higher priority than the
Receiver.
int Reply( Tid tid, char *rply, int rplylen )
- does not block
- unblocks task that called Send
Send and Reply become READY at the same time.
How are They Used?
The Producer/Consumer Problem
Middle (double) arrow shows the direction that information flows.
+--------------+ +--------------+
| | ---> | |
| Producer | ===> | Consumer |
| | <--- | |
+--------------+ +--------------+
Producer Sends
Upper arrow
- producer sends and blocks (I have some XXX for you)
- consumer receives (Give me some XXX)
- consumer accepts XXX
- consumer replies (I got the XXX)
- producer & consumer are simultaneously READY
Note. 1 & 2 could be run in the opposite order
Consumer Sends
Lower arrow
- Consumer sends and blocks (I am ready for some XXX.)
- Producer receives (I have some XXX.)
- Producer replies (Here is the XXX)
- Consumer accepts XXX
- Producer and consumer are simultaneously READY
Note. 1 & 2 could be run in the opposite order
Multiple Producers
Producers send; consumer receives in the order that producers send.
Notes.
- Critical races can occur, which the application programmer must
resolve.
- There are two types of critical race
- Ones internal to the application. For example, order of production
changes in one producer because you add or remove a printf statement.
These ones you can program out of existence by changing priorities,
communication patterns, etc.
- Ones external to the application. For example, one producer is
forwarding bytes from the keyboard, the other from the train
controller, and the order of production changes because the user
types a little faster. These ones you cannot program out of
existence, but must program so that the right thing happens
regardless of the order of production.
Multiple Consumers
Consumers send; producer receives in the order that consumers send.
Note. Critical races can occur, which the application programmer must
resolve.
Multiple Consumers AND Multiple Producers
Consumers send and producers send: who receives?
- A third task: you call it a FIFO or buffer; I call it a warehouse
+---------------+ +------------+ +---------------+
| | ---> | | <--- | |
| Producers | ===> | Buffer | ===> | Consumers |
| | | | | |
+---------------+ +------------+ +---------------+
Buffer receives two types of request
- Producer: Here is some XXX
Send( warehouse, accept some XXX, ... )
- Warehouse receives
Receive( *producer, XXX )
- Warehouse stores XXX, replies
insert( XXX, shelf );
Reply( producer, got XXX, )
- If warehouse is full of XXX two strategies are possible
- Warehouse queues producer, who remains Reply_Blocked until
there is space created in the warehouse
- Warehouse replies with refusal
Reply( producer, can't take XXX, )
The second strategy is considered to be not so good. Why?
- Consumer: I want some YYY
Send( warehouse, want some YYY, ... )
- Warehouse receives, gets YYY
Receive( &consumer, YYY )
- Warehouse replies, providing YYY
extract( YYY, shelf);
Reply( consumer, YYY )
- If warehouse is empty of YYY, two strategies are possible
- Warehouse queues sender, who remains Reply_Blocked
- Warehouse replies with refusal
Reply( consumer, all out of YYY, )
Notes.
- Only a receiver can accept two types of requests at once. The buffer,
which only receives is the simplest example of a server.
- Messages in Send/Receive/Reply must be the same 'type'.
Sequence of States
Sender
- Active -> Receive_Blocked
- Receive_Blocked -> Reply_Blocked
- May happen right away
- When Receive is called with the Receiver's SendQ empty
- Otherwise, when Receive is called
- Reply_Blocked -> Ready
Receiver
- Active -> Send_Blocked
- Send_Blocked -> Ready
- May happen right away
- if the sendQ is not empty
- Ready -> Active
- ...
- Active -> Ready
There are two cases
Send before Receive
| Sender
Action
|
Sender
State
|
Receiver
Action
|
Receiver
State
|
Comments |
|
active |
|
|
|
| Send |
RCV_BL |
|
|
sender added to receiver's sendQ |
|
|
|
active |
|
|
RPL_BL |
Receive |
ready |
request copied
sender deleted from receiver's sendQ
|
|
|
|
active |
service performed |
|
ready |
Reply |
ready |
reply copied |
Receive before Send
| Sender
Action
|
Sender
State
|
Receiver
Action
|
Receiver
State
|
Comments |
|
|
|
active |
|
|
|
Receive |
SND_BL |
receiver's sendQ empty |
|
active |
|
|
|
| Send |
RPL_BL |
|
ready |
request copied |
|
|
|
active |
service perfomed |
|
ready |
Reply |
ready |
reply copied |
Practical Details
- Need to keep around request
- For Send_Blocked receivers in the SendQ
- The same as Receive_Blocked senders
- For Reply_Blocked senders.
- Messages
- Task states
- You can add extra return values beyond those specified
Example of a Difficult Bug
- You notice that a Receiver is never on a readyQ when it is
Send_Blocked
- You decide to use the next pointer in the TD for the head of the sendQ
- probably to fit two TDs into a single cache line
- You test and test and test and nothing ever goes wrong
- One week before the demo, your kernel crashes under your
application
What two things might have gone wrong?
You might have caught one while testing, not likely the other.
Return to: