1. Using A Petri Net Concepts To organize
The Resources Sharing Between Two
Processors
2. o How to control resources appeared.
o How to provide the system with the resources at a proper
time to avoid the system to be failed.
o What is a Deadlock.
The problem is :
3. • A Petri net PN, is a five tuple.
PN = (P, T, F, W, M0)
Where :
P = {P0,..,Pm} a finite set of places
T = {T0,..,Tn} a finite set of transition
F (P x T) U ( T x P) is a set of arcs (flow relation)
W is the weight function
M0 is the initial marking
P ∩ T = ø and P U T ≠ ø
What is Petri net
2
2
a place
a transition
an arc's weight
a token
an arc
4. The states of a system changes via
enabling and firing rules:
Transition t is enable under marking M
if each input place p of t is marked with at
least w(p,t) tokens,
where w(p,t) is the weight of the arc from p to
t.
An enable transition may fire.
firing of an enabled transition t
remove w(p,t) tokens from each input place
p of t, and
adds w(t,p’) tokens to each output place
Transition Enabling
& Firing
2
2
a place
a transition
an arc's weight
multiplicity 2
a token
an arc
2
2
a place
a transition
an arc's weight
a token
an arc
5. 1. Transitions are usually used to model events (activities) and operations which
change the states in places.
2. Places are used to model states after events have happened
Places can model conditions, resources or objects, like a program variable and
so on.
3. Operations after firing a transition are related with input/output arrows of its
transition.
PN Modeling
6. it is a process A waiting for a resource held by
process B that is also waiting for a resource held
by A.
What is A Deadlock
7. A Petri Net is deadlocked if no transition is enable.
Deadlock in Petri Net
11. Methods for Handling Deadlocks
o Ensure that the system will never enter a deadlock state.
o Allow the system to enter a deadlock state and then recover.
11
12. Deadlock Prevention
o Four conditions for Deadlock
o Mutual exclusion
o Hold and wait
o No preemption
o Circular wait
o Prevent Deadlock Ensure that at least one of these cannot hold
(any one is false)
o Prevention is better than cure
12
13. Deadlock Prevention
o Mutual Exclusion –This condition must hold for non-sharable
resources. Ex. Printer. Read only files No need to wait for access. To
make this false all resources should be sharable.
o Hold and Wait –Protocols, (No wait/ No Hold)
o Process must request all resources and be allocated. (No wait)
o Must guarantee that whenever a process requests a resource, it does
not hold any other resources. (No Hold)
13
14. Deadlock Prevention
o Ex. Copy data from Tape drive to disc, sorts the disk file and then
prints result to a printer.
o No wait: Request Tape drive, Disk file and printer.
o No Hold: Request Tape drive and disk file, Release resources, again
request disk file and printer.
o Dis: Protocol 1 Resource Utilization is less.
14
15. Deadlock Prevention
o No Preemption –
o If a process that is holding some resources requests another resource that cannot be
immediately allocated to it, then all resources currently being held are released.
o Preempted resources are added to the list of resources for which the process is
waiting.
o Process will be restarted only when it can regain its old resources, as well as the new
ones that it is requesting.
15
16. Deadlock Prevention
o Circular Wait – impose a total ordering of all resource types, and require that
each process requests resources in an increasing order of enumeration.
16
17. Avoidance algorithms
o Single instance of a resource type. Use a resource-allocation graph
o Multiple instances of a resource type. Use the banker’s algorithm
17
18. Banker’s Algorithm
o The Banker’s Algorithm is a resource allocation and deadlock avoidance algorithm.
o It’s developed by Edsger Dijkstra.
o When a new process enter the system, it declares the maximum number of instances that are needed.
o This number cannot exceed the total number of resources in the system.
o When a process requests a resource it may have to wait.
o When a process gets all its resources it must return them in a finite amount of time.
o The Algorithm is actually made up of two separate algorithms:
1- Safety algorithm
2- Resource allocation algorithm
18
19. 19
Safe, Unsafe , Deadlock State
Difference safe VS unsafe state
A safe state guarantee that all processes will finish
From an unsafe state, no such guarantee can be given
20. 20
Save VS unsafe state
o If a system is in safe state no deadlocks
o A state is safe if it is not deadlocked and there is
some scheduling order in which every P can run to
completion even if all of them suddenly request
maximum # of resources immediately
o A state is safe if there exists a sequence of allocations that allows
all processes to complete
o If a system is in unsafe state possibility of deadlock.
o Avoidance ensure that a system will never enter an
unsafe state.
21. 21
Deadlock Avoidance
Algorithm for deadlock avoidance.
1. When a process requests a resource it may have to wait
2. When a process gets all its resources it must return them in a
finite amount of time
2. Banker’s algorithm pretends that resources are granted and checks
if the resulting state is safe.
3. If the resulting state is safe then the resources are granted
o otherwise they are denied ( postponed until later).
22. Data Structures for the Banker’s Algorithm
o Available: Vector of length m. If available [j] = k, there are k instances of
resource type Rj available.
o Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k
instances of resource type Rj.
o Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k
instances of Rj.
o Need: n x m matrix. If Need[i,j] = k, then Pi may need k more instances of
Rj to complete its task.
Need [i,j] = Max[i,j] – Allocation [i,j].
22
25. 25
3
3 2 2
t1 t7t4
t2 t5 t8
t3 t6 t9
B
A C
Banker’s Model for a Single Resource
4
7
9
2 5
6
Has Max
A 3 9
B 2 4
C 2 7
3 2 2
(a) (b)
Demonstration
that the state
in (a) is safe
Free =
26. 26
1
3 2
t1 t7t4
t2 t5 t8
t3 t6 t9
BA C
after t5+: Banker’s Model for a Single Resource
4
7
9
2 5
6
Has Max
A 3 9
B 2 4
C 2 7
Has Max
A 3 9
B 4 4
C 2 7
3 2 2
(a) (b)
Free =
27. 27
5
3 2
t1 t7t4
t2 t5 t8
t3 t6 t9
BA C
after t6+: Banker’s Model for a Single Resource
4
7
9
2 5
6
Has Max
A 3 9
B 2 4
C 2 7
Has Max
A 3 9
B 0 -
C 2 7
3 2 2
(a) (b)
(c)
Free =
28. 28
3
t1 t7t4
t2 t5 t8
t3 t6 t9
BA C
after t8+: Banker’s Model for a Single Resource
4
7
9
2 5
6
Has Max
A 3 9
B 2 4
C 2 7
Has Max
A 3 9
B 0 -
C 7 7
3 2 2
(a) (b)
(d)
Free =
29. 29
7
3
t1 t7t4
t2 t5 t8
t3 t6 t9
BA C
after t9+: Banker’s Model for a Single Resource
4
7
9
2 5
6
Has Max
A 3 9
B 2 4
C 2 7
3 2 2
(a) (b)
(c)
(d)
Has Max
A 3 9
B 0 -
C 0 -
Free =
30. 30
t1 t7t4
t2 t5 t8
t3 t6 t9
BA C
after t2+ : Banker’s Model for a Single Resource
4
7
9
2 5
6
Has Max
A 3 9
B 2 4
C 2 7
Has Max
A 9 9
B 0 -
C 0 -
3 2 2
(a) (b)
(c)
Free =
31. 31
10
t1 t7t4
t2 t5 t8
1
t3 t6 t9
BA C
after t3+ : Banker’s Model for a Single Resource
4
7
9
2 5
6
Has Max
A 3 9
B 2 4
C 2 7
Has Max
A 0 -
B 0 -
C 0 -
3 2 2
(a) (b)
(c)
Free =
32. 32
Simplified Model of Banker’s Model for a Single Resource
Has Max
A 3 9
B 2 4
C 2 7
(a)
When B: completes. It gives R = 5
When C: completes. It gives R = 7
When A: completes. It gives R = 10.
Demonstration that the state in (a) is safe
needed
released
A
B
2
5
C
4
97
R
6
33. 33
Simplified Model of Banker’s Algorithm for a Single Resource
- from a safe state to an unsafe state
Has Max
A 4 9
B 2 4
C 2 7
(a)
• When B: completes. It gives R = 4
• There is no sequence that guarantees
completion.
• We went from a safe state to an
unsafe state
Has Max
A 3 9
B 2 4
C 2 7
(b)
Demonstration that the state in (b) is unsafe
was 6
A
B
2
5
C
4
97
R
5
34. 34
Deadlock Avoidance
o The bankers algorithm has multiple resources and multiple
processes.
o We have 3 resources and 5 processes and
o We must get all 5 processes fired so that the system stays in a
stable state
o To keep the system from a deadlock we need to make sure we fire the
correct processes sequence,
o so that the resources assigned and available meet the resources needs.
o If you fire a process and the resources are not met, then your system
will become deadlock and will most likely become in an unstable state.
35. Example of Banker’s Algorithm
35
• 5 processes P0 through P4; 3 resource types R0 (10 instances), R1 (5 instances), and R2 (7 instances)
• Snapshot at initial time:
•Is the system in a safe state?
–The system is in a safe state since the sequence <P1, P3, P4, P2, P0> satisfies safety criteria
36. Example of Banker’s Algorithm
36
Allocation Max Need Available
A B C
P0 0 1 0
P1 2 0 0
P2 3 0 2
P3 2 1 1
P4 0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
3 3 2
37. Example of Banker’s Algorithm
37
Allocation Max Need Available
A B C
P0 0 1 0
P1 2 0 0
P2 3 0 2
P3 2 1 1
P4 0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
3 3 2
𝑃0 - Finish[0]=F
38. Example of Banker’s Algorithm
38
Allocation Max Need Available
A B C
P0 0 1 0
P1 2 0 0
P2 3 0 2
P3 2 1 1
P4 0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
3 3 2
𝑃0 - Finish[0]=F
𝑃1 - Finish[1]=T , Work = 5 3 2
39. Example of Banker’s Algorithm
39
Allocation Max Need Available
A B C
P0 0 1 0
P1 2 0 0
P2 3 0 2
P3 2 1 1
P4 0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
3 3 2
𝑃0 - Finish[0]=F
𝑃1 - Finish[1]=T , Work = 5 3 2
𝑃2 - Finish[2]=F
40. Example of Banker’s Algorithm
40
Allocation Max Need Available
A B C
P0 0 1 0
P1 2 0 0
P2 3 0 2
P3 2 1 1
P4 0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
3 3 2
𝑃0 - Finish[0]=F
𝑃1 - Finish[1]=T , Work = 5 3 2
𝑃2 - Finish[2]=F
𝑃3 - Finish[3]=T , Work = 7 4 3
41. Example of Banker’s Algorithm
41
Allocation Max Need Available
A B C
P0 0 1 0
P1 2 0 0
P2 3 0 2
P3 2 1 1
P4 0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
3 3 2
𝑃0 - Finish[0]=F
𝑃1 - Finish[1]=T , Work = 5 3 2
𝑃2 - Finish[2]=F
𝑃3 - Finish[3]=T , Work = 7 4 3
𝑃4 - Finish[4]=T , Work = 7 4 5
42. Example of Banker’s Algorithm
42
Allocation Max Need Available
A B C
P0 0 1 0
P1 2 0 0
P2 3 0 2
P3 2 1 1
P4 0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
3 3 2
𝑃0 - Finish[0]=F
𝑃1 - Finish[1]=T , Work = 5 3 2
𝑃2 - Finish[2]=F
𝑃3 - Finish[3]=T , Work = 7 4 3
𝑃4 - Finish[4]=T , Work = 7 4 5
𝑃0 - Finish[0]=T
Work = 7 5 5
43. Example of Banker’s Algorithm
43
Allocation Max Need Available
A B C
P0 0 1 0
P1 2 0 0
P2 3 0 2
P3 2 1 1
P4 0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
3 3 2
𝑃0 - Finish[0]=F
𝑃1 - Finish[1]=T , Work = 5 3 2
𝑃2 - Finish[2]=F
𝑃3 - Finish[3]=T , Work = 7 4 3
𝑃4 - Finish[4]=T , Work = 7 4 5
𝑃0 - Finish[0]=T
Work = 7 5 5
𝑃2 - Finish[2]=T
Work = 10 5 7
44. Example of Banker’s Algorithm
44
Allocation Max Need Available
A B C
P0 0 1 0
P1 2 0 0
P2 3 0 2
P3 2 1 1
P4 0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
0 1 0
2 0 0
3 0 2
2 1 1
0 0 2
A B C
3 3 2
𝑃0 - Finish[0]=F
𝑃1 - Finish[1]=T , Work = 5 3 2
𝑃2 - Finish[2]=F
𝑃3 - Finish[3]=T , Work = 7 4 3
𝑃4 - Finish[4]=T , Work = 7 4 5
𝑃0 - Finish[0]=T
Work = 7 5 5
𝑃2 - Finish[2]=T
Work = 10 5 7
< 𝑃1 , 𝑃3 , 𝑃4 , 𝑃0 , 𝑃2 >
The Result Sequence:
45. References
1. E.W. Dijkstra. Co-operating sequential processes. In F. Genuys, editor, Program-ming Languages, pages
43112, NewYork, 1968. Academic Press. Reprinted from:Technical Report EWD-123,Technological University,
Eindhoven, the Netherlands,
1965.
2. F.Tricas. Deadlock Analysis, Prevention and Avoidance in Sequential Resource Allo-cation Systems. PhD
thesis, Departamento de Informática
e Ingeniería de Sistemas,
Universidad de Zaragoza, May 2003.
3. F.Tricas, J. M. Colom, and J. Ezpeleta. Some improvements to the banker's algorithm
based on the process structure. In Proceedings of IEEE International Confer-
ence on Robotics and Automation, volume 3, pages 28532858, 2000. San Francisco,
CA, USA.