Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 4 : Deadlock By : Jigar M. Pandya.

Similar presentations


Presentation on theme: "Chapter 4 : Deadlock By : Jigar M. Pandya."— Presentation transcript:

1 Chapter 4 : Deadlock By : Jigar M. Pandya

2 Deadlock 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: Request the resource. Use the resource. Release the resource. By : Jigar M. Pandya

3 Deadlock Examaple : By : Jigar M. Pandya

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. Pandya

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

6 Deadlock Characterization
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 By : Jigar M. Pandya

7 Deadlock Characterization
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 By : Jigar M. Pandya

8 Deadlock Characterization
Deadlock can arise if four conditions hold simultaneously. 4. Circular wait: there exists a set {P0, P1, …, P0} of waiting processes such that P0 is waiting for a resource that is held by P1, P1 is waiting for a resource that is held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and Pn is waiting for a resource that is held by P0 By : Jigar M. Pandya

9 Resource-Allocation Graph
A set of vertices V and a set of edges E. V is partitioned into two types: P = {P1, P2, …, Pn}, the set consisting of all the processes in the system R = {R1, R2, …, Rm}, the set consisting of all resource types in the system request edge – directed edge P1 -> Rj assignment edge – directed edge Rj -> Pi By : Jigar M. Pandya

10 Resource-Allocation Graph ‏
Process Resource Type with 4 instances Pi requests instance of Rj Pi is holding an instance of Rj Pi Pi By : Jigar M. Pandya

11 Resource Allocation Graph With A Deadlock
Before P3 requested an instance of R2 After P3 requested an instance of R2 A Cycle In the Graph May cause Deadlock By : Jigar M. Pandya

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

13 Deadlock By : Jigar M. Pandya

14 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 By : Jigar M. Pandya

15 Deadlock Prevention By : Jigar M. Pandya

16 Do not allow one of the four conditions to occur.
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 aren’t susceptible to deadlock.) Prevention not possible, since some devices are naturally non-sharable By : Jigar M. Pandya

17 Do not allow one of the four conditions to occur.
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 By : Jigar M. Pandya

18 Do not allow one of the four conditions to occur.
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. By : Jigar M. Pandya

19 Do not allow one of the four conditions to occur.
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) = F(disk drive) = F(printer) = 12 By : Jigar M. Pandya

20 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. By : Jigar M. Pandya

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

22 Deadlock Avoidance 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 By : Jigar M. Pandya

23 Deadlock Avoidance 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. By : Jigar M. Pandya

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

25 Deadlock Avoidance 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, < p1, p0, p2 > is a workable sequence. Free = 2 Suppose p2 requests and is given one more resource. What happens then? Free= 1 Process Max Needs Allocated Current Needs P0 10 5 P1 4 2 P2 9 3 7 Process Max Needs Allocated Current Needs P0 10 5 P1 4 2 P2 9 7 By : Jigar M. Pandya

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

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

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

29 Resource-Allocation Graph Algorithm
Suppose that process Pi requests a resource Rj 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 By : Jigar M. Pandya

30 Data Structures for the Banker’s Algorithm
Let n = number of processes, and m = number of resources types. 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. By : Jigar M. Pandya

31 Banker’s Algorithm Safety 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, there’s 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 we’ve 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. By : Jigar M. Pandya

32 Banker’s Algorithm Safety 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 4. 3. 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. By : Jigar M. Pandya

33 Banker’s Algorithm Context Of Matrix need Is Calculated As
Need = Max - Allocation By : Jigar M. Pandya

34 Banker’s Algorithm By : Jigar M. Pandya

35 Banker’s Algorithm By : Jigar M. Pandya

36 Banker’s Algorithm By : Jigar M. Pandya


Download ppt "Chapter 4 : Deadlock By : Jigar M. Pandya."

Similar presentations


Ads by Google