INTERThread COMMUNICATION AND SYNCHRONISATION Lesson13 Inter Thread communication

  • Slides: 22
Download presentation
INTER-Thread COMMUNICATION AND SYNCHRONISATION: Lesson-13: Inter Thread communication 2015 Chapter-9 L 5: "Embedded Systems

INTER-Thread COMMUNICATION AND SYNCHRONISATION: Lesson-13: Inter Thread communication 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 1

1. Inter Thread Communication 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and

1. Inter Thread Communication 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 2

Need of Inter Thread Communication • Assume that there is need to send through

Need of Inter Thread Communication • Assume that there is need to send through the kernel an output data (a message of a known size with or without a header or a flag to notify an event) for Threading or taking note of by another task 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 3

Inter Thread Communication l l Inter Thread communication from a Thread (task or Thread

Inter Thread Communication l l Inter Thread communication from a Thread (task or Thread to another) for a message Inter Thread communication in a multi. Thread system─ used to generate information about certain sets of computations finishing on one Thread and to let the other Threads waiting for finishing the computations take note of the information 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 4

Need of Inter Thread Communication • Global variables problems • Shared data • No

Need of Inter Thread Communication • Global variables problems • Shared data • No encapsulation of the data or message accessibility by other tasks • OS Inter Thread Communication (IPC) functions based solutions 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 5

Inter Thread Communication (IPC) • means that a Thread (scheduler, task or ISR) generates

Inter Thread Communication (IPC) • means that a Thread (scheduler, task or ISR) generates some information by signal (for other Thread start) or value (for example of semaphore) or generates an output so that it lets another Thread take note or use it through the kernel functions for the IPCs 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 6

Inter Thread Communication (IPC) in Multitasking System • Used to signal for other Thread

Inter Thread Communication (IPC) in Multitasking System • Used to signal for other Thread to start or • post a token or flag or • generate message from the certain sets of computations finishing on one task and to let the other tasks take note of signal or get the message 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 7

Operating System Provisioning for the IPC functions • Signal for other Thread to start

Operating System Provisioning for the IPC functions • Signal for other Thread to start • Semaphore (as token, mutex) or counting semaphores for the inter task communication between tasks sharing a common buffer • Queue, • Mailbox • 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 8

2. Example of mutex Inter Thread Communication for print 2015 Chapter-9 L 5: "Embedded

2. Example of mutex Inter Thread Communication for print 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 9

Inter Thread Communication for print • print task can be shared among the multiple

Inter Thread Communication for print • print task can be shared among the multiple tasks, which use the mutex semaphore IPC in their critical sections • When the buffer becomes available for new data, an IPC from the print task is generated and the kernel first takes note of it. • Other tasks then take note of the IPC. 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 10

Inter Thread Communication for print • A task take note of IPC by OSSem.

Inter Thread Communication for print • A task take note of IPC by OSSem. Pend ( ) function─ used at the beginning of the critical section • The task gets mutually exclusive access to the section to send messages into the print-buffer by using the OSSem. Post ( ) function of the kernel at the end of the section 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 11

3. Example of semaphore and mailbox Inter Thread Communication for display of date and

3. Example of semaphore and mailbox Inter Thread Communication for display of date and time 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 12

Threades for display of updated date and time • A mobile phone device Update_Time

Threades for display of updated date and time • A mobile phone device Update_Time task • A task, Task_Display for a multi-line display of outputs which displays current time on last line 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 13

Semaphore Inter Thread Communication for display of date and time • When the multi-line

Semaphore Inter Thread Communication for display of date and time • When the multi-line display task finishes the display of the last but one line, an IPC semaphore supdate. TD from the display task is posted and the kernel takes note of it. • The task — continuously updating time — then takes the supdate. TD 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 14

Coding Example of posting semaphore static void Task_Display (void *task. Pointer) {. while (1)

Coding Example of posting semaphore static void Task_Display (void *task. Pointer) {. while (1) {. . /* IPC for requesting Time. Date */ OSSem. Post (supdate. TD) /* Post for the semaphore supdate. TD. 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 15

Coding Example of waiting for mailbox message. /* IPC for waiting time and date

Coding Example of waiting for mailbox message. /* IPC for waiting time and date in the mailbox */ Time. Date. Msg = OSMbox. Pend (time. Date) /* Wait for the mailbox message time. Date. The time. Date becomes null after the Time. Date. Msg equals the updated time and date */ /* Code for display Time. Date. Msg Time: hr: mm Date: month: date */. Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , }; 2015 16 Raj Kamal, Publs. : Mc. Graw-Hill Education

Coding Example of waiting for the semaphore static void Task_ Update_Time (void *task. Pointer)

Coding Example of waiting for the semaphore static void Task_ Update_Time (void *task. Pointer) {. while (1) { OSSem. Pend (supdate. TD) /* Wait the semaphore supdate. TD. supdate. T becomes 0 after taking the semaphore */. /* Codes for updating time and date as per the number of clock interrupts received so far */ 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 17

Coding Example for posting the mailbox message /* Codes for writing into the mailbox

Coding Example for posting the mailbox message /* Codes for writing into the mailbox */ OSMbox. Post (time. Date) /* Post for the mailbox message and time. Date, which equaled null earlier, now equals new updated time and date*/. }; 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 18

An Alternative for IPC functions • Pipe device • Socket device • Remote procedure

An Alternative for IPC functions • Pipe device • Socket device • Remote procedure call (RPC) for distributed Threades. 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 19

Summary 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj

Summary 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 20

We learnt l Inter Thread communication (IPC) means that a Thread (scheduler or task

We learnt l Inter Thread communication (IPC) means that a Thread (scheduler or task or ISR) generates some information by setting or resetting a Token or value, or generates an output so that it lets another Thread take note or to signal to OS for starting a Thread or use it under the control of an OS 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 21

End of Lesson 13 on Inter Thread Communication 2015 Chapter-9 L 5: "Embedded Systems

End of Lesson 13 on Inter Thread Communication 2015 Chapter-9 L 5: "Embedded Systems - Architecture, Programming and Design" , Raj Kamal, Publs. : Mc. Graw-Hill Education 22