CS452 - Real-Time Programming - Spring 2012
Lecture 9 - Send/Receive/Reply
Pubilc Service Annoucements
- Due date for assignment 1
- Next week's classes
- 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 *message, int mslen )
- 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 *reply, int rplen )
- 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
+--------------+ +--------------+
| | | |
| Producer | ===> | Consumer |
| | | |
+--------------+ +--------------+
Producer Sends
- 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
- 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( ..., accept some XXX, ... )
- Consumer: I want some YYY
Send( ..., want some YYY, ... )
Only a receiver can accept two types of requests at once.
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
Send
...
Receive
...
Reply
... ...
Message copying occurs inside Receive and Reply.
Receive needs to have a list of current senders, the ReceiveQ
Receive before Send
Receive
...
Send
...
Reply
... ...
Message copying occurs inside Send and Reply.
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: