4.18.24 Movement Legacies, Reflection, and Review.pptx
Real time-embedded-system-lec-03
1. Real-time Embedded Systems- Lecture 03 Real-time Embedded Systems Lecture 3 RT Systems Essentials Part II RTOS-Synchronizatiion Prof. Dr. Amitava Gupta Department of Power Engineering Jadavpur University, Kolkata
2. Synchronization Techniques in RTOSs Explicit Implicit Cross Stimulation Semaphores Event flags shared Single proc. shared Single proc. flag counting burst fifo Real-time Embedded Systems- Lecture 03
3. Explicit Task Synchronization by Cross Stimulation The synchronizing processes know each others task id. Example Consider a system comprising two tasks. One task samples a set of temperature data from field sensors and another task computes the deviations from set values and switches on or off the devices that cause heating/cooling (actuators). timeframe T 1 T 2 Task T 1 collects data and stimulates T 2 Real-time Embedded Systems- Lecture 03
4. Advantages This scheme ensures that T 2 always processes the latest temperature data. Also it ensures that the task T 2 does not access the shared resource when T 1 writes on it. Disadvantages Depend on how the cross stimulation is implemented. If it is blocking, i.e. T 2 waits for the stimulation from T 1 , then the scheme has several problems:- T 2 should have a shorter period that T 1 If T 1 gets blocked, T 2 gets blocked too. What happens if the number of scanner tasks is more than one? Real-time Embedded Systems- Lecture 03
5. Using semaphores A semaphore is a structure that is used to guard access to a shared resource. It could be a flag or a multi-valued integer ( counting semaphore ) As a flag , the following operations are possible on a semaphore: set reset As a counting semaphore , these are: increment decrement ( a set operation) ( a reset operation) Real-time Embedded Systems- Lecture 03
6. The temperature controller using a flag semaphore timeframe T 1 T 2 Task T 1 collects data Task T 1 sets the semaphore, writes the data resets the semaphore No access for T 2 now Task T2 sets the semaphore reads the data resets the semaphore No access for T 1 now Real-time Embedded Systems- Lecture 03
7. Using semaphores: the Deadly Embrace problem Task T 1 pseudocode While (forever) { set semaphore_A ----------------------- ------------------------ set semaphore_B ----------------------- ----------------------- reset semaphore_A reset semaphore_B } Task T 2 pseudocode While (forever) { set semaphore_B ----------------------- ------------------------ set semaphore_A ----------------------- ----------------------- reset semaphore_B reset semaphore_A } Task T 1 blocks here Task T 2 blocks here Real-time Embedded Systems- Lecture 03
8.
9. The problem continues…. Priority inversion Let us suppose, we have 3 tasks, A , B and C which share a semaphore amongst them. The priority sequence is A > B > C . Let us suppose that C runs for some time, having set the semaphore, when it is pre-empted by A S After having run for sometime, A tries to set the same semaphore and is blocked. S Now, if B is ready, it will run-displacing A , a higher priority task. A possible solution would be to increase the priority of C to level more than that of B , till it resets the semaphore. Real-time Embedded Systems- Lecture 03
10. The counting semaphore Basic operation Each semaphore is associated with a MAX_COUNT value and is initialized to an initial value which is usually zero. Each set or increment will increase the COUNT by unity. When the COUNT reaches MAX_COUNT, the task trying to set will block. Each reset or decrement will decrease the COUNT by unity. When the COUNT reaches zero, the task trying to reset will block. This is ideal for a producer-consumer environment Real-time Embedded Systems- Lecture 03
11. The counting semaphore in a producer-consumer environment We assume that the Scanner task writes data on to a buffer which is read by the Controller. Thus the Scanner is the producer here and the Controller is the consumer. We further assume that the buffer can hold 2 data elements. We use a counting semaphore with a MAX_COUNT value of 2. The execution profile of the producer and the consumer Chalk and board Real-time Embedded Systems- Lecture 03
12. The Burst and FIFO mode of operation of semaphores Burst Mode : In this mode, if a number of tasks are in the blocked state waiting to set the semaphore, the task with the highest priority gets unblocked first. FIFO Mode : In this mode, if a number of tasks are in the blocked state waiting to set the semaphore, they get unblocked in the sequence they posted a request for set . Real-time Embedded Systems- Lecture 03
13. Execution Profile: Burst and FIFO mode semaphores Burst Mode Task priority A > B > C > D s s s s r r r D sets the semaphore & is pre-empted by B B is blocked when it tries to set the semaphore C is blocked when it tries to set the semaphore A is blocked when it tries to set the semaphore When D resets the semaphore, A gets It even though other tasks had posted requests before. Real-time Embedded Systems- Lecture 03
14. Execution Profile: Burst and FIFO mode semaphores FIFO Mode Task priority A > B > C > D s s s s r r r Task B gets the semaphore now. Real-time Embedded Systems- Lecture 03
15. The Event Flag Basic operation An Event Flag is a single byte or a two byte integer which is used to synchronize processes particularly in a producer-consumer environment. A Producer sets one or more bits in a pre- determined manner ……………… .. A consumer is activated when a certain combination is satisfied. This is determined by the application Real-time Embedded Systems- Lecture 03
16. The Multi-processor support Semaphores and Event Flags may be used to share resources across processors in a multi-processor environment. Such structures are located in the shared global memory . The usage is usually implementation specific with the following basic properties: Sharing is explicit . A shared synchronization structure has pre-defined processors associated with it. Semaphores (both flag and counting) are almost always in the FIFO mode . Real-time Embedded Systems- Lecture 03
17. The Last Word Initialization of Synchronization structures is Application Programmer's responsibility. An Application has to create and initialize these structures before they are used by any task. The OS provides the necessary system calls. Usually, this is done by a startup task which starts first when the application starts and then other tasks are started. Goodbye till next week. Please do not forget to bring a Calculator! Real-time Embedded Systems- Lecture 03