Priority inversion (how not to use semaphores)

Since semaphores are used to synchronize multiple tasks and guard shared resources, does this mean we can use them to protect a piece of data that's being shared between two tasks? Since each task needs to know when it is safe to access the data, the tasks need to be synchronized, right? The danger with this approach is that semaphores have no concept of task priority. A higher-priority task waiting on a semaphore being held by a lower-priority task will wait, regardless of what else might be going on in the system. An example of why this can become a problem will be shown here.

Here's the conceptual example we covered in Chapter 3, Task Signaling and Communication Mechanisms:

The main problems with this sequence are steps 3 and 4TaskB shouldn't be able to preempt TaskC if a higher-priority (TaskA) task is waiting on the semaphore. Let's look at an example of this in the wild with some real code and observe the behavior first-hand!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.147.215