INTRODUCTION
In a multiprogramming environment, several processes may compete for a finite number of resources. A process requests resources; if the resources are not available at that time, the process enters a waiting state. Sometimes, a waiting process is never again able to change state, because the resources it has requested are held by other waiting processes. This situation is called a deadlock.
Deadlock is a critical concept in the field of operating systems, particularly in the context of concurrent and multi-threaded environments. It occurs when two or more processes (or threads) are unable to proceed because each is waiting for the other to release a resource or complete a task, resulting in a standstill where none of the processes can make progress.
To understand deadlock better, let's look at the four necessary conditions for deadlock to occur, often referred to as the "deadlock conditions" or "Coffman conditions":
1. Mutual Exclusion: At least one resource must be held in a non-shareable mode, meaning only one process can use it at a time. When a process holds a resource, no other process can access it until it is released.
2. Hold and Wait: A process must be holding at least one resource while waiting to acquire additional resources that are currently held by other processes.
3. No Preemption: Resources cannot be forcibly taken away from processes; they can only be released voluntarily by the process holding them.
4. Circular Wait: A circular chain of two or more processes exists, where each process is waiting for a resource that is held by another process in the chain.
When all four conditions are met simultaneously, a deadlock situation arises. This can lead to a system becoming unresponsive, and the only way to recover from a deadlock is to abort one or more processes, releasing the resources they were holding. However, deciding which process to abort can be challenging and may lead to data loss or undesirable consequences.
Deadlock avoidance, detection, and recovery are essential techniques used by operating systems to handle or prevent deadlocks from causing system failures. Prevention involves ensuring that at least one of the deadlock conditions is never satisfied. Detection involves periodically examining the state of the system to identify potential deadlocks, while recovery involves taking corrective actions once a deadlock is detected. Handling deadlocks effectively is crucial to ensure the stability and reliability of modern operating systems, especially in scenarios where multiple processes are competing for shared resources.
RESOURCE - ALLOCATION GRAPH
In operating systems, a resource allocation graph is a graphical representation used to model and analyze the allocation of resources among different processes and the relationships between them. It is a critical tool for understanding resource allocation and detecting potential deadlocks in the system. A resource allocation graph consists of two types of nodes:
1. Process Nodes: Each process in the system is represented by a process node. It indicates the processes that are currently running or requesting resources.
2. Resource Nodes: Each resource in the system is represented by a resource node. It indicates the instances of resources available in the system.
- Resource Allocation Order: One way to prevent deadlocks is by defining a specific order in which resources must be allocated. This can prevent circular wait conditions that lead to deadlocks.
- Resource Limitation: Limiting the maximum number of resources a process can hold at any given time can reduce the likelihood of deadlocks.
- Safe State Check: The operating system can employ algorithms like the Banker's algorithm to check if a state is safe before allocating resources to a process. If the state is not safe, the resource allocation is deferred until it becomes safe.
- Resource Request Check: Before allocating resources to a process, the operating system can predict if the allocation will potentially lead to a deadlock. If so, the allocation is postponed or denied.
- Resource Allocation Graph: As mentioned earlier, the resource allocation graph can be used to detect deadlocks in the system. Once a deadlock is detected, appropriate actions can be taken to resolve it.
- Timeout Mechanisms: A timeout can be set on resource requests. If a process cannot acquire the required resources within a specified time, it releases all its allocated resources and restarts execution from the beginning.
- If a process holding certain resources is waiting indefinitely for additional resources held by other processes, the operating system can preempt the resources from the waiting process and allocate them to the waiting process, thereby breaking the potential deadlock.
- In situations where deadlock resolution is difficult or not feasible, the operating system can choose to terminate one or more processes to release the resources they are holding. The terminated processes can be restarted later to continue their tasks.
Detecting deadlocks in operating systems can be accomplished using various techniques. One of the commonly used methods is the resource allocation graph, as mentioned earlier. Here's a step-by-step guide to detecting deadlocks using the resource allocation graph:
1. Resource Allocation Graph (RAG) Construction:
- Identify all the processes and resources in the system.
- Represent each process as a node in the graph.
- Represent each resource as a node in the graph.
- Add directed edges (arcs) between the process nodes and resource nodes to indicate resource requests and allocations.
- Run a deadlock detection algorithm periodically or whenever there is a significant change in resource allocation.
- One such algorithm is the cycle detection algorithm, which checks if there is a cycle in the resource allocation graph.
- Another commonly used algorithm is the Banker's algorithm, which checks for the safe sequence to allocate resources and detects if a deadlock exists.
- Perform a depth-first search (DFS) on the resource allocation graph.
- Start the DFS from each unmarked process node.
- While performing the DFS, mark the visited process nodes and follow the arcs (edges) to traverse the graph.
- If you encounter a marked process node during the DFS, it indicates the presence of a cycle, and thus, a deadlock.
- The Banker's algorithm is used to check if the system is in a safe state, i.e., no deadlock exists.
- It simulates resource allocation and deallocation to determine if a safe sequence can be found to complete all processes.
- If a safe sequence is found, the system is in a safe state, and there is no deadlock.
- If a safe sequence cannot be found, it indicates that the system is in an unsafe state, and deadlock is possible.
- Once a deadlock is detected, the operating system can take appropriate actions based on the chosen deadlock handling strategy.
- This may involve resource preemption, process termination, or other methods to resolve the deadlock and restore the system to a functional state.
EXAMPLE: BANKER‟S ALGORITHM
The Banker's algorithm is a resource allocation and deadlock avoidance algorithm that can also be used for deadlock detection. It is used in systems where multiple processes compete for a finite set of resources. The algorithm keeps track of the available resources, the maximum resources each process may need, and the currently allocated resources.
Here's how the Banker's algorithm works for deadlock detection:
a. Resource Allocation Data Structures:
The system maintains several data structures:
- Available: An array indicating the number of available instances of each resource type.
- Max: A 2D array representing the maximum number of each resource type that each process may need.
- Allocation: A 2D array representing the number of each resource type currently allocated to each process.
- Work: An array initially set to the same values as the Available array. It represents the number of available resources after considering the current allocations.
- Finish: A boolean array initially set to false, representing the finish state of each process.
- Before a resource request is granted to a process, the Banker's algorithm checks if the system is in a safe state. A safe state means that there is at least one sequence in which all processes can complete their execution without getting stuck in a deadlock.
- The algorithm simulates resource allocation and deallocation to check for a safe sequence using the following steps: a. initially, find an index i such that Finish[i] is false, and Need[i] (the maximum needed resources - currently allocated resources for process i) is less than or equal to Work. b. If such an index i is found, add the Allocation[i] to the Work array and mark the process as finished (Finish[i] = true). c. Repeat steps a and b until no such index i is found, or all processes are marked as finished (Finish[i] is true for all i). d. If all processes are marked as finished, the system is in a safe state, and the requested resource can be granted. If not, the system is in an unsafe state, and the resource request is denied to avoid potential deadlock.
- If the Banker's algorithm finds that the system is in an unsafe state while granting a resource request, it indicates that a deadlock may occur if the request is allowed.
- In this case, the system can choose to use other deadlock detection algorithms, such as cycle detection in the resource allocation graph, to confirm the presence of a deadlock.
- Define a global ordering of resources and require processes to request resources in a specific order.
- This approach prevents circular waits, as processes can only request resources in a way that respects the predefined order.
- Set a limit on the maximum number of resources each process can hold simultaneously.
- By limiting the number of resources that a process can have, the chances of deadlocks are reduced.
- In concurrent systems, use the two-phase locking protocol to ensure that processes acquire and release locks on resources in a coordinated manner.
- This method helps prevent deadlocks caused by multiple processes competing for the same resources.
- In database management systems, use transaction serialization techniques to ensure that concurrent transactions do not interfere with each other, reducing the risk of deadlocks.
- Implement spooling, where processes use an intermediate storage area (spool) for holding data or results before using a shared resource.
- Spooling reduces the contention for shared resources and minimizes the chances of deadlocks.
- Set timeouts for resource requests, and if a process cannot acquire the required resources within a specified time, release all its allocated resources and retry later.
- Reclaim unused or idle resources from processes to avoid resource wastage and increase the availability of resources.
- Implement a resource request protocol where processes explicitly declare their resource needs and release resources they no longer require.
- This helps in efficient resource management and reduces the chances of deadlocks.
- Although prevention is the ideal approach, incorporating deadlock detection mechanisms is still valuable.
- Detection mechanisms can be used to periodically check for deadlocks and take appropriate actions to resolve them if they occur.
- The RAG is a graphical representation of the resources and processes in the system, similar to the one used for deadlock detection.
- The graph consists of process nodes, resource nodes, request edges (representing resource requests), and assignment edges (representing resource allocations).
- The deadlock avoidance algorithm continually analyzes the resource allocation graph and makes decisions on resource allocation based on certain criteria to ensure deadlock avoidance.
- The algorithm maintains the concept of a "safe state," which is a state where there exists a sequence of resource allocations that allows all processes to complete successfully without encountering a deadlock.
- If the system is in a safe state, resource allocations can proceed.
- If the system is not in a safe state, the algorithm avoids further resource allocations until a safe state is achievable.
- Before allocating resources to a process, the algorithm predicts the potential effects of the allocation on the system's state.
- If the allocation will lead to an unsafe state (risking a deadlock), the allocation is postponed until it is safe to proceed.
- If the allocation will maintain or lead to a safe state, the resources are allocated to the requesting process.
- The deadlock avoidance algorithm may prioritize processes based on factors such as the number of resources they currently hold, their maximum resource needs, and their priority levels to make optimal resource allocation decisions.
- One of the simplest deadlock recovery methods is to terminate one or more processes involved in the deadlock.
- The terminated processes release their held resources, allowing the remaining processes to continue execution and preventing further deadlocks.
- The operating system can choose which process(es) to terminate based on certain criteria such as priority or resource usage.
- In resource preemption, the operating system forcibly takes resources from one or more processes to break the deadlock.
- The preempted resources are then allocated to other waiting processes.
- Preemption can be either partial or complete, depending on the severity of the deadlock and the criticality of the processes involved.
- In some systems, the state of the entire system can be check pointed periodically.
- When a deadlock is detected, the system can roll back to a previously saved checkpoint and restart the processes from that point, avoiding the conditions that led to the deadlock.
- The operating system can prioritize certain processes over others based on their importance or criticality.
- In case of a deadlock, the higher-priority processes are allowed to continue, while lower-priority processes might be terminated or preempted to break the deadlock.
- In database systems, transactions involved in a deadlock can be aborted and then restarted from the beginning.
- By restarting the transactions, they can potentially proceed differently and avoid the conditions that caused the deadlock.
- In distributed systems or systems with multiple resource managers, deadlock recovery may involve intervention from the resource managers.
- Resource managers may collectively analyze the deadlock and resolve it by releasing or reallocating resources across the system.
- One instance of resource type R1
- Two instances of resource type R2
- One instance of resource type R3
- Three instances of resource type R4
- Process P1 is holding an instance of resource type R2 and is waiting for an instance of resource type R1.
- Process P2 is holding an instance of R1 and an instance of R2 and is waiting for an instance of R3.
- Process P3 is holding an instance of R3.
- Ensure that the system will never enter a deadlock state:
- Allow the system to enter a deadlock state and then recover
- Ignore the problem and pretend that deadlocks never occur in the system; used by most operating systems, including UNIX
- 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.
- 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
- Resource-allocation state is defined by the number of available and allocated resources, and the maximum demands of the processes
- When a process requests an available resource, system must decide if immediate allocation leaves the system in a safe state
- System is in safe state if there exists a sequence
of ALL the processes in the systems such that for each Pi , the resources that Pi can still request can be satisfied by currently available resources + resources held by all the Pj , with j < i That is: If Pi resource needs are not immediately available, then Pi can wait until all Pj have finished When Pj is finished, Pi can obtain needed resources, execute, return allocated resources, and terminate When Pi terminates, Pi +1 can obtain its needed resources, and so on If a system is in safe state ⇒ no deadlocks If a system is in unsafe state ⇒ possibility of deadlock Avoidance ⇒ ensure that a system will never enter an unsafe state. Safe, Unsafe, Deadlock State
- Use a resource-allocation graph
- Use the banker’s algorithm
- In addition to the request and assignment edges, we introduce a new type of edge, called a claim edge.
- A claim edge Pi → Rj indicates that process Pi may request resource Rj at some time in the future.
- This edge resembles a request edge in direction but is represented in the graph by a dashed line.
- When process Pi requests resource Rj, the claim edge Pi → Rj is converted to a request edge.
- Similarly, when a resource Rj is released by Pi, the assignment edge Rj → Pi is reconverted to a claim edge Pi → Rj.
- Note that the resources must be claimed a priori in the system. That is, before process Pi starts executing, all its claim edges must already appear in the resource-allocation graph.
- We can relax this condition by allowing a claim edge Pi → Rj to be added to the graph only if all the edges associated with process Pi are claim edges.
- Now suppose that process Pi requests resource Rj. The request can be granted only if converting the request edge Pi → Rj to an assignment edge Rj → Pi does not result in the formation of a cycle in the resource-allocation graph.
- We check for safety by using a cycle-detection algorithm.
- An algorithm for detecting a cycle in this graph requires an order of n^2 operations, where n is the number of processes in the system.
- If no cycle exists, then the allocation of the resource will leave the system in a safe state.
- If a cycle is found, then the allocation will put the system in an unsafe state. In that case, process Pi will have to wait for its requests to be satisfied.
- To illustrate this algorithm, we consider the resource-allocation graph
- Suppose that P2 requests R2. Although R2 is currently free, we cannot allocate it to P2, since this action will create a cycle in the graph.
- A cycle, as mentioned, indicates that the system is in an unsafe state.
- If P1 requests R2, and P2 requests R1, then a deadlock will occur.
- For Multiple instances of a resource type, we have to use Banker’s algorithm.
- When a new process enters the system, it must declare the maximum number of instances of each resource type that it may need. This number may not exceed the total number of resources in the system.
- When a user requests a set of resources, the system must determine whether the allocation of these resources will leave the system in a safe state.
- If it will, the resources are allocated; otherwise, the process must wait until some other process releases enough resources.
- Available. A vector of length m indicates the number of available resources of each type. If Available[j] equals k, then k instances of resource type Rj are available.
- Max. An n × m matrix defines the maximum demand of each process. If Max[i][j] equals k, then process Pi may request at most k instances of resource type Rj .
- Allocation. An n × m matrix defines the number of resources of each type currently allocated to each process. If Allocation[i][j] equals k, then process Pi is currently allocated k instances of resource type Rj.
- Need. An n × m matrix indicates the remaining resource need of each process. If Need[i][j] equals k, then process Pi may need k more instances of resource type Rj to complete its task. Note that Need[i][j] equals Max[i][j]− Allocation[i][j].
- An algorithm that examines the state of the system to determine whether a deadlock has occurred
- An algorithm to recover from the deadlock
- If all resources have only a single instance, then we can define a deadlock detection algorithm that uses a variant of the resource-allocation graph, called a wait-for graph.
- We obtain this graph from the resource-allocation graph by removing the resource nodes and collapsing the appropriate edges.
- A deadlock exists in the system if and only if the wait-for graph contains a cycle.
- To detect deadlocks, the system needs to maintain the wait-for graph and periodically invoke an algorithm that searches for a cycle in the graph.
- An algorithm to detect a cycle in a graph requires an order of n^2 operations, where n is the number of vertices in the graph
- Nodes are processes
- Pi → Pj if Pi is waiting for Pj
- Available. A vector of lengthmindicates the number of available resources of each type.
- Allocation. An n × m matrix defines the number of resources of each type currently allocated to each process.
- Request. An n × m matrix indicates the current request of each process. If Request[i][j] equals k, then process Pi is requesting k more instances of resource type Rj .
We claim that the system is not in a deadlocked state. Indeed, if we execute our algorithm, we will find that the sequence <P0,P2,P3,P1,P4>
We claim that the system is now deadlocked. Although we can reclaim the resources held by process P0, the number of available resources is not sufficient to fulfill the requests of the other processes. Thus, a deadlock exists, consisting of processes P1, P2, P3, and P4.
- When, and how often, to invoke the detection algorithm depends on:
How many processes will be affected by deadlock when it happens?
- If deadlocks occur frequently, then the detection algorithm should be invoked frequently.
- Resources allocated to deadlocked processes will be idle until the deadlock can be broken. The no of processes involved in deadlock cycle may grow.
- If detection algorithm is invoked arbitrarily, there may be many cycles in the resource graph and so we would not be able to tell which of the many deadlocked processes "caused" the deadlock.
- When a detection algorithm determines that a deadlock exists, several alternatives are available.
- One possibility is to inform the operator that a deadlock has occurred and to let the operator deal with the deadlock manually.
- Another possibility is to let the system recover from the deadlock automatically. There are two options for breaking a deadlock.
- One is simply to abort one or more processes to break the circular wait. The other is to preempt some resources from one or more of the deadlocked processes.
- Abort all deadlocked processes – great expensive
- Abort one process at a time until the deadlock cycle is eliminated (minimum cost)
- In which order should we choose to abort?
- Resource Preemption
- Selecting a victim – minimize cost
- Rollback – return to some safe state, restart process for that state
- Starvation – same process may always be picked as victim, include number of rollback in cost factor
















Comments
Post a Comment