When tasks share resources, as they often do, strange things can and will happen. Priority inversions can be particularly difficult to anticipate. Here's an introduction to priority inversions and a pair of techniques you can use to avoid them.
Most commercial real-time operating systems (RTOSes) employ a priority-based preemptive scheduler. These systems assign each task a unique priority level. The scheduler ensures that of those tasks that are ready to run, the one with the highest priority is always the task that is actually running. To meet this goal, the scheduler may preempt a lower-priority task in mid-execution.
Because tasks share resources, events outside the scheduler's control can prevent the highest priority ready task from running when it should. If this happens, a critical deadline could be missed, causing the system to fail. Priority inversion is the term for a scenario in which the highest-priority ready task fails to run when it should.
Resource sharing
Tasks need to share resources to communicate and process data. This aspect of multi-threaded programming is not specific to real-time or embedded systems.
Any time two tasks share a resource, such as a memory buffer, in a system that employs a priority-based scheduler, one of them will usually have a higher priority. The higher-priority task expects to be run as soon as it is ready. However, if the lower-priority task is using their shared resource when the higher-priority task becomes ready to run, the higher-priority task must wait for the lower-priority task to finish with it. We say that the higher-priority task is pending on the resource. If the higher-priority task has a critical deadline that it must meet, the worst-case "lockout time" for all of its shared resources must be calculated and taken into account in the design. If the cumulative lockout times are too long, the resource-sharing scheme must be redesigned.
Since worst-case delays resulting from the sharing of resources can be calculated at design time, the only way they can affect the performance of the system is if no one properly accounts for them.
Priority inversions
The real trouble arises at run-time, when a medium-priority task preempts a lower-priority task using a shared resource on which the higher-priority task is pending. If the higher-priority task is otherwise ready to run, but a medium-priority task is currently running instead, a priority inversion is said to occur.
This dangerous sequence of events is illustrated in Figure 1. Low-priority Task L and high-priority Task H share a resource. Shortly after Task L takes the resource, Task H becomes ready to run. However, Task H must wait for Task L to finish with the resource, so it pends. Before Task L finishes with the resource, Task M becomes ready to run, preempting Task L. While Task M (and perhaps additional intermediate-priority tasks) runs, Task H, the highest-priority task in the system, remains in a pending state.
Many priority inversions are innocuous or, at most, briefly delay a task that should run right away. But from time to time a system-critical priority inversion takes place. Such an event occurred on the Mars Pathfinder mission in July 1997. The Pathfinder mission is best known for the little rover that took high-resolution color pictures of the Martian surface and relayed them back to Earth.
The problem was not in the landing software, but in the mission software run on the Martian surface. In the spacecraft, various devices communicated over a MIL-STD-1553 data bus. Activity on this bus was managed by a pair of high-priority tasks. One of the bus manager tasks communicated through a pipe with a low-priority meteorological science task.
On Earth, the software mostly ran without incident. On Mars, however, a problem developed that was serious enough to trigger a series of software resets during the mission. The sequence of events leading to each reset began when the low-priority science task was preempted by a couple of medium-priority tasks while it held a mutex related to the pipe. While the low-priority task was preempted, the high-priority bus distribution manager tried to send more data to it over the same pipe. Because the mutex was still held by the science task, the bus distribution manager was made to wait. Shortly thereafter, the other bus scheduler became active. It noticed that the distribution manager hadn't completed its work for that bus cycle and forced a system reset.
This problem was not caused by a mistake in the operating system, such as an incorrectly implemented semaphore, or in the application. Instead, the software exhibited behavior that is a known "feature" of semaphores and intertask communication. In fact, the RTOS used on Pathfinder featured an optional priority-inversion workaround; the scientists at JPL simply hadn't been aware of that option. Fortunately, they were able to recreate the problem on Earth, remotely enable the workaround, and complete the mission successfully.
Workarounds
Research on priority inversion has yielded two solutions. The first is called priority inheritance. This technique mandates that a lower-priority task inherit the priority of any higher-priority task pending on a resource they share. This priority change should take place as soon as the high-priority task begins to pend; it should end when the resource is released. This requires help from the operating system.
The second solution, priority ceilings, associates a priority with each resource; the scheduler then transfers that priority to any task that accesses the resource. The priority assigned to the resource is the priority of its highest-priority user, plus one. Once a task finishes with the resource, its priority returns to normal.
A beneficial feature of the priority ceiling solution is that tasks can share resources simply by changing their priorities, thus eliminating the need for semaphores:
void TaskA(void)
{
...
SetTaskPriority(RES_X_PRIO);
// Access shared resource X.
SetTaskPriority(TASK_A_PRIO);
...
}
While Task A's priority is elevated (and it is accessing shared resource X), it should not pend on any other resource. The higher-priority user will only become the highest-priority ready task when the lower-priority task is finished with their shared resource.
While not all of us are writing software for missions to Mars, we should learn from past mistakes and implement solutions that don't repeat them. Many commercial RTOSes include support for either priority inheritance or priority ceilings. Just make sure you enable one.
This comment has been removed by the author.
ReplyDeleteYour blog provided us with valuable information to work with. Each & every tips of your post are awesome. Thanks a lot for sharing. Keep blogging.
ReplyDeleteنقل عفش بالدمام
شركة نقل عفش بالدمام
شركة نقل اثاث بالدمام
You have done a great job. I will definitely dig it and personally recommend to my friends. I am confident they will be benefited from this site
ReplyDeleteشركة تنظيف بالدمام
شركة تنظيف بالخبر
شركة تنظيف بالجبيل
I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. In fact your creative writing abilities has inspired me to start my own BlogEngine blog now. Really the blogging is spreading its wings rapidly. Your write up is a fine example of it
ReplyDeleteشركة نقل اثاث بالدمام
شركة نقل اثاث بالقطيف
شركة نقل عفش بالدمام
شركة نقل اثاث بالخبر
hank you so much for this very usefull information. It's just a very effectively written article. It will likely be useful to anyone who makes use of it
ReplyDeleteارخص شركة نقل اثاث بالدمام
شركة نقل اثاث بالجبيل
شركة نقل اثاث بالقطيف
I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post
ReplyDeleteشركة غسيل خزانات بالمدينة المنورة
شركة غسيل خزانات بالمدينة
شركة تنظيف خزانات بالمدينة
If you are being attentive to learn several strategies then you ought to browse this article, I am certain you'll get much additional from this article
ReplyDeleteشركة تنظيف خزانات بالدمام
شركة غسيل السجاد بالدمام
شركة تنظيف السجاد بالدمام
شركة تنظيف السجاد بالقطيف
I have read a few of the articles on your website now, and I really like your style of blogging. I added it to my favorites blog site list and will be checking back soon. Please check out my site as well and let me know what you think
ReplyDeleteتنظيف منازل بالدمام
شركه تنظيف منازل بالدمام
شركه تنظيف شقق بالقطيف
شركه تنظيف شقق بالدمام
I got what you mean , a debt of gratitude is in order for posting .Woh I am cheerful to discover this site through google
ReplyDeleteشركة تنظيف كنب بالقطيف
شركة تنظيف السجاد بالدمام
شركة تنظيف السجاد بالقطيف
After exploring a handful of the blog articles on your web site, I really like your technique of blogging. I added it to my bookmark webpage list and will be checking back in the near future
ReplyDeleteشركة كشف تسربات المياه بالخبر
شركة كشف تسربات المياه بالدمام
شركة كشف تسربات المياه بالقطيف
شركة كشف تسربات المياه بالجبيل
This is very informatics, fresh and clear. I feel that everything has been portrayed in methodical way with the goal that peruser could get greatest data and realize numerous things
ReplyDeleteشركة تنظيف مكيفات بالجبيل
شركة تنظيف مكيفات بالقطيف
شركة تنظيف مكيفات بالخبر
I have perused your article, it is exceptionally instructive and accommodating for me.I respect the important data you offer in your articles
ReplyDeleteشركة صيانة مكيفات بالجبيل
شركة صيانة مكيفات بالدمام
شركة صيانة مكيفات بالخبر
شركة صيانة مكيفات بالقطيف
I actually checked right up your website considering Concerning listened to a great deal of pertaining to a person's discussions. Grateful to talk about, a gossip very well; you will be okay around this
ReplyDeleteشركة غسيل مكيفات بالدمام
شركة تنظيف مكيفات بالدمام
شركة تنظيف مكيفات بالجبيل
شركة تنظيف مكيفات بالخبر
TOP Movers in Abu Dhabi
ReplyDeletehttps://www.wemovers.ae/