Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 : Deadlock 1By : Jigar M. Pandya. Deadlock 2 A Process Must request a resource before using it. And Process release the resource after using.

Similar presentations


Presentation on theme: "Chapter 4 : Deadlock 1By : Jigar M. Pandya. Deadlock 2 A Process Must request a resource before using it. And Process release the resource after using."— Presentation transcript:

1 Chapter 4 : Deadlock 1By : Jigar M. Pandya

2 Deadlock 2 A Process Must request a resource before using it. And Process release the resource after using it. Sequence of events required to use a resource: 1. Request the resource. 2. Use the resource. 3. Release the resource.

3 Deadlock By : Jigar M. Pandya3 Examaple :

4 Deadlock A deadlock consists of a set of blocked processes, each holding a resource and waiting to acquire a resource held by another process in the set. By : Jigar M. Pandya4

5 Deadlock Characterization By : Jigar M. Pandya5 Deadlock can arise if four conditions hold simultaneously. 1. Mutual exclusion: only one process at a time can use a resource

6 Deadlock Characterization By : Jigar M. Pandya6 Deadlock can arise if four conditions hold simultaneously. 2. Hold and wait: a process holding at least one resource is waiting to acquire additional resources held by other processes

7 Deadlock Characterization By : Jigar M. Pandya7 Deadlock can arise if four conditions hold simultaneously. 3. No preemption: a resource can be released only voluntarily by the process holding it after that process has completed its task

8 Deadlock Characterization By : Jigar M. Pandya8 Deadlock can arise if four conditions hold simultaneously. 4. Circular wait: there exists a set {P 0, P 1, …, P 0 } of waiting processes such that P 0 is waiting for a resource that is held by P 1, P 1 is waiting for a resource that is held by P 2, …, P n–1 is waiting for a resource that is held by P n, and P n is waiting for a resource that is held by P 0

9 By : Jigar M. Pandya9 Resource-Allocation Graph V is partitioned into two types: P = {P 1, P 2, …, P n }, the set consisting of all the processes in the system R = {R 1, R 2, …, R m }, the set consisting of all resource types in the system request edge – directed edge P 1 -> R j assignment edge – directed edge R j -> P i A set of vertices V and a set of edges E.

10 By : Jigar M. Pandya10 Resource-Allocation Graph Resource-Allocation Graph Process Resource Type with 4 instances P i requests instance of R j P i is holding an instance of R j PiPi PiPi

11 By : Jigar M. Pandya11 Resource Allocation Graph With A Deadlock Before P 3 requested an instance of R 2 After P 3 requested an instance of R 2 A Cycle In the Graph May cause Deadlock

12 By : Jigar M. Pandya12 Graph With A Cycle But No Deadlock Process P 4 may release its instance of resource type R 2. That resource can then be allocated to P3, thereby breaking the cycle.

13 Deadlock By : Jigar M. Pandya13

14 By : Jigar M. Pandya14 Methods for Handling Deadlocks Prevention Ensure that the system will never enter a deadlock state Avoidance Ensure that the system will never enter an unsafe state Detection Allow the system to enter a deadlock state and then recover Do Nothing Ignore the problem and let the user or system administrator respond to the problem; used by most operating systems, including Windows and UNIX

15 By : Jigar M. Pandya15 Deadlock Prevention

16 By : Jigar M. Pandya16 Deadlock Prevention To prevent deadlock, we can restrain the ways that a request can be made Do not allow one of the four conditions to occur. Mutual Exclusion : if no resource were ever assigned to a single process exclusively,we would never have deadlock. Shared entities (read only files) don't need mutual exclusion (and arent susceptible to deadlock.) Prevention not possible, since some devices are naturally non-sharable

17 By : Jigar M. Pandya17 Deadlock Prevention To prevent deadlock, we can restrain the ways that a request can be made Do not allow one of the four conditions to occur. Hold And Wait : we must guarantee that whenever a process requests a resource, it does not hold any other resources Require a process to request and be allocated all its resources before it begins execution allow a process to request resources only when the process has none Result: Low resource utilization; starvation possible

18 By : Jigar M. Pandya18 Deadlock Prevention To prevent deadlock, we can restrain the ways that a request can be made Do not allow one of the four conditions to occur. 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 Pi Rj Pj Rk A process will be restarted only when it can regain its old resources, as well as the new ones that it is requesting. Allow preemption - if a needed resource is held by another process, which is also waiting on some resource, steal it. Otherwise wait.

