I have read many posts on priority inversion still I am not able to clarify my understanding on some of the parts. I would be happy if someone can throw some light on my question.
Let's describe the situation first. I have a pseudo code, that is self explanatory.
I have a shared resource - int t; Here is the function that will be executed by the three threads - low task p1, medium priority task p2, high priority task p3.
/**Shared resource **/
int t;
/** Function that will be executed by three threads **/
void func()
{
printf("hello..world"); /** line number 11**/
mutex_lock(&lock); /** line number 12**/
{ /** line number 13**/
for(int i = 0; i<=100; i++) /** line number 14**/
{ /** line number 15**/
t++; /** line number 16**/
} /** line number 17**/
} /** line number 18**/
mutex_unlock(&lock); /** line number 19**/
} /** line number 20**/
Let's say p1(low p).. starts executing func(). Now let's say it is in the line number- 13. after mutex lock.Mean while let's say p3 ..starts running. Now, p3 will be blocked because p1 is in the critical section. So, p3 goes to the blocking state.
Scenario- p1 - inside critical section - in the Running State. p3 - blocked state.
Now, let's say p2 starts running. As p2 is in the running state, it will also be blocked by p1 since p2 is in the critical section. Then how come a priority inversion happens here? I am missing the understanding afterwards, kindly please explain me..
Are my understanding below is correct? If not, then please correct it. What should be the situation when the priority inversion happens by p2 task? I understand that priority inversion happens when p2 starts running. After p2 completion, p1 starts running. And p3 never gets a chance.Or it could be that after p2 is done, p3 runs. This makes the p3 delay. In such cases, the mutex timeout can happen.
This was one of the scenario - bug in our software. Where there was a crash due to mutex time out. This was happening because of priority inversion somebody said. This was fixed by setting the mutex attribute to priority inheritance. I was trying to post-mortem the fix, but I am held with the priority inversion fundamentals. I have read many post - Mars path finder, but I am stuck with my questions. Please help me here.