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
    CSI-505
    Progress0 / 20 topics
    Topics
    1. History and Goals2. Evolution of Multi-User Systems3. Process and CPU Management4. Multithreading5. Kernel and User Modes6. Protection7. Problems of Cooperative Processes8. Synchronization9. Deadlocks10. Memory Management and Virtual Memory11. Relocation12. External Fragmentation13. Paging and Demand Paging14. Secondary Storage15. Security and Protection16. File Systems17. I/O Systems18. Introduction to Distributed Operating Systems19. Scheduling and Dispatch20. Introduction to Concurrency
    CSI-505›Deadlocks
    Operating SystemsTopic 9 of 20

    Deadlocks

    8 minread
    1,344words
    Intermediatelevel

    Deadlocks in Operating Systems

    A deadlock in an operating system occurs when a set of processes are blocked indefinitely because they are each waiting for another process in the set to release a resource, causing a circular dependency. In other words, each process in a deadlock is holding a resource and waiting for another resource held by another process, and no process can proceed.

    Deadlocks are a serious problem in multi-processing and multi-threading environments, especially in systems that involve shared resources. If a system enters a deadlock state, it can lead to a halt in system execution or a significant degradation of performance.

    1. Conditions for Deadlock

    Deadlocks can occur if all of the following four conditions, known as the Coffman conditions, are met simultaneously:

    1. Mutual Exclusion: At least one resource is in a non-shareable mode, meaning only one process can access the resource at a time.
    2. Hold and Wait: A process that is holding at least one resource is waiting to acquire additional resources that are currently being held by other processes.
    3. No Preemption: Resources cannot be forcibly taken away from the processes holding them. They must be released voluntarily.
    4. Circular Wait: A set of processes exists such that each process is waiting for a resource held by the next process in the set, forming a circular chain of waiting.

    These four conditions together create a situation where processes cannot make progress, resulting in a deadlock.

    2. Example of Deadlock

    Consider a system with two resources, Resource 1 and Resource 2, and two processes, Process A and Process B:

    • Process A holds Resource 1 and needs Resource 2 to proceed.
    • Process B holds Resource 2 and needs Resource 1 to proceed.

    Now, Process A is waiting for Resource 2 to be released by Process B, and Process B is waiting for Resource 1 to be released by Process A. Since neither process can proceed and both are waiting for each other, a deadlock has occurred.

    3. Types of Deadlock

    • Temporary Deadlock: A situation where deadlock occurs temporarily and is resolved when one of the processes releases a resource or when a timeout occurs.
    • Permanent Deadlock: A situation where deadlock persists indefinitely, and no process ever proceeds unless some form of external intervention is applied (such as killing processes or restarting the system).

    4. Deadlock Prevention, Avoidance, and Detection

    Operating systems use different strategies to deal with deadlocks: prevention, avoidance, and detection.

    a) Deadlock Prevention

    Deadlock prevention aims to eliminate one or more of the Coffman conditions to prevent deadlock from occurring in the system.

    • Breaking Mutual Exclusion: This is difficult because some resources, such as printers or files, inherently need to be used exclusively. Thus, this condition is typically left intact.

    • Breaking Hold and Wait: To prevent hold and wait, a process must either request all of the resources it needs at once (before execution starts) or release all its resources before requesting any new resources. This could lead to inefficiencies, as processes might be forced to wait for resources that they do not need immediately.

    • Breaking No Preemption: Allow preemption of resources. If a process holding some resources is waiting for other resources, the system can preempt the resources it holds and allocate them to other processes.

    • Breaking Circular Wait: This can be avoided by enforcing a strict ordering of resource requests. Each resource is assigned a unique number, and processes are required to request resources in an increasing order of their numbers.

    b) Deadlock Avoidance

    Deadlock avoidance requires the system to make decisions about whether to grant a resource request based on the current state of resource allocation.

    A famous approach for deadlock avoidance is the Banker's Algorithm, which is used to decide whether resource allocation will lead to a safe state (where no deadlock will occur). The system checks if granting a resource request would keep the system in a "safe" state, where all processes can eventually finish execution.

    • Safe State: A state in which there is a way to allocate resources to each process such that every process can eventually finish execution without getting stuck in a circular wait.
    • Unsafe State: A state where there is no guarantee that every process will finish execution, possibly leading to deadlock.

    To determine whether the system is in a safe state, the Banker's algorithm evaluates each resource request by considering whether it can be satisfied while ensuring the system remains in a safe state. If granting the request would lead to an unsafe state, the request is denied.

    c) Deadlock Detection

    Deadlock detection allows deadlocks to occur but actively looks for signs of deadlock in the system, then recovers from them. The system must periodically check the state of resource allocation to identify deadlocks.

    • Wait-for Graphs: A system can maintain a wait-for graph, where nodes represent processes, and directed edges represent waiting relationships between processes and resources. If a cycle is detected in the graph, a deadlock has occurred.

    Example of wait-for graph:

    • If Process A is waiting for Process B to release a resource, and Process B is waiting for Process A to release a resource, a cycle forms in the graph, indicating deadlock.

    • Detection Algorithms: These algorithms check the resource allocation graph for cycles. If a cycle is found, a deadlock has occurred. For example, the Banker's algorithm for deadlock detection can be used to examine whether it’s possible to allocate resources in a way that all processes can eventually complete.

    Recovery from Deadlock: Once a deadlock is detected, the system must recover. Recovery strategies include:

    • Process Termination: The simplest approach is to kill one or more processes involved in the deadlock. This removes the circular wait and allows other processes to proceed.
    • Resource Preemption: The system may forcibly take resources from processes involved in the deadlock. This can be done by preempting resources and rolling back processes to a safe state.

    d) Deadlock Avoidance vs. Detection

    • Deadlock Prevention focuses on making deadlock impossible by denying one of the Coffman conditions.
    • Deadlock Avoidance ensures that resource allocation decisions do not lead to a deadlock.
    • Deadlock Detection allows deadlocks to occur but periodically checks for them and then attempts to resolve the situation.

    5. Strategies for Deadlock Resolution

    Once a deadlock is detected, the operating system needs to choose one of the following strategies for resolution:

    • Process Termination: Kill one or more processes involved in the deadlock. The process can be chosen in several ways:

      • Terminate all processes involved in the deadlock.
      • Terminate one process at a time, selecting the least important or the process with the least amount of resources allocated.
    • Resource Preemption: Take resources away from one or more processes involved in the deadlock. This can be tricky because it may require rolling back processes to a safe state, and could potentially create starvation if the preempted process is repeatedly unable to get resources.

    6. Example of Deadlock Detection with a Resource Allocation Graph

    Consider a system with three resources (A, B, C) and four processes (P1, P2, P3, P4):

    • P1 holds A, waits for B.
    • P2 holds B, waits for C.
    • P3 holds C, waits for A.
    • P4 is not involved in the deadlock.

    The resource allocation graph would show a cycle between P1 → P2 → P3 → P1. This cycle indicates a deadlock, as each process in the cycle is waiting for a resource that is held by another process in the cycle.

    Conclusion

    Deadlocks are a critical issue in operating systems that can halt system execution or cause significant delays. To handle deadlocks, operating systems use strategies like deadlock prevention, deadlock avoidance, and deadlock detection. Deadlock prevention eliminates one of the necessary conditions for deadlock, deadlock avoidance ensures the system stays in a safe state, and deadlock detection periodically checks for deadlocks and attempts to recover from them. Effective deadlock management is crucial for maintaining the performance and reliability of a system.

    Previous topic 8
    Synchronization
    Next topic 10
    Memory Management and Virtual Memory

    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 time8 min
      Word count1,344
      Code examples0
      DifficultyIntermediate