Computer Science & Engineering Electrical Engineering EE 472 – Embedded Systems Dr. Shwetak Patel Assistant Professor Computer Science & Engineering Electrical Engineering
Agenda Critical regions Semaphores Lab 3 questions Midterms results 2 Shwetak N. Patel - EE 472
Mars Pathfinder 1997
Multitasking Need to share resources Think about the data in Lab 2
Sharing Global variables Shared buffers Producer / Consumer model
Buffers Single buffer Multiple buffers
Intertask Communication Messages Mailbox
Critical Sections Only one task can access a shared resource A short segment of code that cannot be preempted
Critical Sections What are ways to address the concurrency problem?
Critical Sections Ways to address this Don’t use a preemptive scheduler Mask interrupts Use protection code Flags Semaphores
Semaphores A type of flagging scheme Used to protect critical sections Atomic operation Have two operations: Wait(s) or Pend(s) or P(s) Signal(s) or Post(s) or V(s)
Semaphores Also used for synchronization
Semaphores Task can relinquish control until blocking is over Task is put in a waiting queue
Semaphores – Pros/Cons Easy to use and implement One semaphore per resource (reduce blocking) Drawback: Can cause priority inversion
Priority Inversion Hard to debug and detect Prevention techniques Priority inheritance
Deadlock Two or more processes wait indefinitely
Deadlock Prevention Conditions for deadlock (all 4 have to hold) Mutual exclusion No preemption Hold and wait Circular wait
Class Exercise Task1 1 X Task2 2 Task3 3 Task4 4 Task5 5 Task6 6 X Priority Resources R1 R2 R3 Task1 1 X Task2 2 Task3 3 Task4 4 Task5 5 Task6 6 X
Class Exercise How many semaphore are needed? 2 Where do you see priority inversion problems? Tasks 1,2,3 (R2)
Class Exercise Task1 1 X Task2 2 Task3 3 Task4 4 Task5 5 Task6 6 X Priority Resources R1 R2 R3 Task1 1 X Task2 2 Task3 3 Task4 4 Task5 5 Task6 6 X
Class Exercise How many semaphore are needed? 3 Where do you see priority inversion problems? Tasks 1,2,3 (R1) and 1,2,3 (R2) and 1,2,4 (R1) Do you see any other problems? Potential deadlock between Tasks 1 and 3
Class Exercise Task 1 1. Initialize digital I/O port for input 2. Read digital input 3. Count rising edges on each input Task 2 1. Read Data from serial port 1 2. Check for errors in serial data 3. if data buffer is empty {write data payload to buffer} Task 3 1. Initialize digital I/O port to output 2. write data to digital outputs 3. toggle bit 0 of digital I/O port on and off (i.e. make a brief pulse on bit 0). Task 4 1. if data buffer is full {take data from buffer} 2. Write data to serial port 2 Task 5 1. Count number of bytes in data buffer 2. display byte count on LCD
Class Exercise What resources are being shared? Do you see critical sections? Which tasks? Where in each task?
Class Exercise What resources are being shared? IO and the buffer Do you see a critical sections? Yes Which tasks and what resources? 1 - io,2 - buffer,3 - io, 4 – buffer, 5 - buffer Where in each task?
Questions? Shwetak N. Patel - EE 472