User:Pravasmohanty

Mutexes Prevent Priority Inversion
By Pravas Ranjan Mohanty

Himachal Futuristic Comm. Ltd Solan

Priority inversions can interfere with the normal operation of multitasking

application software. The use of mutexes, which provide mutual exclusion for

data structure access, can solve the problem.

Priority inversion situations are sometimes the underlying cause of what are popularly

called real-time "software glitches." They can have a serious impact on application

software, such as data losses or even total system resets.

After I describe the phenomenon of priority inversion, I'll present a detailed example of a

priority inversion scenario. I'll make the point that even when using an operating system's

semaphore features, a serious problem known as unbounded priority inversion can

develop.

I'll introduce an operating system mechanism called a mutex (for mutual exclusion),

which has the potential of addressing problems of unbounded priority inversion. Mutexes

come in several varieties. Priority inheritance mutexes and priority ceiling mutexes will

be examined in detail, and their relative advantages and disadvantages weighed.

What is priority Inversion?

Modern real-time operating systems support multitasking with a priority-based preemptive scheduler. Each concurrent task (or "thread") in an application program isassigned a priority number. It's the job of the scheduler to ensure that at all times, the highest priority task that is ready to run will be the task which is actually running. The scheduler may stop (pre-empt) a running task in mid-execution to ensure this, if a higher priority task becomes ready to run.

However, because of the often complex interrelationships between tasks, situations might occur in which a relatively low-priority task will be running at a time when you would want or expect a high-priority task to be executing. This is priority inversion. Priority inversions aren't generally caused by errors in the design of the scheduler of an operating system; rather, OS task schedulers are pretty reliable these days. Instead, priority inversions occur when the operating system has marked a task as unready to run at a moment when the software designer would have wanted it to be running:

The inversion might occur, for example, if a task of high priority wishes to run to process a message arriving at a certain queue. But if the message has been given to another (lower-priority) task instead, then that lower-priority task would be allowed to run instead. The OS would block the high priority task from running, declaring it unready to run for lack of a message to process. In this case, the operating system might well be working as designed and as instructed to work. (Perhaps instructions are being given to the OS without their ramifications being fully understood.) But if the software designer