Message-based Inter-task...

36
Based on G.C. Buttazzo, «Sistemi in Tempo Reale», Capitolo 10 Message-based Inter-task communication Embedded Real-Time Systems Prof. Davide Brugali Università degli Studi di Bergamo

Transcript of Message-based Inter-task...

Page 1: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Based on G.C. Buttazzo,

«Sistemi in Tempo Reale», Capitolo 10

Message-based

Inter-task communication

Embedded Real-Time Systems

Prof. Davide Brugali

Università degli Studi di Bergamo

Page 2: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Global shared variables

In most RT applications, tasks exchange data through global

shared variables.

Advantages

this is the fastest mechanism

low run-time overhead

schedulability analysis is available

Disadvantages Data must be accessed in mutual exclusion:

non-preemptive regions;

semaphores, priority inheritance, priority ceiling.

Not good for modular design:

local details are exposed to other tasks;

a change in a task affect other tasks.

Page 3: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Message passing paradigm

Another approach is to exchange data through a message passing

paradigm:

Every task operates on a private memory space;

Data are exchanged by messages through a channel.

Page 4: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Communication Ports

Many operating systems provides the channel abstraction through the port construct.

A task can use a port to exchange messages by means of two primitives:

send sends a message to a port

receive receives a message from a port

Some operating systems allows the user to define different types of ports, with peculiar semantics

Page 5: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Port characteristics

Ports may differ for:

number of tasks allowed to send messages;

number of tasks allowed to receive messages;

policy used to insert and extract messages.

behavior to manage exceptions (sending to a full port or receiving

from an empty port).

Before being used, a port has to be created, and then destroyed when

it is not needed any more.

Port attributes need to be defined at creation time

Page 6: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Sending a message

Page 7: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Receiving a message

Page 8: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Using a port

Page 9: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Periodic task communication

Page 10: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Periodic task communication

Page 11: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Periodic task communication

Page 12: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Exchanged messages

Page 13: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Buffer saturation

Page 14: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

STICK Ports

Page 15: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

STICK Ports

Page 16: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Blocking on STICK ports

Although a task cannot blocks for a full or empty buffer, it can block

for mutual exclusion:

indeed a semaphore is needed to protect the internal buffer from

simultaneous accesses.

Long messages may cause long blocking delays on such a semaphore.

Long waiting times due to long messages can be avoided through a

buffer replication mechanism.

Page 17: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Cyclic Asynchronous Buffer (CAB)

It is a mechanism for exchanging messages among periodic tasks with

different rates.

Memory conflicts are avoided by replicating the internal buffers.

At any time, the most recent message is always available for reading.

A new message overrides the previous one.

Messages are not consumed by reading.

Page 18: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Simultaneous accesses

Page 19: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Dual buffering

Dual buffering is often used to transfer large data, (images) from the

input peripheral device to a task:

Once written, the new message becomes available to the next

reader:

Page 20: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Accessing a CAB

A CAB generalizes dual buffering for N readers;

Data is accessed through a memory pointer.

Hence, a reader is not forced to copy the message in its memory

space.

More tasks can concurrently read the same message.

At any time, a pointer (mrb) points to the most recent buffer used

for writing a new message.

Page 21: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

CAB example

Page 22: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

CAB example

Page 23: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

CAB example

Page 24: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Dimensioning a CAB

If a CAB is used by N tasks, to avoid blocking, it must have at least N

+1 buffers.

The (N+1)-th buffer is needed for keeping the most recent message

in the case all the other buffers are used.

Page 25: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Inconsistency with N buffers

Page 26: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Writing Protocol

To write a message in a CAB a task must:

1. ask the CAB for a pointer to a free buffer;

2. copy the message into the buffer using the pointer;

3. release the pointer to the CAB to make the message

accessible to the next reader.

Page 27: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Reading Protocol

To read a message from a CAB a task must

1. get the pointer to the most recent message in the

CAB;

2. process the message through the pointer;

3. release the pointer, to allow the CAB to recycle the

buffer if it is not used.

Page 28: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

CAB Primitives

Page 29: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

CAB Primitives

Page 30: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Writing in a CAB

Page 31: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Reading from a CAB

Page 32: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

Implementing CABs

Page 33: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

cab_reserve()

Page 34: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

cab_putmes()

Page 35: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

cab_getmes()

Page 36: Message-based Inter-task communicationrobotics.unibg.it/teaching/srt/pdf/07_TaskCommunication.pdf · Communication Ports Many operating systems provides the channel abstraction through

cab_unget()