Beruflich Dokumente
Kultur Dokumente
17 May 2017
Motivation
Threads with priorities are used in a broad range of software systems from cars to
spacecrafts:
Motivation
Threads with priorities are used in a broad range of software systems from cars to
spacecrafts:
Important
Many software systems use threads with priorities to handle tasks
with different execution urgency.
Motivation
Threads with priorities are used in a broad range of software systems from cars to
spacecrafts:
Important
Many software systems use threads with priorities to handle tasks
with different execution urgency.
Example
In a car the bracket subsystem should have higher priority than the
music subsystem to access resources.
Modeling Multithreaded Programs with Priorities
Definition
A Dynamic Pushdown Network with Priorities (P-DPN) is a
tuple M = (P, , , ), where:
I P is a finite set of control locations,
DPN with Priorities (P-DPN): Model Definition
Definition
A Dynamic Pushdown Network with Priorities (P-DPN) is a
tuple M = (P, , , ), where:
I P is a finite set of control locations,
I is a finite stack alphabet,
DPN with Priorities (P-DPN): Model Definition
Definition
A Dynamic Pushdown Network with Priorities (P-DPN) is a
tuple M = (P, , , ), where:
I P is a finite set of control locations,
I is a finite stack alphabet,
I : P I is a function from control locations to a finite set
of natural numbers I representing priorities,
DPN with Priorities (P-DPN): Model Definition
Definition
A Dynamic Pushdown Network with Priorities (P-DPN) is a
tuple M = (P, , , ), where:
I P is a finite set of control locations,
I is a finite stack alphabet,
I : P I is a function from control locations to a finite set
of natural numbers I representing priorities,
I is a finite set of:
DPN with Priorities (P-DPN): Model Definition
Definition
A Dynamic Pushdown Network with Priorities (P-DPN) is a
tuple M = (P, , , ), where:
I P is a finite set of control locations,
I is a finite stack alphabet,
I : P I is a function from control locations to a finite set
of natural numbers I representing priorities,
I is a finite set of:
I non-spawning rules: p , qw ,
DPN with Priorities (P-DPN): Model Definition
Definition
A Dynamic Pushdown Network with Priorities (P-DPN) is a
tuple M = (P, , , ), where:
I P is a finite set of control locations,
I is a finite stack alphabet,
I : P I is a function from control locations to a finite set
of natural numbers I representing priorities,
I is a finite set of:
I non-spawning rules: p , qw ,
I spawning rules p , q1 w1 B q2 w2 .
DPN with Priorities (P-DPN): Model Definition
Definition
A Dynamic Pushdown Network with Priorities (P-DPN) is a
tuple M = (P, , , ), where:
I P is a finite set of control locations,
I is a finite stack alphabet,
I : P I is a function from control locations to a finite set
of natural numbers I representing priorities,
I is a finite set of:
I non-spawning rules: p , qw ,
I spawning rules p , q1 w1 B q2 w2 .
where p, q1 , q2 P, and , 1 , 2 .
DPN with Priorities (P-DPN): Model Definition
Definition
A Dynamic Pushdown Network with Priorities (P-DPN) is a
tuple M = (P, , , ), where:
I P is a finite set of control locations,
I is a finite stack alphabet,
I : P I is a function from control locations to a finite set
of natural numbers I representing priorities,
I is a finite set of:
I non-spawning rules: p , qw ,
I spawning rules p , q1 w1 B q2 w2 .
where p, q1 , q2 P, and , 1 , 2 .
A Dynamic Pushdown Network (DPN) can be seen as a P-DPN
(P, , , 0 ) such that p P, 0 (p) = 0.
DPN with Priorities (P-DPN): Semantics
Definition
We overload to global configurations p1 w1 . . . pn wn (P ) ,
(p1 w1 . . . pn wn ) := max((p1 ), . . . , (pn )).
DPN with Priorities (P-DPN): Semantics
Definition
We overload to global configurations p1 w1 . . . pn wn (P ) ,
(p1 w1 . . . pn wn ) := max((p1 ), . . . , (pn )).
Definition
The transition relation M of a P-DPN M is defined as the
smallest relation in ConfM ConfM such that c1 , c2 ConfM :
DPN with Priorities (P-DPN): Semantics
Definition
We overload to global configurations p1 w1 . . . pn wn (P ) ,
(p1 w1 . . . pn wn ) := max((p1 ), . . . , (pn )).
Definition
The transition relation M of a P-DPN M is defined as the
smallest relation in ConfM ConfM such that c1 , c2 ConfM :
1. c1 pr c2 M c1 qr c2 ,
if (c1 pr c2 ) = (p) and p , q ;
DPN with Priorities (P-DPN): Semantics
Definition
We overload to global configurations p1 w1 . . . pn wn (P ) ,
(p1 w1 . . . pn wn ) := max((p1 ), . . . , (pn )).
Definition
The transition relation M of a P-DPN M is defined as the
smallest relation in ConfM ConfM such that c1 , c2 ConfM :
1. c1 pr c2 M c1 qr c2 ,
if (c1 pr c2 ) = (p) and p , q ;
2. c1 pr c2 M c1 q2 2 q1 1 r c2 ,
if (c1 pr c2 ) = (p) and p , q1 1 B q2 2 ;
DPN with Priorities (P-DPN): Semantics
Definition
We overload to global configurations p1 w1 . . . pn wn (P ) ,
(p1 w1 . . . pn wn ) := max((p1 ), . . . , (pn )).
Definition
The transition relation M of a P-DPN M is defined as the
smallest relation in ConfM ConfM such that c1 , c2 ConfM :
1. c1 pr c2 M c1 qr c2 ,
if (c1 pr c2 ) = (p) and p , q ;
2. c1 pr c2 M c1 q2 2 q1 1 r c2 ,
if (c1 pr c2 ) = (p) and p , q1 1 B q2 2 ;
where p, q, q1 , q2 P, , , 1 , 2 , r .
We denote as M the transitive-reflexive closure of M .
Example of Multithreaded Program with Priorities
1 int x = 0;
2 spin_lock l ;
3
4 void main () { I The starting thread main dynamically
5 // Priority 1.
6 thread_create (A ,1); creates two threads: A and B.
7 thread_create (B ,2);
8 }
9
10 void A () {
11 // Priority 1.
12 spinlock_lock ( l );
13 x ++;
14 s pi nl oc k _unlock ( l );
15 }
16
17 void B () {
18 // Priority 2.
19 spinlock_lock ( l );
20 int tmp = x ;
21 assert ( tmp == x );
22 s pi nl oc k _unlock ( l );
23 }
Example of Multithreaded Program with Priorities
1 int x = 0;
2 spin_lock l ;
3
4 void main () { I The starting thread main dynamically
5 // Priority 1.
6 thread_create (A ,1); creates two threads: A and B.
7 thread_create (B ,2);
8
9
} I Thread A has priority of 1 and thread B
10
11
void A () {
// Priority 1.
has priority of 2.
12 spinlock_lock ( l );
13 x ++;
14 s pi nl oc k _unlock ( l );
15 }
16
17 void B () {
18 // Priority 2.
19 spinlock_lock ( l );
20 int tmp = x ;
21 assert ( tmp == x );
22 s pi nl oc k _unlock ( l );
23 }
Example of Multithreaded Program with Priorities
1 int x = 0;
2 spin_lock l ;
3
4 void main () { I The starting thread main dynamically
5 // Priority 1.
6 thread_create (A ,1); creates two threads: A and B.
7 thread_create (B ,2);
8
9
} I Thread A has priority of 1 and thread B
10
11
void A () {
// Priority 1.
has priority of 2.
12 spinlock_lock ( l );
13 x ++;
I Thread A cannot interrupt thread B, once
14 s pi nl oc k _unlock ( l );
15 } B it is created.
16
17 void B () {
18 // Priority 2.
19 spinlock_lock ( l );
20 int tmp = x ;
21 assert ( tmp == x );
22 s pi nl oc k _unlock ( l );
23 }
Example of Multithreaded Program with Priorities
1 int x = 0;
2 spin_lock l ;
3
4 void main () { I The starting thread main dynamically
5 // Priority 1.
6 thread_create (A ,1); creates two threads: A and B.
7 thread_create (B ,2);
8
9
} I Thread A has priority of 1 and thread B
10
11
void A () {
// Priority 1.
has priority of 2.
12 spinlock_lock ( l );
13 x ++;
I Thread A cannot interrupt thread B, once
14 s pi nl oc k _unlock ( l );
15 } B it is created.
16
17 void B () { I A thread attempting to acquire a spinlock
18 // Priority 2.
19 spinlock_lock ( l ); does not sleep if it cannot acquire it.
20 int tmp = x ;
21 assert ( tmp == x );
22 s pi nl oc k _unlock ( l );
23 }
Example of Multithreaded Program with Priorities
1 int x = 0;
2 spin_lock l ;
3
4 void main () { I The starting thread main dynamically
5 // Priority 1.
6 thread_create (A ,1); creates two threads: A and B.
7 thread_create (B ,2);
8
9
} I Thread A has priority of 1 and thread B
10
11
void A () {
// Priority 1.
has priority of 2.
12 spinlock_lock ( l );
13 x ++;
I Thread A cannot interrupt thread B, once
14 s pi nl oc k _unlock ( l );
15 } B it is created.
16
17 void B () { I A thread attempting to acquire a spinlock
18 // Priority 2.
19 spinlock_lock ( l ); does not sleep if it cannot acquire it.
20 int tmp = x ;
21
22
assert ( tmp == x );
s pi nl oc k _unlock ( l );
I Then there is a deadlock in this code.
23 }
Modeling the previous example with P-DPN
1 int x = 0;
2 spin_lock l ;
3 I The set of control locations is
4 void main () {
5 // Priority 1. P = {p0 , p1 , p2 }, one for each priority.
6 thread_create (A ,1);
7 thread_create (B ,2);
8 }
9
10 void A () {
11 // Priority 1.
12 spinlock_lock ( l );
13 x ++;
14 s pi nl oc k _unlock ( l );
15 }
16
17 void B () {
18 // Priority 2.
19 spinlock_lock ( l );
20 int tmp = x ;
21 assert ( tmp == x );
22 s pi nl oc k _unlock ( l );
23 }
Modeling the previous example with P-DPN
1 int x = 0;
2 spin_lock l ;
3 I The set of control locations is
4 void main () {
5 // Priority 1. P = {p0 , p1 , p2 }, one for each priority.
6 thread_create (A ,1);
7
8 }
thread_create (B ,2); I The stack alphabet is
9
10 void A () {
= {m0 , m1 , a0 , . . . , a2 , b0 , . . . , b3 }.
11 // Priority 1.
12 spinlock_lock ( l );
13 x ++;
14 s pi nl oc k _unlock ( l );
15 }
16
17 void B () {
18 // Priority 2.
19 spinlock_lock ( l );
20 int tmp = x ;
21 assert ( tmp == x );
22 s pi nl oc k _unlock ( l );
23 }
Modeling the previous example with P-DPN
1 int x = 0;
2 spin_lock l ;
3 I The set of control locations is
4 void main () {
5 // Priority 1. P = {p0 , p1 , p2 }, one for each priority.
6 thread_create (A ,1);
7
8 }
thread_create (B ,2); I The stack alphabet is
9
10 void A () {
= {m0 , m1 , a0 , . . . , a2 , b0 , . . . , b3 }.
11 // Priority 1.
12 spinlock_lock ( l );
I The transitions rules are = {
13 x ++;
14 s pi nl oc k _unlock ( l ); p1 m0 , p1 m1 B p1 a0 , p1 m1 , p0 B p2 b0 ,
15 }
16 p1 a0 , p1 a1 , p1 a1 , p1 a2 , p1 a2 , p0 ,
17 void B () {
18 // Priority 2.
p2 b0 , p2 b1 , p2 b1 , p2 b2 ,
19
20
spinlock_lock ( l );
int tmp = x ;
p2 b2 , p2 b3 , p2 b3 , p0 }.
21 assert ( tmp == x );
22 s pi nl oc k _unlock ( l );
23 }
Modeling the previous example with P-DPN
1 int x = 0;
2 spin_lock l ;
3 I The set of control locations is
4 void main () {
5 // Priority 1. P = {p0 , p1 , p2 }, one for each priority.
6 thread_create (A ,1);
7
8 }
thread_create (B ,2); I The stack alphabet is
9
10 void A () {
= {m0 , m1 , a0 , . . . , a2 , b0 , . . . , b3 }.
11 // Priority 1.
12 spinlock_lock ( l );
I The transitions rules are = {
13 x ++;
14 s pi nl oc k _unlock ( l ); p1 m0 , p1 m1 B p1 a0 , p1 m1 , p0 B p2 b0 ,
15 }
16 p1 a0 , p1 a1 , p1 a1 , p1 a2 , p1 a2 , p0 ,
17 void B () {
18 // Priority 2.
p2 b0 , p2 b1 , p2 b1 , p2 b2 ,
19
20
spinlock_lock ( l );
int tmp = x ;
p2 b2 , p2 b3 , p2 b3 , p0 }.
21 assert ( tmp == x );
22 s pi nl oc k _unlock ( l ); I For all pi P, (pi ) = i.
23 }
Execution tree of a P-DPN
p1 a1 p2 b0 p0
p1 a2 p2 b1
p0 p2 b2
p2 b3
p0
Priority Structures: Idea
p1 m0
Why is the configuration p0 p0 p0 reachable?
p1 a0 p1 m1 I Because the lowest priority of the
transitions of thread A (xa ) is lower than
p1 a1 p2 b0 p0 the lowest priority of the transitions of
thread B (xb ) allowing thread B to
p1 a2 p2 b1 execute until the end, and
p0 p2 b2
p2 b3
p0
Priority Structures: Idea
p1 m0
Why is the configuration p0 p0 p0 reachable?
p1 a0 p1 m1 I Because the lowest priority of the
transitions of thread A (xa ) is lower than
p1 a1 p2 b0 p0 the lowest priority of the transitions of
thread B (xb ) allowing thread B to
p1 a2 p2 b1 execute until the end, and
I Because the last priority of thread B (yb )
p0 p2 b2 is lower than (xa ) allowing thread A to
execute until the end.
p2 b3
p0
Priority Structures: Idea
p1 m0
Why is the configuration p0 p0 p0 reachable?
p1 a0 p1 m1 I Because the lowest priority of the
transitions of thread A (xa ) is lower than
p1 a1 p2 b0 p0 the lowest priority of the transitions of
thread B (xb ) allowing thread B to
p1 a2 p2 b1 execute until the end, and
I Because the last priority of thread B (yb )
p0 p2 b2 is lower than (xa ) allowing thread A to
execute until the end.
p2 b3 Because yb xa xb .
p0
Priority Structures: Idea
p1 m0
Why is the configuration p0 p0 p0 reachable?
p1 a0 p1 m1 I Because the lowest priority of the
transitions of thread A (xa ) is lower than
p1 a1 p2 b0 p0 the lowest priority of the transitions of
thread B (xb ) allowing thread B to
p1 a2 p2 b1 execute until the end, and
I Because the last priority of thread B (yb )
p0 p2 b2 is lower than (xa ) allowing thread A to
execute until the end.
p2 b3 Because yb xa xb .
p0
Lemma
Two threads A and B can be scheduled together until the end iff
yb xa xb ya xb xa .
Priority Structures: Computation Algorithm
s1 := , s2 := .
Priority Structures: Computation Algorithm
s1 := , s2 := .
s1 s2 := Jmin(x1 , x2 ), max(y1 , y2 )K if
(y2 x1 x1 x2 ) (y1 x2 x2 x1 ), otherwise .
Priority Structures: Computation Example
p1 m0
p1 a0 p1 m1
p1 a1 p2 b0 p0
p1 a2 p2 b1
p0 p2 b2
p2 b3
p0
Priority Structures: Computation Example
I We start from the leaves,
setting the lowest
p1 m0 transition priority to
and the highest last
p1 a0 p1 m1 priority to the priority of
the control state.
p1 a1 p2 b0 p0 , J, 0K
p1 a2 p2 b1
p0 , J, 0K p2 b2
p2 b3
p0 , J, 0K
Priority Structures: Computation Example
I We start from the leaves,
setting the lowest
p1 m0 transition priority to
and the highest last
p1 a0 p1 m 1 priority to the priority of
the control state.
p1 a1 p2 b0 p0 , J, 0K I Then move to the roots
updating the lowest
p1 , J1, 0Ka2 p2 b1 transition priority and
propagating the highest
p0 , J, 0K p2 b2 last priority.
p2 , J2, 0Kb3
p0 , J, 0K
Priority Structures: Computation Example
I We start from the leaves,
setting the lowest
p1 m0 transition priority to
and the highest last
p1 a0 p1 m 1 priority to the priority of
the control state.
p1 , J1, 0Ka1 p2 b0 p0 , J, 0K I Then move to the roots
updating the lowest
p1 , J1, 0Ka2 p2 b1 transition priority and
propagating the highest
p0 , J, 0K p2 , J2, 0Kb2 last priority.
p2 , J2, 0Kb3
p0 , J, 0K
Priority Structures: Computation Example
I We start from the leaves,
setting the lowest
p1 m0 transition priority to
and the highest last
p1 , J1, 0Ka0 p1 m 1 priority to the priority of
the control state.
p1 , J1, 0Ka1 p2 b0 p0 , J, 0K I Then move to the roots
updating the lowest
p1 , J1, 0Ka2 p2 , J2, 0Kb1 transition priority and
propagating the highest
p0 , J, 0K p2 , J2, 0Kb2 last priority.
p2 , J2, 0Kb3
p0 , J, 0K
Priority Structures: Computation Example
I We start from the leaves,
setting the lowest
p1 m0 transition priority to
and the highest last
p1 , J1, 0Ka0 p1 m 1 priority to the priority of
the control state.
p1 , J1, 0Ka1 p2 , J2, 0Kb0 p0 , J, 0K I Then move to the roots
updating the lowest
p1 , J1, 0Ka2 p2 , J2, 0Kb1 transition priority and
propagating the highest
p0 , J, 0K p2 , J2, 0Kb2 last priority.
p2 , J2, 0Kb3
p0 , J, 0K
Priority Structures: Computation Example
I We start from the leaves,
setting the lowest
p1 m0 transition priority to
and the highest last
p1 , J1, 0Ka0 p1 , J2, 0Km1 priority to the priority of
the control state.
p1 , J1, 0Ka1 p2 , J2, 0Kb0 p0 , J, 0K I Then move to the roots
updating the lowest
p1 , J1, 0Ka2 p2 , J2, 0Kb1 transition priority and
propagating the highest
p0 , J, 0K p2 , J2, 0Kb2 last priority.
I Then we compose the
p2 , J2, 0Kb3 priority structures of the
created subtree with the
p0 , J, 0K priority structure of the
rest of the father
execution.
Priority Structures: Computation Example
I We start from the leaves,
setting the lowest
p1 , J1, 0Km0 transition priority to
and the highest last
p1 , J1, 0Ka0 p1 , J2, 0Km1 priority to the priority of
the control state.
p1 , J1, 0Ka1 p2 , J2, 0Kb0 p0 , J, 0K I Then move to the roots
updating the lowest
p1 , J1, 0Ka2 p2 , J2, 0Kb1 transition priority and
propagating the highest
p0 , J, 0K p2 , J2, 0Kb2 last priority.
I Then we compose the
p2 , J2, 0Kb3 priority structures of the
created subtree with the
p0 , J, 0K priority structure of the
rest of the father
execution.
Computing Predecessor Sets (pre)
q7
)
,0
b3
,2
2
(p
(p0 , , 0) (p0 , , 0) (p0 , , 0)
start q0 q1 q2 q3 q4 q5
q7
)
,0
b3 , b2
,2
2
(p
(p0 , , 0) (p0 , , 0) (p0 , , 0)
start q0 q1 q2 q3 q4 q5
q7
)
,0
b3 , b2 , b1
,2
2
(p
(p0 , , 0) (p0 , , 0) (p0 , , 0)
start q0 q1 q2 q3 q4 q5
q7
)
,0
b3 , b2 , b1 , b0
,2
2
(p
(p0 , , 0) (p0 , , 0) (p0 , , 0)
start q0 q1 q2 q3 q4 q5
q6
)
,0
,1
a2
1
(p
q7
)
,0
b3 , b2 , b1 , b0
,2
2
(p
(p0 , , 0) (p0 , , 0) (p0 , , 0)
start q0 q1 q2 q3 q4 q5
q6
)
,0
a1
,1
a2 ,
1
(p
q7
)
,0
b3 , b2 , b1 , b0
,2
2
(p
(p0 , , 0) (p0 , , 0) (p0 , , 0)
start q0 q1 q2 q3 q4 q5
q6
a0
,0
,1
a1 ,
1
(p
a2 ,
q7
)
,0
b3 , b2 , b1 , b0
,2
2
(p
(p0 , , 0) (p0 , , 0) (p0 , , 0)
start q0 q1 q2 q3 q4 q5
q6
q8
)
a0
,0
,1
a1 ,
m
1
(p
a2 ,
1
)
q7
1, 0
1,
)
,0
(p b3 , b2 , b1 , b0
,2
2
(p
(p0 , , 0) (p0 , , 0) (p0 , , 0)
start q0 q1 q2 q3 q4 q5
q8 m
0
a0
,0
,1
a1 ,
m
1
(p
a2 ,
1
)
q7
1, 0
1,
)
,0
(p
b3 , b2 , b1 , b0
,2
2
(p
(p0 , , 0) (p0 , , 0) (p0 , , 0)
start q0 q1 q2 q3 q4 q5
q8 m
0
a0
,0
,1
a1 ,
m
1
(p
a2 ,
1
)
q7
1, 0
1,
)
,0
(p
b3 , b2 , b1 , b0
,2
2
(p
(p0 , , 0) (p0 , , 0) (p0 , , 0)
start q0 q1 q2 q3 q4 q5