ToCS_lecture15_Problems of management of

Transkrypt

ToCS_lecture15_Problems of management of
Program mentorski receptą na efektywne kształcenie na makrokierunku automatyka
i robotyka, elektronika i telekomunikacja, informatyka na Politechnice Śląskiej
(Mentoring program - a recipe for efficient education at the Macrocourse on Automatic Control and Robotics, Electronics and
Telecommunication, and Computer Science offered by the Silesian University) of Technology)
POKL.04.01.02-00-209/11
Materiały dydaktyczne dla przedmiotu
„Podstawy Informatyki”
Teaching materials for lecture
„Theory of Computer Science”
Prezentacja współfinansowana przez Unię Europejską ze środków Europejskiego Funduszu Społecznego
Presentation co-financed by the European Union from the financial resources of the European Social Fund
Prezentacja dystrybuowana bezpłatnie
Presentation distributed free of charge
Management of Resources and
Synchronization
Theory of Computer Science
Instytut Informatyki, Politechnika Śląska, Gliwice
Deadlock
• Two processes and two resources
process1() {
…
printHeader();
plotData();
printData()
…
}
Instytut Informatyki, Politechnika Śląska, Gliwice
process2() {
…
plot1();
print();
plot2()
…
}
3
Deadlock
• Two processes and two resources
process1() {
…
P( printer );
printHeader();
P( plotter );
plotData();
V( plotter );
printData();
V( printer );
…
}
Instytut Informatyki, Politechnika Śląska, Gliwice
process2() {
…
P( plotter
plot1();
P( printer
print();
V( printer
plot2();
V( plotter
…
}
);
);
);
);
4
Deadlock
• Two processes and two resources
process1() {
…
P( printer );
printHeader();
P( plotter );
plotData();
V( plotter );
printData();
V( printer );
…
}
Instytut Informatyki, Politechnika Śląska, Gliwice
process2() {
…
P( plotter
plot1();
P( printer
print();
V( printer
plot2();
V( plotter
…
}
);
);
);
);
5
Deadlock characterization
•
•
•
•
Mutual exclusion
Hold and wait
No preemption
Circular wait
Instytut Informatyki, Politechnika Śląska, Gliwice
6
Handling deadlocks
• Prevention and avoidance
• Detection and recovering
• Ignoring the problem
Instytut Informatyki, Politechnika Śląska, Gliwice
7
Deadlock prevention
Deadlock can arise only if four conditions hold
simultaneously
• Mutual exlusion – must hold for non-sharable
resources
• Hold and wait – must guarantee that whenever
a process requests a resource, it does not hold
any other resources
– Require process to request and be allocated all its
resources before its begins execution, or allow
process to request resources only when the process
has none (low resource utilization)
Instytut Informatyki, Politechnika Śląska, Gliwice
8
Deadlock prevention - cont.
• No preemption
– If a process that is holding some resources requests
another resource that cannot be immediately
allocated to it, then all resources currently beeing
held are released
– Preemted resources are added to the list of resources
for which the process is waiting
• Circular wait
– Impose a total ordering of all resource types, and
require that each process requests resources in an
increasing order of enumeration
Instytut Informatyki, Politechnika Śląska, Gliwice
9
Deadlock avoidance
• Requires that the system has some additional a
priori information available
– Simples and most useful model requires that each
process declare the maximum number of resources
of each type that it may need
– The deadlock avoidance dynamically examines the
resource-allocation state to ensure that there can
never be a circullar-wait condition
– Resource allocation state is defined by the number of
available and allocated resources and the maximum
demands of the processes
Instytut Informatyki, Politechnika Śląska, Gliwice
10
Stages of the process
• Resource allocation or release means next stage
process1() {
…
P( printer );
printHeader();
P( plotter );
plotData();
V( plotter );
printData();
V( printer );
…
}
Instytut Informatyki, Politechnika Śląska, Gliwice
process2() {
…
P( plotter
plot1();
P( printer
print();
V( printer
plot2();
V( plotter
…
}
);
);
);
);
11
Vector of allocated resources
• Resource allocation or release means next stage
process1() {
0 …
0 P( printer );
printHeader();
P( plotter );
plotData();
V( plotter );
printData();
V( printer );
…
}
Instytut Informatyki, Politechnika Śląska, Gliwice
process2() {
…
P( plotter
plot1();
P( printer
print();
V( printer
plot2();
V( plotter
…
}
);
);
);
);
12
Vector of allocated resources
• Resource allocation or release means next stage
process1() {
0 …
0 P( printer );
1 printHeader();
0 P( plotter );
plotData();
V( plotter );
printData();
V( printer );
…
}
Instytut Informatyki, Politechnika Śląska, Gliwice
process2() {
…
P( plotter
plot1();
P( printer
print();
V( printer
plot2();
V( plotter
…
}
);
);
);
);
13
Vector of allocated resources
• Resource allocation or release means next stage
process1() {
0 …
0 P( printer );
1 printHeader();
0 P( plotter );
1 plotData();
1 V( plotter );
printData();
V( printer );
…
}
Instytut Informatyki, Politechnika Śląska, Gliwice
process2() {
…
P( plotter
plot1();
P( printer
print();
V( printer
plot2();
V( plotter
…
}
);
);
);
);
14
Vector of allocated resources
• Resource allocation or release means next stage
process1() {
0 …
0 P( printer );
1 printHeader();
0 P( plotter );
1 plotData();
1 V( plotter );
1 printData();
0 V( printer );
…
}
Instytut Informatyki, Politechnika Śląska, Gliwice
process2() {
…
P( plotter
plot1();
P( printer
print();
V( printer
plot2();
V( plotter
…
}
);
);
);
);
15
Vector of allocated resources
• Resource allocation or release means next stage
process1() {
0 …
0 P( printer );
1 printHeader();
0 P( plotter );
1 plotData();
1 V( plotter );
1 printData();
0 V( printer );
0 …
0}
Instytut Informatyki, Politechnika Śląska, Gliwice
process2() {
…
P( plotter
plot1();
P( printer
print();
V( printer
plot2();
V( plotter
…
}
);
);
);
);
16
Vector of demands
• Resource allocation or release means next stage
process1() {
1 …
1 P( printer );
1 printHeader();
1 P( plotter );
1 plotData();
1 V( plotter );
1 printData();
0 V( printer );
0 …
0}
Instytut Informatyki, Politechnika Śląska, Gliwice
process2() {
…
P( plotter
plot1();
P( printer
print();
V( printer
plot2();
V( plotter
…
}
);
);
);
);
17
Current state of system
• Notation
– X – all non-sharable resources available in the
operating system
– Ai – resources currently allocated to process i
– Bi – current vector of demands of process i
– R – the reserve of the system
Instytut Informatyki, Politechnika Śląska, Gliwice
18
Possible state of the system
• Must be met three conditions
Instytut Informatyki, Politechnika Śląska, Gliwice
19
Safe state
• System is in safe state if there exists a
sequence of all the processes, such that for
each process Pi, the resources that Pi can still
request can be satisfied by currently available
resources and resources held by all the
processes Pj terminated before process Pi
Instytut Informatyki, Politechnika Śląska, Gliwice
20
Example
process1() {
…
P( printer );
printHeader();
P( plotter );
plotData();
V( plotter );
printData();
V( printer );
…
}
Instytut Informatyki, Politechnika Śląska, Gliwice
process2() {
…
P( plotter
plot1();
P( printer
print();
V( printer
plot2();
V( plotter
…
}
);
);
);
);
21
Example
X = | 1, 1 |
R = | 0, 0 |
A1 = | 1, 0 |
B1 = | 1, 1 |
Instytut Informatyki, Politechnika Śląska, Gliwice
A2 = | 0, 1 |
B2 = | 1, 1 |
22
Example
X = | 1, 1 |
R = | 0, 0 |
A1 = | 1, 0 |
B1 = | 1, 1 |
A2 = | 0, 1 |
B2 = | 1, 1 |
B1 = | 1, 1 | ≤ X
B2 = | 1, 1 | ≤ X
A1 = | 1, 0 | ≤ B1
A2 = | 0, 1 | ≤ B2
A1 + A2 = | 1, 0 | + | 0, 1 | = | 1, 1 | A1 ≤ X
Instytut Informatyki, Politechnika Śląska, Gliwice
23
Example
X = | 1, 1 |
R = | 0, 0 |
P1, P2:
B1 – A1
B1 – A1
P2, P1:
B2 – A2
B2 – A2
A1 = | 1, 0 |
B1 = | 1, 1 |
A2 = | 0, 1 |
B2 = | 1, 1 |
≤R
= | 1, 1 | - | 1, 0 | = | 0, 1 | ≤ R
≤R
= | 1, 1 | - | 0, 1 | = | 1, 0 | ≤ R
Instytut Informatyki, Politechnika Śląska, Gliwice
24
Example
process1() {
…
P( printer );
printHeader();
P( plotter );
plotData();
V( plotter );
printData();
V( printer );
…
}
Instytut Informatyki, Politechnika Śląska, Gliwice
process2() {
…
P( plotter
plot1();
P( printer
print();
V( printer
plot2();
V( plotter
…
}
);
);
);
);
25
Example
X = | 1, 1 |
R = | 0, 0 |
P1, P2:
B1 – A1 ≤
B1 – A1 =
P2, P1:
B2 – A2 ≤
B2 – A2 =
B1 – A1 ≤
B1 – A1 =
A1 = | 1, 0 |
B1 = | 1, 1 |
A2 = | 0, 1 |
B2 = | 0, 1 |
R
| 1, 1 | - | 1, 0 | = | 0, 1 | ≤ R
R
| 0, 1 | - | 0, 1 | = | 0, 0 | ≤ R
R + A2
| 1, 1 | - | 1, 0 | = | 0, 1 | ≤ R + A2
Instytut Informatyki, Politechnika Śląska, Gliwice
26
Phase plane of processes
Plotter
Printer
P2
Printer
Plotter
Instytut Informatyki, Politechnika Śląska, Gliwice
P1
27
Phase plane of processes
Plotter
Printer
P2
Printer
Plotter
Instytut Informatyki, Politechnika Śląska, Gliwice
P1
28
Phase plane of processes
Plotter
Printer
P2
Printer
Plotter
Instytut Informatyki, Politechnika Śląska, Gliwice
P1
29
Phase plane of processes
Plotter
Printer
P2
Printer
Plotter
Instytut Informatyki, Politechnika Śląska, Gliwice
P1
30
Phase plane of processes
Plotter
Printer
P2
Printer
Plotter
Instytut Informatyki, Politechnika Śląska, Gliwice
P1
31
Phase plane of processes
Plotter
Printer
P2
Printer
Plotter
Instytut Informatyki, Politechnika Śląska, Gliwice
P1
32
Phase plane of processes
Plotter
Printer
P2
Printer
Plotter
Instytut Informatyki, Politechnika Śląska, Gliwice
P1
33
Phase plane of processes
Plotter
Printer
P2
Printer
Plotter
Instytut Informatyki, Politechnika Śląska, Gliwice
P1
34
Phase plane of processes
Plotter
Printer
P2
Printer
Plotter
Instytut Informatyki, Politechnika Śląska, Gliwice
P1
35
Phase plane of processes
Plotter
Printer
P2
Printer
Plotter
Instytut Informatyki, Politechnika Śląska, Gliwice
P1
36
Phase plane of processes
Plotter
Printer
P2
Printer
Plotter
Instytut Informatyki, Politechnika Śląska, Gliwice
P1
37
Phase plane of processes
Plotter
Printer
P2
Printer
Plotter
Instytut Informatyki, Politechnika Śląska, Gliwice
P1
38
Phase plane of processes
Plotter
Printer
P2
Printer
Plotter
Instytut Informatyki, Politechnika Śląska, Gliwice
P1
39
Deadlock avoidance
• Each process declare the maximum number of
resources of each type that it may need
• The deadlock-avoidance algorithm dynamically
examines the resource-allocation state to
ensure that there can never be a unsafe state
• When a process requests a resource it may
have to wait
Instytut Informatyki, Politechnika Śląska, Gliwice
40
Producer – consumer
producer() {
do {
generate();
put();
}
while( … );
}
Instytut Informatyki, Politechnika Śląska, Gliwice
consumer() {
do {
get();
consume();
}
while( … );
}
41
Producer – consumer –
solution
producer() {
do {
generate();
P( free );
put();
V( occ );
}
while( … );
}
consumer() {
do {
P( occ );
get();
V( free );
consume();
}
while( … );
}
e0( occ ) = 0
e0( free ) = buffersize
Instytut Informatyki, Politechnika Śląska, Gliwice
42
Producer – consumer –
solution
producer() {
do {
generate();
P( free );
P( buf );
put();
V( occ );
V( buf );
}
while( … );
}
Instytut Informatyki, Politechnika Śląska, Gliwice
consumer() {
do {
P( occ );
P( buf );
get();
V( free );
V( buf );
consume();
}
while( … );
}
43
Management of Resources and
Synchronization
Theory of Computer Science
Instytut Informatyki, Politechnika Śląska, Gliwice
Program mentorski receptą na efektywne kształcenie na makrokierunku automatyka
i robotyka, elektronika i telekomunikacja, informatyka na Politechnice Śląskiej
(Mentoring program - a recipe for efficient education at the Macrocourse on Automatic Control and Robotics, Electronics and
Telecommunication, and Computer Science offered by the Silesian University) of Technology)
POKL.04.01.02-00-209/11
Materiały dydaktyczne dla przedmiotu
„Podstawy Informatyki”
Teaching materials for lecture
„Theory of Computer Science”
Prezentacja współfinansowana przez Unię Europejską ze środków Europejskiego Funduszu Społecznego
Presentation co-financed by the European Union from the financial resources of the European Social Fund
Prezentacja dystrybuowana bezpłatnie
Presentation distributed free of charge

Podobne dokumenty