DeadLock_11111111111796998698869891.pptx

wayap91187 0 views 22 slides Oct 09, 2025
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

hgkdkuy ikhghfkluyf hn uyljhgytyddf j nngljh


Slide Content

DeadLock By Benay Kumar Ray, SOE, JNU

System consists of a finite number of units of each resource type (for example, three printers, six tape drives, four disk drives, two CPUs, etc.), Multiple concurrent processes normally have to compete to use a resources. The sequence of events required to use a resource by a process are: Request : The process first makes a request for the resource Allocate: The system allocates the resource to the requesting process as soon as possible Release: After the process has finished using the allocated resource, it releases the resource to the system Note: If the total request made by multiple concurrent processes for resources exceeds the amount available Some strategy is needed to order the assignment of resources in time Care must be taken that the strategy applied cannot cause a deadlock. It may happen that, some of the processes that entered the waiting state will never again change state, because the resources they have requested are held by other waiting processes. DeadLock ?

Suppose that a system has two tape drives TI and T2 T he resource allocation strategy is such that a requested resource is immediately allocated to the requester if the resource is free . S uppose that two concurrent processes PI and P2 make requests for the tape drives in the following order: PI requests for one tape drive and the system allocates TI to it P2 requests for one tape drive and the system allocates T2 to it. PI requests for one more tape drive and enters a waiting state because no tape drive is presently available. P2 requests for one more tape drive and it also enters a waiting state because no tape drive is presently available. Example? P1 P2 T 1 T 2 PI and P2 will wait for each other indefinitely Since PI will not release TI until it gets T2 to carry out its designated task and vice versa Therefore, the two processes are in a state of deadlock.

Deadlock Example with Semaphores Data: A semaphore S1 initialized to 1 A semaphore S2 initialized to 1 Two processes P1 and P2 P1: wait(s1) wait(s2) P2: wait(s2) wait(s1)

Deadlock Example on a one-way Bridge

Deadlock Example with Cars

Mutual-exclusion condition : If a resource is held by a process, any other process requesting for that resource must wait until the resource has been released. Only one process at a time can use a resource Hold-and-wait condition: Processes are allowed to request for new resources without releasing the resources that they are currently holding. A process holding at least one resource is waiting to acquire additional resources held by other processes No-preemption condition: A resource that has been allocated to a process becomes available for allocation to another process only after it has been voluntarily released by the process holding it. A resource can be released only voluntarily by the process holding it, after that process has completed its task. Conditions for Deadlock to Occur in a system

Circular-wait condition : Two or more processes must form a circular chain in which each process is waiting for a resource that is held by the next member of the chain. There exists a set { P , P 1 , …, P n } of waiting processes such that P 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 . Note: All four conditions must hold simultaneously in a system for a deadlock to occur . If anyone of them is absent, no deadlock can occur Conditions for Deadlock to Occur in a system

Deadlocks can be modeled using directed graphs. Some terminology from graph theory Directed graph: A directed graph is a pair (N, E), where N is a nonempty set of nodes and E is a set of directed edges. A directed edge is an ordered pair (a, b), where a and b are nodes in N. Path : A path is a sequence of nodes (a, b, c, ... , i, j) of a directed graph such that (a, b), (b, c), ... , (i, j) are directed edges. Cycle : A cycle is a path whose first and last nodes are the same. Reachable set: The reachable set of a node a is the set of all nodes b such that a path exists from a to b. Knot: A knot is a collection of vertices and edges with the property that every vertex in the knot has outgoing edges, and all outgoing edges from vertices in the knot terminate at other vertices in the knot. A knot always contains one or more cycles . Deadlock Modeling The graph has a set of nodes {a, b, c, d, e,f } and a set of directed edges {(a, b), (b, c), (c, d), (d, e), ( e,j ), (f, a), ( e,b )} cycles (a, b, c, d, e, f, a) and (b, c, d, e, b). Reachable set from node a is (a,b,c,d,,e,f) A knot {a, b, c, d, e, f) that contains the two cycles of the graph.

