45
Using A Petri Net Concepts To organize The Resources Sharing Between Two Processors

A petri-net

Embed Size (px)

Citation preview

Using A Petri Net Concepts To organize The Resources Sharing Between Two Processors

How to control resources appeared.How to provide the system with the resources at a proper time to avoid the system to be failed.What is a Deadlock.

The problem is :

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

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, andadds w(t,p) tokens to each output place

Transition Enabling & Firing

Transitions are usually used to model events (activities) and operations which change the states in places.

Places are used to model states after events have happened Places can model conditions, resources or objects, like a program variable and so on.

Operations after firing a transition are related with input/output arrows of its transition.

PN Modeling

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

A Petri Net is deadlocked if no transition is enable.

Deadlock in Petri Net

8

down(r_1)down(r_2)up(r_2)up(r_1)down(r_1)down(r_2)up(r_2)up(r_1)t3t2t5t4t6t7p2t8r_1p1p3 - criticalp4p6p7 - criticalp8p5

t1r_2Deadlock-Free Petri Nets with two ResourcesProcess AProcess Bred transitions are enable

8

9

down(r_1)down(r_2)up(r_2)up(r_1)down(r_2)down(r_1)up(r_1)up(r_2)t3t2t5t4t6t7p2t8r_1p1p3 - criticalp4p6p7 - criticalp8

t1r_2Potential Deadlock Petri Nets with two Resources

p5Process AProcess B

9

10

down(r_1)down(r_2)up(r_2)up(r_1)down(r_2)down(r_1)up(r_1)up(r_2)t3t2t5t4t6t7p2t8r_1p1p3 - criticalp4p6p7 - criticalp8

t1r_2Deadlock Petri Net with two Resources

p5Process AProcess B

10

Methods for Handling DeadlocksEnsure that the system will never enter a deadlock state.Allow the system to enter a deadlock state and then recover.11

Deadlock Prevention Four conditions for DeadlockMutual exclusionHold and waitNo preemptionCircular waitPrevent Deadlock Ensure that at least one of these cannot hold (any one is false)Prevention is better than cure12

Deadlock Prevention 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.

Hold and Wait Protocols, (No wait/ No Hold) Process must request all resources and be allocated. (No wait)Must guarantee that whenever a process requests a resource, it does not hold any other resources. (No Hold)13

Deadlock Prevention Ex. Copy data from Tape drive to disc, sorts the disk file and then prints result to a printer.No wait: Request Tape drive, Disk file and printer.No Hold: Request Tape drive and disk file, Release resources, again request disk file and printer.Dis: Protocol 1 Resource Utilization is less. 14

Deadlock Prevention No Preemption 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.Preempted resources are added to the list of resources for which the process is waiting.Process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting.

15

Deadlock Prevention Circular Wait impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration.

16

Avoidance algorithms Single instance of a resource type. Use a resource-allocation graph

Multiple instances of a resource type. Use the bankers algorithm17

Bankers Algorithm The Bankers Algorithm is a resource allocation and deadlock avoidance algorithm.Its developed by Edsger Dijkstra.When a new process enter the system, it declares the maximum number of instances that are needed.This number cannot exceed the total number of resources in the system.When a process requests a resource it may have to wait. When a process gets all its resources it must return them in a finite amount of time.The Algorithm is actually made up of two separate algorithms:1- Safety algorithm2- Resource allocation algorithm18

19Safe, Unsafe , Deadlock State

Difference safe VS unsafe stateA safe state guarantee that all processes will finishFrom an unsafe state, no such guarantee can be given

19Bankers Algorithm Assumptions:Multiple instances of resource types.

Each process must a priori claim maximum use.

When a process requests a resource it may have to wait.

When a process gets all its resources it must return them in a finite amount of time.

20Save VS unsafe stateIf a system is in safe state no deadlocks

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 immediatelyA state is safe if there exists a sequence of allocations that allows all processes to complete

If a system is in unsafe state possibility of deadlock.

Avoidance ensure that a system will never enter an unsafe state.

20

21Deadlock AvoidanceAlgorithm for deadlock avoidance.

When a process requests a resource it may have to wait

When a process gets all its resources it must return them in a finite amount of time

2. Bankers 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 otherwise they are denied ( postponed until later).

21

Data Structures for the Bankers Algorithm Available: Vector of length m. If available [j] = k, there are k instances of resource type Rj available.Max: n x m matrix. If Max [i,j] = k, then process Pi may request at most k instances of resource type Rj.Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently allocated k instances of Rj.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

Data Structures for the Bankers Algorithm 23

Steps To Find Safe Sequence24

253322

t1t7t4

t2t5t8

t3t6t9BACBankers Model for a Single Resource

479256HasMaxA39B24C27

322(a)(b)Demonstration that the state in (a) is safeFree =

25

2613

2

t1t7t4

t2t5t8

t3t6t9BAC after t5+: Bankers Model for a Single Resource

