networking for embedded systems - 國立中興大學 7.pdf · embedded systems author: qing li with...
TRANSCRIPT
![Page 1: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/1.jpg)
RealReal--Time Concepts for Time Concepts for Embedded SystemsEmbedded Systems
Author: Qing Li with Caroline Yao
ISBN: 1-57820-124-1CMPBooks
![Page 2: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/2.jpg)
Chapter 7Chapter 7Message QueuesMessage Queues
![Page 3: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/3.jpg)
OutlineOutline7.1 Introduction7.2 Defining Message Queues7.3 Message Queue States7.4 Message Queue Content 7.5 Message Queue Storage7.6 Typical Message Queue Operations 7.7 Typical Message Queue Use
![Page 4: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/4.jpg)
7.1 Introduction7.1 IntroductionTo facilitate inter-task data communication, kernels provide
a message queue object and message queue management services
![Page 5: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/5.jpg)
7.2 Defining Semaphores7.2 Defining SemaphoresA message queue
a buffer-like object through which tasks and ISRs send and receive messages to communicate and synchronize with data
When a message queue is first created, it is assigneda queue control block (QCB)a message queue namea unique IDmemory buffersa queue lengtha maximum message lengthtask-waiting lists
![Page 6: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/6.jpg)
A message queue, its associated parameters, A message queue, its associated parameters, and supporting data structures and supporting data structures
![Page 7: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/7.jpg)
Message QueueMessage QueueThe message queue itself consists of a number of elements, each of which can hold a single message.Kernel takes developer-supplied parameters to determine how much memory is required for the message queue:
queue length and maximum message length
![Page 8: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/8.jpg)
7.3 Message Queue States7.3 Message Queue StatesThe state diagram for a message queue:
![Page 9: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/9.jpg)
Message Queue States (Cont.)Message Queue States (Cont.)When a task attempts to send a message to a full message queue, two ways of kernel implementation:
the sending function returns an error code to that taskSending task is blocked and is moved into sending task-waiting list
![Page 10: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/10.jpg)
7.4 7.4 Message Queue Content Message Queue Content Message queues can be used to send and receive a variety of data. Some examples:
a temperature value from a sensora bitmap to draw on a displaya text message to print to an LCDa keyboard eventa data packet to send over the network
![Page 11: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/11.jpg)
Message Queue Content (Cont.)Message Queue Content (Cont.)When a task sends a message to another task, the message normally is copied twice
from sender’s memory area to the message queue’s memory areafrom the message queue’s memory area to receiver’s memory area
Copying data can be expensive in terms of performance and memory requirements
![Page 12: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/12.jpg)
Message copying and memory use Message copying and memory use for sending and receiving messages for sending and receiving messages
![Page 13: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/13.jpg)
Message Queue Content (Cont.)Message Queue Content (Cont.)Keep copying to a minimum in a real-time embedded system:
by keeping messages small by using a pointer instead
Send a pointer to the data, rather than the data itself
overcome the limit on message lengthimprove both performance and memory utilization
![Page 14: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/14.jpg)
7.5 7.5 Message Queue Storage Message Queue Storage Message queues may be stored in a system pool or private buffersSystem Pools
the messages of all queues are stored in one large shared area of memoryAdvantage: save on memory useDownside: a message queue with large messages can easily use most of the pooled memory
![Page 15: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/15.jpg)
Message Queue Storage (Cont.)Message Queue Storage (Cont.)Private Buffers
separate memory areas for each message queueDownside: uses up more memory
requires enough reserved memory area for the full capacity of every message queue that will be created
Advantage: better reliability ensures that messages do not get overwritten and that room is available for all messages
![Page 16: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/16.jpg)
7.6 Typical Message Queue 7.6 Typical Message Queue Operations Operations
creating and deleting message queues sending and receiving messagesobtaining message queue information
![Page 17: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/17.jpg)
7.6.1 Creating and Deleting Message 7.6.1 Creating and Deleting Message QueuesQueues
When created, message queues are treated as global objects and are not owned by any particular task.When creating a message queue, a developer needs to decide
message queue length the maximum messages size the blocked tasks waiting order
![Page 18: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/18.jpg)
Message queue creation and deletion Message queue creation and deletion operationsoperations
Operation Description
Create Creates a message queue
Delete Deletes a message queue
![Page 19: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/19.jpg)
7.6.2 7.6.2 Sending and Receiving Sending and Receiving MessagesMessages
Operation Description
Send Sends a message to a message queue
Receive Receives a message from a message queue
Broadcast Broadcasts messages
![Page 20: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/20.jpg)
Sending messages in FIFO or LIFO Sending messages in FIFO or LIFO orderorder
![Page 21: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/21.jpg)
Sending MessagesSending MessagesTasks can send messages with different blocking policies:
not block (ISRs and tasks)If a message queue is already full, the send call returns with an error, the sender does not block
block with a timeout (tasks only)block forever (tasks only)
The blocked task is placed in the message queue’s task-waiting list
FIFO or priority-based order
![Page 22: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/22.jpg)
FIFO and priorityFIFO and priority--based taskbased task--waiting lists waiting lists
![Page 23: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/23.jpg)
Receiving MessagesReceiving MessagesTasks can receive messages with different blocking policies:
not blockingblocking with a timeoutblocking forever
Due to the empty message queue, the blocked task is placed in the message queue’s task-waiting list
FIFO or priority-based order
![Page 24: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/24.jpg)
FIFO and priorityFIFO and priority--based taskbased task--waiting lists waiting lists
![Page 25: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/25.jpg)
Receiving Messages (Cont.)Receiving Messages (Cont.)Messages can be read from the head of a message queue in two different ways:
destructive readremoves the message from the message queue’s storage buffer after successfully read
non-destructive readwithout removing the message
![Page 26: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/26.jpg)
7.6.3 7.6.3 Obtaining Message Queue Obtaining Message Queue Information Information
Obtain information about a message queue:message queue ID, task-waiting list queuing order (FIFO or priority-based), and the number of messages queued.
Operation Description Show queue info Gets information on a message queueShow queue’s task-waiting list
Gets a list of tasks in the queue’s task-waiting list
![Page 27: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/27.jpg)
7.7 Typical Message Queue Use7.7 Typical Message Queue UseTypical ways to use message queues within an application:
non-interlocked, one-way data communicationinterlocked, one-way data communicationinterlocked, two-way data communicationbroadcast communication
![Page 28: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/28.jpg)
7.7.1 Non7.7.1 Non--Interlocked, OneInterlocked, One--Way Way Data Communication Data Communication
Non-interlocked (or loosely coupled), one-way data communication:
The activities of tSourceTask and tSinkTask are not synchronized.
TSourceTask simply sends a message and does not require acknowledgement from tSinkTask.
![Page 29: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/29.jpg)
NonNon--Interlocked, OneInterlocked, One--Way Data Way Data CommunicationCommunication
tSourceTask (){
:Send message to message queue:
}
tSinkTask (){
:Receive message from message queue:
}
![Page 30: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/30.jpg)
NonNon--Interlocked, OneInterlocked, One--Way Data Way Data Communication (Cont.)Communication (Cont.)
ISRs typically use non-interlocked, one-way communication.
A task such as tSinkTask runs and waits on the message queue. When the hardware triggers an ISR to run, the ISR puts one or more messages into the message queue for tSinkTask.
ISRs send messages to the message queue in a non-blocking way.
If the message queue becomes full, any additional messages that the ISR sends to the message queue are lost.
![Page 31: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/31.jpg)
7.7.2 Interlocked, One7.7.2 Interlocked, One--Way Data Way Data Communication Communication
Interlocked communicationthe sending task sends a message and waits to see if the message is receiveduseful for reliable communications or task synchronization
Solutiona binary semaphore initially set to 0 and a message queue with a length of 1 (also called a mailbox) Sender tSourceTask and receiver tSinkTask operate in lockstep with each other
![Page 32: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/32.jpg)
Interlocked, OneInterlocked, One--Way Data Way Data CommunicationCommunication
tSourceTask (){
:Send message to message queueAcquire binary semaphore :
}
tSinkTask (){
:Receive message from message queueGive binary semaphore :
}
![Page 33: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/33.jpg)
7.7.3 Interlocked, Two7.7.3 Interlocked, Two--Way Data Way Data Communication Communication
Interlocked, two-way data communication (also called full-duplex or tightly coupled communication)
data flow bidirectionally between tasksuseful when designing a client/server-based systemtwo separate message queues are required
If multiple clients need to be set upall clients can use the client message queue to post requeststServerTask uses a separate message queue to fulfill the different clients’ requests
![Page 34: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/34.jpg)
Interlocked, TwoInterlocked, Two--Way Data Way Data Communication Communication
tClientTask (){
:Send a message to the requests queueWait for message from the server queue:
}
tServerTask (){
:Receive a message from the requests queueSend a message to the client queue:
}
![Page 35: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/35.jpg)
7.7.4 7.7.4 Broadcast Communication Broadcast Communication Allow developers to broadcast a copy of the same message to multiple tasksMessage broadcasting is a one-to-many-task relationship.
tBroadcastTask sends the message on which multiple tSink-Task are waiting.
![Page 36: Networking for Embedded Systems - 國立中興大學 7.pdf · Embedded Systems Author: Qing Li with Caroline Yao ISBN: 1-57820-124-1 CMPBooks. Chapter 7 Message Queues. Outline](https://reader034.vdocuments.site/reader034/viewer/2022042810/5f9e57f3cb40650a1c20b7e3/html5/thumbnails/36.jpg)
tBroadcastTask (){
:Send broadcast message to queue:
}
Note: similar code for tSignalTasks 1, 2, and 3.
tSignalTask (){
:Receive message on queue:
}
Broadcasting messages Broadcasting messages