Priority Ceiling Protocols: Today's Topic: Resource/Data Sharing and Synchronization
Priority Ceiling Protocols: Today's Topic: Resource/Data Sharing and Synchronization
1
Basic functions of RTOS kernel
Time management
Task mangement
Interrupt handling
Memory management
no virtual memory for hard RT tasks
Exception handling
Task synchronization
Avoid priority inversion
Task scheduling
Task 1 Task 9
- Shared Resource R -
- -
P(S) P(S)
Using R Using R
V(S) V(S)
- -
- -
Task 1 P(S)
computing
using R
Task 9 P(S) V(S)
blocked
2
Un-bounded priority inversion
Task 1 Task 9 Task 2
-
- Shared Resource R -
- - -
P(S) P(S) -
Using R Using R -
V(S) V(S)
- -
- -
Task 2 ...
computing
using R
Task 9 P(S) V(S)
blocked
Solutions
3
Non Preemption Protocol (NPP)
Modify P(S) so that the ”caller” is assigned
the highest priority if it succeeds in locking S
Highest priority=non preemtion!
Modify V(S) so that the ”caller” is assigned its own
priority back when it releases S
NPP: + and –
P(s) V(s)
4
Basic Priority Inheritance Protocol (BIP)
supported in RT POSIX
Idea:
A gets semaphore S
B with higher priority tries to lock S, and blocked by S
B transfers its priority to A (so A is resumed and run with
B’s priority)
Run time behaviour: whenever a lower-
priotity task blocks a higher priority task, it
inherits the priority of the blocked task
Example
V(S1)
P(S1) P(S2) V(S2)
H Task 1
P(S1)
V(S1)
M Task 2
P(S2)
V(S2)
L Task 3
Blocked
Using S1
Running with priority H Using S2
10
5
Problem 1: potential deadlock
P(S2)
H P(S1) Task 1
P(S1)
11
V(S1)
P(S1) P(S2) V(S2)
H Task 1
P(S1)
V(S1)
M Task 2
P(S2)
V(S2)
L Task 3
Blocked
Using S1
Using S2
Task 1 needs M resources may be blocked M times:
many preemptions/run-time overheads
12
maximal blocking=sum of all CS sections for lower-priority tasks
6
BIP: Blocking time calculation
Let
CS(k,S) denote the computing time for the critical section
that task k uses semaphore S.
13
14
7
Implementation of Ceiling Protocols
Main ideas:
Priority-based scheduling
Implement P/V operations on Semaphores to assign task
priorities dynamically
15
counter
queue
Holder
16
8
Standard P-operation (without BIP)
P(scb):
Disable-interrupt;
If scb.counter>0 then {scb.counter - -1;
else
{save-context( );
current-task.state := blocked;
insert(current-task, scb.queue);
dispatch();
load-context() }
Enable-interrupt
17
18
9
Standard V-operation (without BIP)
V(scb):
Disable-interrupt;
If not-empty(scb.queue) then
{ next-to-run := get-first(scb.queue);
next-to-run.state := ready;
insert(next-to-run, ready-queue);
save-context();
schedule(); /* dispatch invoked*/
load-context() }
else scb.counter ++1;
Enable-interrupt
19
20
10
Immediate Priority Inheritance:
=Highest Locker’s Priority Protocol (HLP)
=Priority Protect Protocol (PPP)
21
22
11
Example
23
H Task 1 blocked
P(S) V(S)
L
P(S) V(S)
Task 3
Lower
24
12
Property 1: Deadlock free (HLP)
P(S2) P(S1)
released
H Task 1
P(S1) P(S2)
L Task 2
Property 2:
Tasks will be blocked at most once
P(S1) V(S1)V(S2)
P(S2)
Ready and blocked
P(S1) V(S1)
26
13
HLP: Blocking time calculation
Let
CS(k,S) denote the computing time for the critical section
that task k uses semaphore S.
27
Implementation of HLP
28
14
Semaphore Control Block for HLP
counter
queue
Ceiling
29
P(scb):
Disable-interrupt;
If scb.counter>0 then
{ scb.counter - -1;
save(current-task.priority);
current-task.priority := Ceiling(scb) }
else
{save-context();
current-task.state := blocked
insert(current-task, scb.queue);
dispatch();
load-context() }
Enable-interrupt
30
15
V-operation with HLP
V(scb):
Disable-interrupt;
current-task.priority := get(previous-priority)
If not-empty(scb.queue) then
next-to-run := get-first(scb.queue);
next-to-run.state := ready;
next-to-run.priority := Ceiling(scb);
insert(next-to-run, ready-queue);
save-context();
schedule(); /* dispatch invoked*/
load-context();
end then
else scb.counter ++1;
end else
Enable-interrupt
31
32
16
Summary
33
34
17
Example: PCP
A: ...P(S1)...V(S1)... Prio(A)=H
B: ...P(S2)...P(S3)...V(S3)...V(S2)... Prio(B)=M C(S1)=H
C: ...P(S3)...P(S2)...V(S2)...V(S3) Prio(C)=L C(S2)=C(S3)=M
P(S1) V(S1)
36
18
Exercise: implementation of PCP
37
38
19
Summary
39
20