Message Buffer

A message buffer is an object for achieving synchronization and communication by the passing of variable-size messages. Functions are provided for creating and deleting a message buffer, sending and receiving messages using a message buffer, and referencing message buffer status. A message buffer is an object identified by an ID number called a message buffer ID.

A message buffer keeps a queue of messages waiting to be sent (send queue) and a queue of tasks waiting for message receipt (receive queue). It also has a message buffer space for holding sent messages. The message sender (the side making event notification) copies to the message buffer a message it wants to send. If there is insufficient space in the message buffer area, the message is queued for sending until enough space is available. A task waiting to send a message to the message buffer is put in the send queue. On the message receipt side (waiting for event notification), one message is fetched from the message buffer. If the message buffer has no messages, the task enters WAIT state until the next message is sent. A task waiting for receipt from a message buffer is put in the receive queue of that message buffer.

Synchronous messaging can be realized by setting the message buffer space size to 0. In that case, both the sending task and receiving task wait for a system call to be invoked by each other, and the message is passed when both sides issue system calls.

[Additional Notes]

The message buffer functioning when the size of the message buffer space is set to 0 is explained here using the example in Figure 4.3. In this example, Task A and Task B run asynchronously.

  • If Task A calls tk_snd_mbf first, it goes to WAIT state until Task B calls tk_rcv_mbf. In this case Task A is put in the message buffer send queue (a).
  • On the other hand, if Task B calls tk_rcv_mbf first, Task B goes to WAIT state until Task A calls tk_snd_mbf. Task B is put in the message buffer receive queue (b).
  • At the point where both Task A has called tk_snd_mbf and Task B has called tk_rcv_mbf, a message is passed from Task A to Task B; then both tasks go from WAIT state to run state.



Figure 4.3: Synchronous Communication by Message Buffer

Tasks waiting to send to a message buffer send messages in their queued order. Suppose Task A wanting to send a 40-byte message to a message buffer, and Task B wanting to send a 10-byte message, are queued in that order. If another task receives a message opening 20 bytes of space in the message buffer, Task B is still required to wait until Task A sends its message.

A message buffer is used to pass variable-size messages by copying them. It is the copying of messages that makes this function different from the mailbox function.

It is assumed that the message buffer will be implemented as a ring buffer.

Comments

Click here to Post a Comment