3

In the below scenario how the priority of the task t1 will change when the locks are released, assuming Sem_Take() and Sem_Give() are lock and release method.

I understand that using priority ceiling protocol raises the priority of the task as soon as a resource is locked, but what happens when the lock is released.

    void t1()//Initial priority 2
{
    int a;
    Sem_take(S1); //priority ceiling for S1 is 4
    .
    .
    Sem_take(S2);//priority ceiling for S2 is 6
    .
    .
    Sem_Give(S1);
    .//What is the priority at this line?
    .
    Sem_Give(s2);
    .//What is the priority at this line?
    .
}

Also in the above scenario the semaphore lock and release are mismatched that is it erroneous but a program might mistakenly do that, then in this case how the PCP will work.

Anuj Priyadarshi
  • 347
  • 4
  • 16

1 Answers1

2

Priority ceiling was created to avoid priority inversion. A well implemented algorithm would always give each process the highest priority that is associated with each of the resources held by that process (in this case semaphores). So specifically regarding your code sample: after taking S1, the process priority will be raised to 4, then after taking S2 it will be raised again to 6. Upon releasing S1 priority is still 6 (S2 is still held). Upon releasing S2 it should revert back to priority == 2.

Uri Brecher
  • 437
  • 2
  • 11
  • Understood. but what happens if S1 priority is 6 and S2 priority is 4, so upon releasing S1 the priority will revert back to 2 and then again raised to 4, as it was never raised to 4 before this. If this is case how will this raise to 4 will be triggered as s2 has been locked before releasing S1, then what will trigger this rasie. – Anuj Priyadarshi Jun 01 '16 at 06:02
  • I totally understand your concerns and I can assume some RTOS had a vulnerable implementation at one point of time. However I do believe this could be implemented correctly. Whatever RTOS your using you can get a list of all running processes along with their priorities and see the result for yourself. As for making sure your process isn't downgraded to p2 before raised back to 4 that's more tricky. You would have to write some code that to detect this. I need to think about it. – Uri Brecher Jun 01 '16 at 06:15
  • To detect a glitch in priority create a priority 3 process that perform I/O operation in a loop. If your first process has a priority 2 transient then it will allow the I/O process to schedule in and perform it I/O. – Uri Brecher Jun 01 '16 at 06:26