19 By : Jigar M. Pandya19 Deadlock Prevention To prevent deadlock, we can restrain the ways that a request can be made Do not allow one of the four conditions to occur. Circular Wait : impose a total ordering of all resource types, and require that each process requests resources in an increasing order of enumeration. F : R N F(ri) < F(rj) P1 r1 P2 r2 P3……. Pn-1 rn Pn For example: F(tape drive) = 1 F(disk drive) = 5 F(printer) = 12

20 By : Jigar M. Pandya20 Deadlock Prevention EACH of these prevention techniques may cause a decrease in utilization and/or resources. For this reason, prevention isn't necessarily the best technique. Prevention is generally the easiest to implement.

21 Deadlock Avoidance Process must declare all the required resource before starting the execution. P1 Requires A,B. P2 Requires B,A. By : Jigar M. Pandya21 P1 B P2 A 1) P1 A 2) P2 B 3) P1 B 4) P2 B 5) P2 A Process P1 Complete Release Resource Process P2 Complete Release Resourece

22 Deadlock Avoidance By : Jigar M. Pandya22 Simplest and most useful model requires that 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 circular-wait condition A resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes

23 Deadlock Avoidance By : Jigar M. Pandya23 Possible states are: Deadlock No forward progress can be made. Unsafe state A state that may allow deadlock. Safe state A state is safe if a sequence of processes exist such that there are enough resources for the first to finish, and as each finishes and releases its resources there are enough for the next to finish. The rule is simple: If a request allocation would cause an unsafe state, do not honor that request.

24 Deadlock Avoidance By : Jigar M. Pandya24 NOTE: All deadlocks are unsafe, but all unsafes are NOT deadlocks. SAFE DEADLOCK UNSAFE Only with luck will processes avoid deadlock. O.S. can avoid deadlock.

25 Deadlock Avoidance By : Jigar M. Pandya25 EXAMPLE: There exists a total of 12 resources. Each resource is used exclusively by a process. The current state looks like this: In this example, is a workable sequence. Free = 2 Suppose p2 requests and is given one more resource. What happens then? Free= 1 ProcessMax NeedsAllocatedCurrent Needs P01055 P1422 P2937 ProcessMax NeedsAllocatedCurrent Needs P01055 P1422 P2947

26 By : Jigar M. Pandya26 Avoidance algorithms For a single instance of a resource type, use a resource-allocation graph For multiple instances of a resource type, use the bankers algorithm

27 By : Jigar M. Pandya27 Resource-Allocation Graph with Claim Edges Request edge Assignment edge Claim edge Claim edge

28 By : Jigar M. Pandya28 Unsafe State In Resource-Allocation Graph Assignment edge Request edge Assignment edge Claim edge

29 By : Jigar M. Pandya29 Resource-Allocation Graph Algorithm Suppose that process P i requests a resource R j The request can be granted only if converting the request edge to an assignment edge does not result in the formation of a cycle in the resource allocation graph

30 By : Jigar M. Pandya30 Data Structures for the Bankers Algorithm Available: Vector of length m. If available [j] = k, there are k instances of resource type R j available. Max: n x m matrix. If Max [i,j] = k, then process P i may request at most k instances of resource type R j. Allocation: n x m matrix. If Allocation[i,j] = k then P i is currently allocated k instances of R j. Need: n x m matrix. If Need[i,j] = k, then P i may need k more instances of R j to complete its task. Let n = number of processes, and m = number of resources types.

31 By : Jigar M. Pandya31 Bankers Algorithm A method used to determine if a particular state is safe. It's safe if there exists a sequence of processes such that for all the processes, theres a way to avoid deadlock: The algorithm uses these variables: Need[I] – the remaining resource needs of each process. Work - Temporary variable – how many of the resource are currently available. Finish[I] – flag for each process showing weve analyzed that process or not. need <= available + allocated[0] allocated[I-1] Sign of success Let work and finish be vectors of length m and n respectively. Safety Algorithm

32 By : Jigar M. Pandya32 Bankers Algorithm 1. Initialize work = available Initialize finish[i] = false, for i = 1,2,3,..n 2. Find an i such that: finish[i] == false and need[i] <= work If no such i exists, go to step work = work + allocation[i] finish[i] = true goto step 2 4. if finish[i] == true for all i, then the system is in a safe state. Safety Algorithm

33 Bankers Algorithm By : Jigar M. Pandya33 Context Of Matrix need Is Calculated As Need = Max - Allocation

34 Bankers Algorithm By : Jigar M. Pandya34

35 By : Jigar M. Pandya35 Bankers Algorithm

36 By : Jigar M. Pandya36 Bankers Algorithm


Download ppt "Chapter 4 : Deadlock 1By : Jigar M. Pandya. Deadlock 2 A Process Must request a resource before using it. And Process release the resource after using."

Similar presentations


Ads by Google