Priority inversion and priority inheritance
Tags: syncstitch, rtos, concurrency
Published: 2018-07-22 00:49:42

---
title: Priority inversion and priority inheritance
published: true
description: 
tags: SyncStitch, RTOS, concurrency
---

# Priority inversion

see [Wikipedia](https://en.wikipedia.org/wiki/Priority_inversion).

I will deal with the problem in the following steps:

1. Model the problem on [SyncStitch](https://www.principia-m.com/syncstitch/).
2. Observe the problem by simulation.
3. Define the safety specificaion againt the problem.
4. Implement priority-inheritance protocol.
5. Check the safety.

# Warming up

Using SyncStitch, you can easily realize and test your ideas rapidly.
Before challenging the priority inversion problem, build a simple model as warming-up.

A simple RTOS can be modelled on which processes has the following state-transitions.

![sd1](https://thepracticaldev.s3.amazonaws.com/i/g39ut8moo30xx62egu50.png)

The kernel can be modelled like this: (just a sketch, this may be the smallest rtos in the world?)

![ker0](https://thepracticaldev.s3.amazonaws.com/i/5wt1otk6o9mzagq39d6b.png)

Then, you can perform simulations for it:

![pe0](https://thepracticaldev.s3.amazonaws.com/i/c54zyzaalj0c0t21bqfy.png)

You can see that a process of lower priority is preempted by a higher one.

# Implement locks

Like this:


![sd2](https://thepracticaldev.s3.amazonaws.com/i/iogd627axf3qkpqai5wl.png)


![kernel](https://thepracticaldev.s3.amazonaws.com/i/jmrtilbujyeri3zwwh62.png)

Of course there is possibilities for processes to be preempted in locks.
In fact, you can observe the following sequence:

1. Process 2 wakes up (the bigger number, the lower priority).
2. Process 2 locks.
3. Process 0 wakes up.
4. Process 2 is preempted.
5. Process 0 runs. try to lock, but failed since it is already owned by Process 2.
6. Process 1 wakes up. It can run as long as it wants though the higher Process 0 is waiting for the lock.

![pe2](https://thepracticaldev.s3.amazonaws.com/i/cfsy7akwi9ab63unla0b.png)

This phenomenon is what is called the priority-inversion problem. This can be show as a diagram:

![pi](https://thepracticaldev.s3.amazonaws.com/i/2pdz245jk640ziolmb24.png)


# Safety specification

We observed the problem by simulation above. Next, we make a specification which enable the tool automatically detect the problem or make sure there is no problem.

This type of specifications are called safety specifications, which can be described as a state-transition which accepts only possible sequecens and does not include any harmful sequence.

We can define the safety specification for the priority-inversion protocol like this:

![safety](https://thepracticaldev.s3.amazonaws.com/i/n4rbu1zdys0mhgdwqkvj.png)

SyncStitch is one of what they call refinement checkers, and it has the ability to compare the given specification and the given implementation model and check the correctness relation between them.

The result of the check is this; a violation is found:

![assert1](https://thepracticaldev.s3.amazonaws.com/i/2xitjlvzwjhv57cjps1j.png)

Then, you can thoroughly investigate the violation using the analyzing tools provided by SyncStitch.

You can see the same sequence we saw above for the violation:

![seq](https://thepracticaldev.s3.amazonaws.com/i/fqxps80yp1itxgcmd8xu.png)

# Priority-inheritance protocol

One of the solutions for the priority-inversion problem is the priority-inheritance protocol. The idea is simple: when schedule the processes, select the process which blocks the process of highest priority.

![pi](https://thepracticaldev.s3.amazonaws.com/i/7km3lw9pluj09budp424.png)

The function for the selection is like this:

![sp](https://thepracticaldev.s3.amazonaws.com/i/xqv491i7k6nqo4slgmfe.png)

Putting it into the model, you will see the problem is resolved.

![assert2](https://thepracticaldev.s3.amazonaws.com/i/d3971xjfvl7aj3w9q6xu.png)