Professional Documents
Culture Documents
Critical sections
1 2
global
wait(s) memory buffer wait(s)
write read
critical x = 3; int x; a = x+1;
Problems caused by section y = 5; int y; b = y+2; critical
section
mutual exclusion signal(s) c = x+y;
signal(s)
priority priority B
1 1
2 2
3 3
7 8
2 Solution
Introduce a concurrency control protocol for
3 accessing critical sections.
9 10
Classical semaphores (No protocol) Critical sections are correctly accessed by tasks:
13 14
15 16
PROBLEMS: 3
1. Tasks may block even if they do not use resources.
2. Since tasks are blocked at activation, blocking could be Priority assigned to i
pi = Pmax = max(P1, …, Pn)
unnecessary (pessimistic assumption). inside critical sections:
19 20
1 2 1 blocks just in case ... Access Rule: A task never blocks at the entrance of a
critical section, but at its activation time.
21 22
P2 wait(S): pi = C(S)
P3 signal(S): p i = Pi
Priority assigned to i
inside a resource R: pi(R) = max { Pj | j uses R } Note: HLP is also known as Immediate Priority Ceiling (IPC).
23 24
10/29/2019
Direct blocking
priority
direct blocking A task blocks on a locked semaphore
1
Indirect blocking (Push-through blocking)
push-through blocking
2 A task blocks because a lower priority task inherited
a higher priority.
3 inherits the priority of 1
3
BLOCKING:
P1 a delay caused by lower priority tasks
P3
27 28
signal(s): if (there are blocked tasks) { 2. Sk is shared between tasks with priority lower
<awake the highest priority task in the semaphore queue> than i and tasks having priority higher than i
pexe = Pexe
<call the scheduler>
(push-through blocking).
}
else s = 1; 29 30
10/29/2019
Lemma 1: A task i can be blocked at most Lemma 2: A task i can be blocked at most
once by a lower priority task. once on a semaphore Sk.
If there are ni tasks with priority lower than i, If there are mi distinct semaphores that can block
then i can be blocked at most at most ni times, a task i, then i can be blocked at most mi times,
independently of the number of critical sections independently of the number of critical sections
that can block i. that can block i.
31 32
critical sections. 3 B D
ni = number of tasks with priority less than i 1 can be blocked once by 2 (on A2 or C2) and
once by 3 (on B3 or D3)
mi = number of semaphores that can block i
2 can be blocked once by 3 (on B3 or D3)
(either directly or indirectly).
3 cannot be blocked
33 34
1 A B C D
ik worst-case duration of Zik
i set of the longest critical sections used by i for each
P1
semaphore Sk: i = { Zik | Sk used by i }
2 A C
ij set of critical sections used by j that can block i
3 B D
i set of critical sections that can block i
i maximum number of critical sections that can block i
Bi worst-case blocking time for i
35 36
10/29/2019
n
C.S. that can block i i ij
j i 1
C.S. that can block i i ij
j i 1
37 38
3. Compute i IN 1 B 4 OUT
Example 2 Identification of 1
1 A B C Ci Ti A B C D E
3 4 5
B1 1 15 60 3 4 5
2 A A B D From the task code 2 30 100 6 11 5
3 6 11 5 we can derive the
3 C E following table: 3 20 150 10 8
10 8 4 40 200 12 14 10
4 B D E
12 14 10
1 = {A2, B2, C3, B4}
Ci Ti A B C D E
1 15 60 3 4 5 1 can only experience direct blocking because it is
2 30 100 6 11 5 the highest priority task.
3 20 150 10 8
4 40 200 12 14 10
41 42
10/29/2019
Identification of 2 Identification of 3
Ci Ti A B C D E Ci Ti A B C D E
1 15 60 3 4 5 1 15 60 3 4 5
B2 2 30 100 6 11 5 2 30 100 6 11 5
3 20 150 10 8 B3 3 20 150 10 8
4 40 200 12 14 10 4 40 200 12 14 10
Identification of 4 Identification of i
Ci Ti A B C D E Ci Ti A B C D E i ni mi i
1 15 60 3 4 5 1 15 60 3 4 5 {A2, B2, C3, B4} 3 3 3
2 30 100 6 11 5 2 30 100 6 11 5 {C3, B4, D4} 2 3 2
3 20 150 10 8 3 20 150 10 8 {B4, D4, E4} 1 3 1
B4 4 40 200 12 14 10 4 40 200 12 14 10 {} 0 0 0
NOTES
PROBLEMS:
For 1, if we select B2, we cannot select B4, because
More complex to implement (especially to support
each semaphore can block only once (Lemma 2).
nested critical sections).
For 2, we cannot select B4 and D4, because each task
It is prone to chained blocking.
can block only once (Lemma 1).
It does not avoid deadlocks.
47 48
10/29/2019
NOTE: 1 can be blocked at most once NOTE: PCP can be viewed as PIP + access test
for each lower priority task.
49 50
2 C
C(sk) = max { Pi | i uses sk }
3 B
Then a task i can enter a critical section only if its
priority is higher than the maximum ceiling of the
4 A locked semaphores:
ceiling blocking
2 B
Theorem 2
t1
Theorem 3
t1: 2 is blocked by the PCP, since P2 < C(sA) PCP prevents deadlocks.
53 54
10/29/2019
55 56
59 60
10/29/2019
preemption i 1
Ri
by HP tasks Ri Ci Bi Ck
i k 1 Tk
blocking by
LP tasks Iterative solution:
iterate while
i 1
Ck C Bi Ri0 Ci Bi
i 1 i 1 2 i 1 ( s 1)
Ris Ri( s 1)
k 1 Tk Ti R
Ri( s ) Ci Bi i
Ck
k 1 Tk
61 62
63 64
67 68
69 70
RB 2 2 15 2 1 1 1 A
3 20 1 0 2 2 A B
RA 1
0 3 3 3 s 2
3
RB - 0 1 2
3 B B B
2 A
P1 p3
P2
P3
3 B B
t*
This is not possible, because 2 could not preempt 3
A task is blocked when accessing a resource because, at time t*, 2 < s
73 74
Proof
Since s = max{CR(nk)}, then there are enough Proof
resources to satisfy the requirements of i and those From Theorem 2, if a task can never block once
of all tasks that can preempt i . started, then no deadlock can occur.
Question
If a task can never block once started, can we get
rid of the wait / signal primitives?
75 76
1 > 2 When Di = Ti
1 2 A task set is schedulable if
blocked by SRP
1 i 1
Ck Ci Bi
A B i T
Ti
1
2 k 1 k
B A
s
1 Bi can be computed as under PCP and refers to the
2 length of longest critical section that can block i.
77 78
10/29/2019
PUSH
where: B(L) = max { jh | (Dj > L) and (Dh L) } stack pointer
n
Dmax = max (D1, …, Dn) (T D )U
i i i
stack
H = lcm(T1, …, Tn) L* i 1
1U 79 80
big problems
blocked SP1
SP1 1 SP2
1
SP2
SP2 2
2
stack
stack
81 82
83 84