For deadlock modeling, a directed graph, called a resource allocation graph , is used In resource allocation graph both the set of nodes and the set of edges are partitioned into two types Process nodes . A process node represents a process of the system. In a resource allocation graph, it is normally shown as a circle, with the name of the process written inside the circle (e.g. node P 1 , P 2 , P 3 ). Resource nodes: A resource node represents a resource of the system. In a resource allocation graph, it is normally shown as a rectangle with the name of the resource written inside the rectangle. Each Resource type may contains more than one unit of resources and is denoted by bullet. Deadlock Modeling: Contd. Assignment edges: An assignment edge is a directed edge from a resource node to a process node. It signifies that the resource is currently held by the process. Request edges: A request edge is a directed edge from a process node to a resource node. It signifies that the process made a request for a unit of the resource type and is currently waiting for that resource (Edges (P I R 2 ) and (P 2 , R I ))

A resource allocation graph provides an overall view of the processes holding or waiting for the various resources in the system. The graph changes dynamically as the processes in the system request for or release resources or the system allocates a resource to a process. That is, when a process P1 requests for a unit of resource type R1 , a request edge (P1, R1 ) is inserted in the resource allocation graph. When this request can be fulfilled, a unit of resource R, is allocated to P1 and the request edge (P1, R1 ) is instantaneously transformed to an assignment edge (R1, P1). Later, when P1 releases R1 , the assignment edge (R1, P1) is deleted from the graph. Constructing a Resource Allocation Graph

In a resource allocation graph , a cycle is a necessary condition for a deadlock to exist Note I f the graph has no cycles , then it represents a state that is free from deadlock . On the other hand , if the graph contains a cycle , a deadlock may exist Therefore, the presence of a cycle in a general resource allocation graph is a necessary but not a sufficient. condition for the existence of deadlock Necessary and Sufficient Conditions for Deadlock The figure does not represent a deadlock state though it contains a cycle This is because when P 3 completes using R 1 and releases it, R 1 can be allocated to P 2 . With both R 1 and R 2 allocated to it, P 2 can now complete its job after which it will release both R 1 and R 2 . As soon as R 2 is released, it can be allocated to P 1 . Therefore, all processes can finish their job one by one .

The sufficient condition for deadlock is different for the following different cases: A cycle in the graph is both a necessary and a sufficient condition for deadlock if all the resource types requested by the processes forming the cycle have only a single unit each. Example Necessary and Sufficient Conditions for Deadlock Figure shows a deadlock state in which processes P I and P 2 are deadlocked. Notice that in this graph, although there are three units of resource R 3 , it is not involved in the cycle Both R 1 and R 2 that are involved in the cycle have only one unit each. Therefore, the cycle represents a deadlock state.

The sufficient condition for deadlock is different for the following different cases: A cycle in the graph is a necessary but not a sufficient condition for deadlock if one or more of the resource types requested by the processes forming the cycle have more than one unit. In this case, a knot is a sufficient condition for deadlock .. Example Necessary and Sufficient Conditions for Deadlock suppose that in the this graph P 3 requests for R 2 and a request edge (P 3 , R 2 ) is added to the graph. The modified graph is shown in next figure Since the graph contains a knot, it represents a deadlock state in which processes P 1 , P 2 , and P 3 are deadlocked.

The sufficient condition for deadlock is different for the following different cases: A cycle in the graph is both a necessary and a sufficient condition for deadlock if all the resource types requested by the processes forming the cycle have only a single unit each. Example Necessary and Sufficient Conditions for Deadlock Figure shows a deadlock state in which processes P I and P 2 are deadlocked. Notice that in this graph, although there are three units of resource R 3 , it is not involved in the cycle Both R 1 and R 2 that are involved in the cycle have only one unit each. Therefore, the cycle represents a deadlock state.

