ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Operating Systems
    CC-211
    Progress0 / 34 topics
    Topics
    1. Operating Systems Basics2. System Calls3. Process Concept and Scheduling4. Interprocess Communication5. Multithreaded Programming6. Multithreading Models7. Threading Issues8. Process Scheduling Algorithms9. Thread Scheduling10. Multiple-Processor Scheduling11. Synchronization12. Critical Section13. Synchronization Hardware14. Synchronization Problems15. Deadlocks16. Detecting and Recovering from Deadlocks17. Memory Management18. Swapping19. Contiguous Memory Allocation20. Segmentation and Paging21. Virtual Memory Management22. Demand Paging23. Thrashing24. Memory-Mapped Files25. File Systems26. File Concept27. Directory and Disk Structure28. Directory Implementation29. Free Space Management30. Disk Structure and Scheduling31. Swap Space Management32. System Protection33. Virtual Machines34. Operating System Security
    CC-311›Detecting and Recovering from Deadlocks
    Operating SystemsTopic 16 of 34

    Detecting and Recovering from Deadlocks

    9 minread
    1,454words
    Intermediatelevel

    Detecting and Recovering from Deadlocks in Operating Systems

    Deadlock detection and recovery are crucial components of deadlock management in operating systems, especially when strategies like deadlock prevention or deadlock avoidance are not feasible or desirable. The aim is to identify deadlocks when they occur and take appropriate actions to recover from them without leading to system instability or resource wastage.


    Deadlock Detection

    Deadlock detection refers to the process of identifying whether a system is in a deadlock state, where a set of processes is unable to make progress due to waiting for resources held by each other. The system needs to periodically check whether a deadlock has occurred and then initiate recovery procedures if a deadlock is detected.

    Methods of Deadlock Detection

    1. Wait-for Graph (WFG):

      • One of the simplest methods for detecting deadlocks is by constructing a wait-for graph. In this directed graph:
        • Nodes represent processes.
        • Edges represent the waiting relationship between processes. An edge from process P1 to process P2 means that P1 is waiting for a resource currently held by P2.
      • Deadlock Condition: A cycle in the wait-for graph indicates a deadlock. If a process is waiting for another process, and that process is also waiting for the first process (or for any other process in the cycle), a deadlock is present.

      Steps for Detection:

      • Build a wait-for graph by examining the current state of processes and their resource requests.
      • Check for cycles in the graph. If a cycle is detected, a deadlock exists, as it means that processes in the cycle are waiting on each other.

      Example:

      • If P1 is waiting for a resource held by P2, and P2 is waiting for a resource held by P3, and P3 is waiting for a resource held by P1, we have a cycle: P1 → P2 → P3 → P1. This indicates a deadlock.
    2. Resource Allocation Graph (RAG):

      • Another common approach is using a resource allocation graph. This graph tracks both processes and resources. The key difference between a wait-for graph and a resource allocation graph is that, in a RAG:
        • Nodes represent both processes and resources.
        • Edges:
          • A directed edge from a process to a resource indicates that the process is holding that resource.
          • A directed edge from a resource to a process indicates that the process is waiting for that resource.

      Deadlock Detection using the RAG:

      • A cycle in a Resource Allocation Graph indicates a deadlock. The cycle implies that processes are stuck in a waiting situation where they are unable to proceed because they are waiting on resources that are already being held by other processes in the cycle.
    3. Banker's Algorithm (for Avoidance and Detection):

      • The Banker's Algorithm is typically used for deadlock avoidance, but it can also be adapted for deadlock detection by comparing the system's current allocation state against safe and unsafe states.
      • In this approach, the system checks if there is a safe sequence of processes that can eventually finish without causing a deadlock. If no such sequence exists, the system is in an unsafe state, and deadlock detection can be triggered.

    Deadlock Recovery

    Once a deadlock has been detected, the operating system must recover from it. There are two main strategies for recovery: process termination and resource preemption.

    1. Process Termination

    One method for recovering from deadlock is to terminate one or more processes involved in the deadlock to break the circular wait and allow the system to proceed. There are two primary ways to terminate processes:

    • Terminate All Deadlocked Processes: In this approach, the system terminates all the processes involved in the deadlock. This method is simple but can be costly, especially if a large number of processes are involved.
    • Terminate One Process at a Time: Alternatively, the system may choose to terminate processes one by one until the deadlock cycle is broken. When choosing which process to terminate, the system can consider various criteria, such as:
      • Priority: Terminate the process with the lowest priority.
      • Execution Time: Terminate the process that has executed the least amount of time.
      • Resource Utilization: Terminate the process that holds the most resources, or one that is holding resources that other processes need.
    Example of Process Termination:

    If processes P1, P2, and P3 are involved in a deadlock, one of the processes (say P1) is chosen for termination. Once P1 is terminated, resources it held are released, which allows other processes to continue executing, and the system recovers from the deadlock.

    2. Resource Preemption

    Resource preemption involves forcibly taking resources away from processes that are currently holding them. After preemption, the resources are reallocated to other processes, potentially breaking the deadlock cycle. The preempted process will be restarted later when the necessary resources are available.

    Steps in Resource Preemption:
    • Identify the preempted process: The system must determine which process should be preempted. This decision can be made based on various factors, such as process priority, resource usage, or execution time.
    • Preempt resources: The resources held by the preempted process are forcibly released and made available to other processes. The preempted process is then placed in a waiting state.
    • Rollback if necessary: If the preempted process has already done some work (e.g., modified data or made decisions), the process may need to rollback to a safe state to avoid inconsistencies.
    Example of Resource Preemption:

    In the case of P1, P2, and P3 involved in a deadlock, the system may decide to preempt some resources from P1 and give them to P2 or P3, allowing them to complete their execution. Afterward, P1 can resume or be restarted once the resources it needs are available.

    3. Rollback

    Sometimes, it is possible to rollback a process to a previous checkpoint to undo the actions it has taken so far, especially if preemption alone is not enough to resolve the deadlock. By rolling back the process to a previous state, the system can eliminate the conditions leading to the deadlock.

    Rollback can involve:

    • State saving: The system saves checkpoints at certain intervals during the execution of processes. If a deadlock occurs, the process is rolled back to the most recent checkpoint.
    • State restoration: After rollback, the process resumes execution as if the deadlock had never occurred.

    4. Combination of Methods

    In many cases, a combination of the above methods is used for efficient deadlock recovery. For example, the system may first try resource preemption to break the deadlock cycle. If that is not successful, the system may choose to terminate some processes or rollback processes to recover.


    Deadlock Detection and Recovery in Distributed Systems

    Deadlock detection and recovery are even more challenging in distributed systems due to the following reasons:

    • Lack of a central authority: In distributed systems, there is no global memory or central point to track resource allocation.
    • Communication overhead: Detecting deadlocks in distributed systems often requires processes to communicate with each other to exchange resource and wait-for information.

    Approaches for Distributed Deadlock Detection:

    1. Distributed Wait-for Graph: In distributed systems, each process maintains its own local wait-for graph and exchanges information with other processes to construct a global view of the system’s resource allocation. If a cycle is detected in this global view, deadlock is present.
    2. Centralized Deadlock Detection: A central coordinator can collect resource allocation information from all processes, construct a global wait-for graph, and detect deadlocks. However, this approach introduces a single point of failure and additional communication overhead.
    3. Distributed Algorithms (e.g., Chandy-Misra-Haas Algorithm): This is a well-known algorithm for distributed deadlock detection. It works by having processes exchange messages to detect cycles in the distributed wait-for graph.

    Recovery in Distributed Systems:

    • In distributed systems, the recovery process typically involves process termination or rollback, similar to centralized systems. However, distributed systems often require compensating actions to ensure consistency across all involved systems.

    Conclusion

    Deadlock detection and recovery are critical aspects of managing concurrency in operating systems. While deadlock prevention and avoidance aim to prevent deadlocks from occurring, deadlock detection and recovery deal with situations where deadlocks do occur.

    • Detection typically involves the use of wait-for graphs or resource allocation graphs to identify cycles.
    • Recovery from deadlocks involves strategies like process termination, resource preemption, and rollback. The choice of recovery method depends on factors like the severity of the deadlock, system requirements, and resource availability.

    In distributed systems, detecting and recovering from deadlocks is more complex due to the lack of a central controller, requiring specialized algorithms and techniques. Regardless of the approach, efficient deadlock detection and recovery mechanisms are essential for ensuring the stability and performance of concurrent systems.

    Previous topic 15
    Deadlocks
    Next topic 17
    Memory Management

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time9 min
      Word count1,454
      Code examples0
      DifficultyIntermediate