479256HasMaxA39B24C27

HasMaxA39B44C27

322(a)(b)

Free =

26

2753

2

t1t7t4

t2t5t8

t3t6t9BACafter t6+: Bankers Model for a Single Resource

479256HasMaxA39B24C27

HasMaxA39B0-C27

322(a)(b)(c)Free =

27

28

3

t1t7t4

t2t5t8

t3t6t9BACafter t8+: Bankers Model for a Single Resource

479256HasMaxA39B24C27

HasMaxA39B0-C77

322(a)(b)(d)

Free =

28

2973

t1t7t4

t2t5t8

t3t6t9BACafter t9+: Bankers Model for a Single Resource

479256HasMaxA39B24C27

322(a)(b)(c)(d)HasMaxA39B0-C0-

Free =

29

30

t1t7t4

t2t5t8

t3t6t9BACafter t2+ : Bankers Model for a Single Resource

479256HasMaxA39B24C27

HasMaxA99B0-C0-

322(a)(b)(c)

Free =

30

3110

t1t7t4

t2t5t8

1

t3t6t9BACafter t3+ : Bankers Model for a Single Resource

479256HasMaxA39B24C27

HasMaxA0-B0-C0-

322(a)(b)(c)Free =

31

32Simplified Model of Bankers Model for a Single ResourceHasMaxA39B24C27

(a)When B: completes. It gives R = 5When C: completes. It gives R = 7When A: completes. It gives R = 10.

Demonstration that the state in (a) is safeneededreleased

32

33Simplified Model of Bankers Algorithm for a Single Resource - from a safe state to an unsafe stateHasMaxA49B24C27

(a)When B: completes. It gives R = 4

There is no sequence that guarantees completion.

We went from a safe state to an unsafe stateHasMaxA39B24C27

(b)Demonstration that the state in (b) is unsafe

33Demonstration that the sate in b is not safeCan we find a sequence that is guaranteed to work?(a) initial state A request and gets another resource(b) The scheduler could run B until it asked for all its resources, see (c ). Eventually B completes, see (d)We are stuck there is no sequence that guarantees completionRunning A or C at (b) doesnt work eitherThe allocation decision that moved the system from (a) to (b) went from a safe state to an unsafe stateAs request shouldnt have been granted

34Deadlock AvoidanceThe bankers algorithm has multiple resources and multiple processes. We have 3 resources and 5 processes and

We must get all 5 processes fired so that the system stays in a stable stateTo keep the system from a deadlock we need to make sure we fire the correct processes sequence, so that the resources assigned and available meet the resources needs.

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.

34

Example of Bankers Algorithm 355 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 satisfies safety criteria

Example of Bankers Algorithm 36 Allocation Max Need AvailableABCP0010P1200P2302P3211P4002

ABC010200302211002

ABC010200302211002

ABC332

Example of Bankers Algorithm 37 Allocation Max Need AvailableABCP0010P1200P2302P3211P4002

ABC010200302211002

ABC010200302211002

ABC332

Example of Bankers Algorithm 38 Allocation Max Need AvailableABCP0010P1200P2302P3211P4002

ABC010200302211002

ABC010200302211002

ABC332

Example of Bankers Algorithm 39 Allocation Max Need AvailableABCP0010P1200P2302P3211P4002

ABC010200302211002

ABC010200302211002

ABC332

Example of Bankers Algorithm 40 Allocation Max Need AvailableABCP0010P1200P2302P3211P4002

ABC010200302211002

ABC010200302211002

ABC332

Example of Bankers Algorithm 41 Allocation Max Need AvailableABCP0010P1200P2302P3211P4002

ABC010200302211002

ABC010200302211002

ABC332

Example of Bankers Algorithm 42 Allocation Max Need AvailableABCP0010P1200P2302P3211P4002

ABC010200302211002

ABC010200302211002

ABC332

Example of Bankers Algorithm 43 Allocation Max Need AvailableABCP0010P1200P2302P3211P4002

ABC010200302211002

ABC010200302211002

ABC332

Example of Bankers Algorithm 44 Allocation Max Need AvailableABCP0010P1200P2302P3211P4002

ABC010200302211002

ABC010200302211002

ABC332

The Result Sequence:

References

1. E. W. Dijkstra. Co-operating sequential processes. In F. Genuys, editor, Program-ming Languages, pages 43112, New York, 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 Informtica e Ingeniera de Sistemas,Universidad de Zaragoza, May 2003.

3. F. Tricas, J. M. Colom, and J. Ezpeleta. Some improvements to the banker's algorithmbased on the process structure. In Proceedings of IEEE International Confer-ence on Robotics and Automation, volume 3, pages 28532858, 2000. San Francisco,CA, USA.

2

2a placea transitionan arc's weightmultiplicity 2a tokenan arc 2

2a placea transitionan arc's weighta tokenan arc

C

A

4

B

2

5

9

7

R

6

C

was 6

A

4

B

2

5

9

7

R

5