When all the resource types have only a single unit each, a simplified form of resource allocation graph is normally used. The simplified graph is obtained from the original resource allocation graph by removing the resource nodes and collapsing the appropriate edges. Figure shows an example of a resource allocation graph and its simplified form. Wait-for Graph The simplified graph is commonly known as a wait-for graph (WFG) because it clearly shows which processes are waiting for which other processes. In the WFG of processes PI and P3 are waiting for P2 and process P2 is waiting for P1. Since WFG is constructed only when each resource type has only a single unit, a cycle is both a necessary and sufficient condition for deadlock in a WFG.

Three commonly used strategies to handle deadlocks are as follows: Avoidance: Resources are carefully allocated to avoid deadlocks. Prevention: Constraints are imposed on the ways in which processes request resources in order to prevent deadlocks. Detection and recovery : Deadlocks are allowed to occur and a detection algorithm is used to detect them. After a deadlock is detected, it is resolved by certain means. Note: The third strategy is the most commonly used one in distributed systems Handling Deadlocks

Deadlock avoidance methods use some advance knowledge of the resource requirements of processes to predict the future state of the system for avoiding allocations that can eventually lead to a deadlock. Deadlock avoidance algorithms are usually follow the following steps: When a process requests for a resource, even if the resource is available for allocation, it is not immediately allocated to the process. Rather, the system simply assumes that the request is granted. With the assumption made in step 1 and advance knowledge of the resource requirements of processes, the system performs some analysis to decide whether granting the process's request is safe or unsafe. The resource is allocated to the process only when the analysis of step 2 shows that it is safe to do so; otherwise the request is deferred. Deadlock Avoidance

Note: Deadlock avoidance are based on the concept of safe and unsafe states Hence it is important to look at the notion of safety in resource allocation . A system is in a safe state : If it is not in a deadlock state and there exists some ordering of the processes in which the resource requests of the processes can be granted to run all of them to completion . Here any ordering of the processes that can guarantee the completion of all the processes is called a safe sequence. A system state is said to be unsafe if no safe sequence exists for that state. Let us understand the safe and unsafe states with the help of an example . Assumption: System have in total of 8 units of a particular resource type for which three processes P 1 , P 2 , and P 3 are competing. Maximum units of the resource required by P 1 , P 2 , and P 3 are 4, 5, and 6, respectively. Currently each of the three processes is holding 2 units of resource Deadlock Avoidance Contd.

Deadlock Avoidance Contd. This state has two safe sequences, (P 1 , P 2 , P 3 ) and (P 1 , P 3 , P 2 ) . P 1 is run exclusively, until it asked for and allocated two more units of the resource that are currently free , Scheduler could choose any one of the two processes P 2 and P 3 Hence the initial state of Figure is a safe state because the system, by careful scheduling, can avoid deadlock.

If resource allocation is not done cautiously, the system may move from a safe state to an unsafe state. For instance, let us consider the example shown before Suppose process P 2 requests for one additional unit of the resource and the same is allocated to it by the system. Now the system is no longer in a safe state because we only have one unit of the resource free, which is not sufficient to run any of the three processes to completion . Thus the decision to allocate one unit of the resource to P 2 moved the system from a safe state to an unsafe state . Deadlock Avoidance Contd.

The algorithms work on the assumption that advance knowledge of the resource requirements of the various processes is available . However, in practice, processes rarely know in advance what their maximum resource needs will be The algorithms also assume that the number of processes that compete for a particular resource is fixed and known in advance . However, in practice, the number of processes is not fixed but dynamically varies as new users log in and log out. The algorithms also assume that the number of units of a particular resource type is always fixed and known in advance . However, in practice, the actual number of units available may change dynamically due to the sudden breakdown and repair of one or more units. Because of these reasons deadlock avoidance algorithms are rarely used. Limitation of Deadlock Avoidance
Tags