2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 1
INTER-PROCESS COMMUNICATION AND SYNCHRONISATION OF
PROCESSES, THREADS and TASKS:
Lesson-16: Mailbox
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 2
1. IPC Mailbox functions
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 3
• Some OSes provide the mailbox and queue both IPC functions
• When the IPC functions for mailbox are not provided by an OS, then the OS employs queue for the same purpose.
Queue and MailboxQueue and Mailbox
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 4
MailboxMailbox
• Mailbox (for message) is an IPC through a message-block at an OS that can be used only by a single destined task.
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 5
Mailbox Mailbox ……
• A task on an OS function call puts (means post and also send) into the mailbox only a pointer to a mailbox message
• Mailbox message may also include a header to identify the message-type specification.]
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 6
Mailbox IPC featuresMailbox IPC features
• OS provides for inserting and deleting message into the mailbox message-pointer. Deleting means message-pointer pointing to Null.
• Each mailbox for a message need initialization (creation) before using the functions in the scheduler for the message queue and message pointer pointing to Null.
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 7
Mailbox IPC features…Mailbox IPC features…
• There may be a provision for multiple mailboxes for the multiple types or destinations of messages. Each mailbox has an ID.
• Each mailbox usually has one message pointer only, which can point to message.
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 8
Mailbox Types
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 9
Mailbox IPC features …Mailbox IPC features …
• When an OS call is to post into the mailbox, the message bytes are as per the pointed number of bytes by the mailbox message pointer.
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 10
2. Mailbox Related Functions at the OS2. Mailbox Related Functions at the OS
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 11
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 12
Tasks j sending a message-pointer into a mailbox and task k receiving that
Task jMailbox mb1
Post mb1, *mj
OS Mailbox functionsCreate, Query, Post,
Pend, Accept, Delete
Task CTask CTask kTask k
Pend mb1
*mb1*mb1
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 13
SectionSection of codes in the tasks B, C, and D waiting for messages of codes in the tasks B, C, and D waiting for messages mm1, 1, mm2 and 2 and mm3 into mailboxes3 into mailboxes
Task A Task B
Post m1
Pend m1
Task CTask C
Post m2
Pend m2
Post m3
Task DTask D
Pend m3
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 14
Task i sending message pointer *mi to initiate a task section waiting to take a message i before it could run
Task i
Task j
Send mi
Waitmi
OSOS
signal mask j
*mi Not Null
*mi = NULL
Start
*mi = NULL
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 15
Mailbox IPC functionsMailbox IPC functions1. OSMBoxCreate creates a box and
initializes the mailbox contents with a NULL pointer at *msg .
2. OSMBoxPost sends at *msg, which now does not point to Null.
• An ISR can also post into mailbox for a task
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 16
Mailbox IPC functions…Mailbox IPC functions…
3. OSMBoxWait (Pend) waits for *msg not Null, which is read when not Null and again *msg points to Null.
• The time out and error handling function can be provided with Pend function argument.
• ISR not permitted to wait for message into mailbox. Only the task can wait
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 17
Mailbox IPC functions…Mailbox IPC functions…4. OSMBoxAccept reads the message at
*msg after checking the presence yes or no [No wait.] Deletes (reads) the mailbox message when read and *msg again points to Null
• An ISR can also accept mailbox message for a task
5. OSMBoxQuery queries the mailbox *msg.
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 18
3. IPC Queue functions Application Example
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 19
int *m_j; A global variable integer pointer...; ...; ...; static void Task_j (void *taskPointer) {...while (1) {...; ...; ...; & m_i = 8; ...; ...; ...; OSMboxPost (m_j); /* after this instruction executes the next task section can operate on the m_j */...; ...; ...;}; }
Task_Task_jj sending an integer value sending an integer value mmjj
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 20
static void Task_k (void *taskPointer) {...while (1) {...; ...; ...;OSMboxPend (m_j); /* OSMboxPend waits for mailbox message m_i and when available m_i, reset *m_i = NULL and proceed to next statement */...; ...; ...;};}
Task_kTask_k waiting for the mwaiting for the mii
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 21
Task Task Read_AmountRead_Amount in ACVMin ACVM
static void Task Read-Amount (void *taskPointer) {
.while (1) {./* Codes for reading the coins inserted into
the machine */
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 22
Task Task Read_AmountRead_Amount posting amount posting amount information information in ACVMin ACVM
/* Codes for writing into the mailbox full amount message if cost of chocolate is received*/
OSMboxPost (mboxAmt, fullAmount) /* Post for the mailbox message and fullAmount, which equaled null now equals fullAmountmessage pointer*/
.};
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 23
Chocolate delivery task in ACVMChocolate delivery task in ACVM
static void Chocolate delivery task (void *taskPointer) {
.while (1) {.
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 24
Chocolate delivery task waiting for Chocolate delivery task waiting for message in ACVMmessage in ACVM
/* IPC for requesting full amount message */fullAmountMsg = OSMboxPend (mboxAmt,
20, *err) /* Wait for the mailbox mboxAmtmessage for 20 clock ticks and error if message not found. mboxAmt becomes null after message is read.
.};
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 25
Task_Task_User_Keypad_InputUser_Keypad_Input in ACVMin ACVM
static void Task_User_Keypad_Input (void *taskPointer) {
.while (1) {./* Codes for reading keys pressed by the user
before the enter key */
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 26
Task_Task_User_Keypad_InputUser_Keypad_Input posting amount posting amount information information in ACVMin ACVM
/* Codes for writing into the mailbox */ OSMboxPost (mboxUser, userInput) /* Post
for the mailbox message and userInput, which equaled null now equals userInputmessage pointer*/
.};
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 27
Task_DisplayTask_Display in ACVMin ACVM
static void Task_Display (void *taskPointer){
.while (1) {.
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 28
Task_DisplayTask_Display waiting for a message in waiting for a message in ACVMACVM
/* IPC for waiting for User input message */ UserInputMsg = OSMboxPend (mboxUser,
20, *err) /* Wait for the mailbox mboxUsermessage for 20 clock ticks and error if message not found. mboxUser becomes null after message is read.
./* Code for display of user Input */
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 29
Task_DisplayTask_Display waiting for another message waiting for another message in ACVMin ACVM
TimeDateMsg = OSMboxPend (timeDate, 20, err) /* Wait for the mailbox message timeDate.
/* Code for display TimeDateMsg Time: hr:mm Date: month:date */
.};
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 30
SummarySummary
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 31
• OS provides the IPC functions • Create, Post, PostFront, Pend, Accept, Flush
and Query for using message at mailbox. • The time out and error handling function
can be provided with Pend function argument.
We learntWe learnt
2008 Chapter-8 L15: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs.: McGraw-Hill, Inc. 32
End of LLesson-16: Mailbox
Top Related