120 likes | 263 Views
Advanced Embedded Systems Design. Lecture 4 Preemptive multi-tasking – inter-task communications BAE 5030 - 003 Fall 2004 Instructor: Marvin Stone Biosystems and Agricultural Engineering Oklahoma State University. Goals for Class Today. Questions over reading Preemptive multi-tasking
E N D
Advanced Embedded Systems Design Lecture 4 Preemptive multi-tasking – inter-task communications BAE 5030 - 003 Fall 2004 Instructor: Marvin Stone Biosystems and Agricultural Engineering Oklahoma State University
Goals for Class Today • Questions over reading • Preemptive multi-tasking • Time triggered scheduler • Keil Compiler • interrupts / profiling • Set assignment
inter- task communications • Required to allow data to be passed between producing and consuming tasks • Queueing is needed when inflow and outflow rates are not matched. (average inflow <= average outflow) • Types • message queues (ping-pong, circular) • Pipes - FIFO buffer between tasks • Sockets - bidirectional, sequenced flow between tasks • Other • Task synchronization and mutual exclusion mechanisms • binary / mutual-exclusion semaphores • counting semaphores – where multiple of a resource must be metered
Nature of inter-task communications • Generally done with shared memory • Tasks mutually understand a named memory as a communications pathway • Preemptive multi-tasking systems • Require inter-task coordination of access to shared memory • Non-atomic access to memory corrupts communication • Named shared memory is a limited shared resource • Time triggered systems • Time division multiplexing assures no two tasks run at the same time therefore do not require coordination to shared memory • Assumes shared memory is completely updated during each tasks access
Inter-task communications - Example • Task 1 writes integer buffer for consumption during its execution • Task 2 reads integer buffer during its execution • If Task 1 is interrupted during the writing of the integer by task 2, communications will be corrupted. (Race condition) • Example: See 8051 MOV instruction and storage of int.
Task 1 Tests Semaphore to assure not busy otherwise waits Sets Semaphore to “Busy” Writes integer Sets Semaphore to “Free” Task 2 Tests Semaphore to assure not busy otherwise waits Sets Semaphore to busy Reads integer Sets Semaphore to “Free” Inter-task communications – Binary Semaphore
Inter-task communications – Binary Semaphore – Issues • Note that in both task 1 and task 2, both test then set the semaphore to “Busy” • Consider the potential problem that might occur if task 1 interrupts task 2 between the test of semaphore and set to busy • Task 1 concludes semaphore is free, sets the semaphore to “Busy”, Begins writing to Integer Flag. • Task 2 also sets flag to “Busy” and reads while Task 1 writes • Corrupted communications could result. Generally Task 2 completes before task 1 may continue and the process works successfully – not as intended • An assumption in the above system is that the semaphore can be written in a single un-interruptable (atomic) process. If the writing of the semaphore is a multi-instruction process, an interrupt could occur during writing of the semaphore and a corrupted result would occur as in the original example.
Task 1 Increments Semaphore to indicate an integer has been added to the queue Writes integer Task 2 Tests semaphore to assure it is > 0 Reads integer Decrements Semaphore to indicate an integer has been removed from the queue Inter-task communications – Counting Semaphore
Inter-task communications – Counting Semaphore – Issues • As in the original problem, Synchronized access to shared data must be provided. • Here a binary semaphore could be used to control access to the resource and counting semaphore • An “All-in-one” counting semaphore can be used assuming atomic access
Strategies to test and set semaphore in an atom ;Subroutine to set semaphore to Free SETB Semaphore RTS ;Subroutine to set semaphore to Busy CLRB Semaphore RTS ;Subroutine to check and wait on semaphore ;Set indicates Free, Clear indicates Busy testset JBC Semaphore, busy ; Jump if semaphore set and clear semaphore JMP testset busy RTS
Message Queues / Pipes / Sockets • Memory buffer • Character • Normally organized as FIFO • Bi-directional flow (sockets) and flow control handshaking between processes may be supported • Function • Head maintained at the “read” end by consumer. • Tail maintained at “write” end by producer • Tail advanced to the end of buffer and then to beginning • Tail may not advance past head (Buffer full) • Head may not be consumed beyond tail (Buffer empty)
Assignment • Code a program to receive characters from the serial port and to accumulate a string of them and print the string after receiving a carriage return. Use the hybrid scheduler given in Pont. Use a serial port ISR to receive characters and a time triggered task to accumulate characters and print after a carriage return. • Read Pont, Chapter 17 • Tutorial – 30 min • Explain Rate Monotonic Analysis