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
    COMP3142
    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
    COMP3142›Deadlocks
    Operating SystemsTopic 15 of 34

    Deadlocks

    8 minread
    1,330words
    Intermediatelevel

    Deadlocks in Operating Systems

    A deadlock in operating systems refers to a situation where a set of processes is unable to proceed because each process is waiting for another to release a resource, and none of them can proceed. This creates a circular wait, where each process is in a state of waiting for something that another process holds. Deadlocks can occur in systems that have limited resources, particularly when multiple processes request and hold resources in an overlapping or conflicting manner.

    Understanding and managing deadlocks is a crucial part of designing and operating multi-threaded or multi-process systems, as it can cause the system to become unresponsive or inefficient.


    Conditions for Deadlock

    A deadlock can occur in an environment where all the following four conditions hold simultaneously. These conditions are known as the Coffman conditions:

    1. Mutual Exclusion:

      • A resource is held by only one process at a time. Other processes requesting the same resource must wait until it is released.
    2. Hold and Wait:

      • A process is holding at least one resource and is waiting to acquire additional resources that are being held by other processes.
    3. No Preemption:

      • Resources cannot be forcibly removed from a process holding them. They must be released voluntarily by the process.
    4. Circular Wait:

      • A set of processes {P1, P2, ..., Pn} exists such that each process Pi is waiting for a resource held by the next process Pi+1 in the set, and the last process Pn is waiting for a resource held by P1, forming a cycle.

    These four conditions must all be present for a deadlock to occur. If any one of these conditions is violated, deadlock cannot happen.


    Deadlock Example

    Imagine two processes and two resources (e.g., a printer and a scanner):

    1. Process P1 holds the printer and needs the scanner to proceed.
    2. Process P2 holds the scanner and needs the printer to proceed.

    Both processes are now in a deadlock situation:

    • P1 is waiting for P2 to release the scanner.
    • P2 is waiting for P1 to release the printer.
      Neither can proceed, so the system is stuck in a deadlock.

    Deadlock Prevention

    Deadlock prevention involves taking steps to ensure that at least one of the Coffman conditions does not hold. By breaking one or more of these conditions, we can prevent deadlocks from occurring.

    1. Prevent Mutual Exclusion:

      • In some cases, mutual exclusion can be avoided if resources are shareable (e.g., read-only data or resources). However, many resources are inherently non-shareable (e.g., printers, CPU), so this condition is often difficult to eliminate.
    2. Prevent Hold and Wait:

      • A process can be required to request all resources it needs at once before starting its execution. If all resources cannot be allocated at the same time, the process is blocked until they are all available. This approach reduces the risk of hold and wait but can lead to resource starvation and poor resource utilization.
    3. Prevent No Preemption:

      • If a process is holding a resource and is waiting for additional resources, we can allow the system to preempt the resources from the process. The process would be forced to release its current resources, and it will later request the resources again when they become available. However, this solution may require additional logic and complicate the system.
    4. Prevent Circular Wait:

      • One method to prevent circular wait is to assign a unique ordering to resources. Each process is required to request resources in a predefined order (e.g., always request the printer before the scanner). This ensures that a circular chain of waiting processes cannot form. Another approach is to impose a resource hierarchy, where each process can only request resources in a strictly increasing order.

    Deadlock Avoidance

    Deadlock avoidance involves dynamically checking whether granting a resource request will lead to a potential deadlock. This can be done by analyzing the current state of resource allocation and ensuring that each resource request will not lead the system into an unsafe state.

    One of the most well-known methods for deadlock avoidance is the Banker's Algorithm, which ensures that the system only grants resource requests if they will not result in a deadlock. The Banker's Algorithm is similar to a "safety check" that examines whether there is a safe sequence of processes that can eventually complete without entering a deadlock state.

    Banker's Algorithm

    The Banker's Algorithm works based on the following concepts:

    • Safe State: A state is considered safe if there is at least one sequence of processes that can finish without causing a deadlock. In a safe state, resources are allocated in such a way that each process can eventually complete its execution.
    • Unsafe State: A state is unsafe if no such sequence exists and a deadlock is inevitable.

    In the Banker's Algorithm:

    • Requesting a Resource: When a process requests a resource, the system checks if granting the request will result in a safe state. If it does, the request is granted; otherwise, it is denied.
    • Resource Allocation: If a process holds some resources and requests more, the system evaluates whether the remaining processes can eventually get their required resources and complete.

    Deadlock Detection and Recovery

    When deadlock prevention and avoidance strategies are not feasible or not used, the system must detect deadlocks and recover from them. This involves periodically checking the system for deadlocks and taking actions to resolve them.

    Deadlock Detection

    The system can detect deadlocks by using a wait-for graph, where:

    • Nodes represent processes.
    • Directed edges represent the waiting relationship between processes. An edge from process Pi to process Pj indicates that Pi is waiting for a resource held by Pj.

    A cycle in this graph indicates the presence of a deadlock. If no cycles are detected, the system is not in a deadlock state.

    Deadlock Recovery

    Once a deadlock is detected, the system must take steps to recover from it. The two main approaches to recovery are:

    1. Process Termination:

      • Terminate one or more processes involved in the deadlock. This can be done by terminating the processes involved in the cycle, either one at a time or all at once. The process chosen for termination can be selected based on factors like priority, execution time, or resource usage.
      • Example: If process P1 and P2 are in a deadlock, terminating one of the processes will break the circular wait.
    2. Resource Preemption:

      • Preempt resources from processes involved in the deadlock. This involves forcibly taking resources away from one or more processes and reassigning them to other processes to break the cycle.
      • After preemption, the process may need to roll back its execution to a safe state and reattempt its resource allocation.

    Deadlock in Distributed Systems

    Deadlocks are not limited to single-machine systems. In distributed systems, deadlocks can also occur, but they are more difficult to detect and handle due to the lack of a central coordinator. Distributed deadlock detection involves maintaining information about resource allocation and waiting states across multiple machines, which can be challenging.

    Some distributed systems use distributed algorithms to detect and resolve deadlocks by having processes communicate with each other to determine the existence of a cycle or other indicators of deadlock.


    Conclusion

    Deadlocks represent one of the most challenging problems in concurrent computing, as they can cause a system to come to a halt and reduce its performance. The four Coffman conditions — mutual exclusion, hold and wait, no preemption, and circular wait — must all hold for deadlock to occur.

    While deadlocks can be prevented, avoided, or detected, the approach depends on the specific application and system requirements. Deadlock prevention aims to eliminate one of the Coffman conditions, deadlock avoidance uses algorithms like the Banker's Algorithm to ensure a safe system state, and deadlock detection involves periodically checking for cycles in the wait-for graph. Once a deadlock is detected, the system must recover by either terminating processes or preempting resources to break the cycle and allow the system to continue functioning.

    Previous topic 14
    Synchronization Problems
    Next topic 16
    Detecting and Recovering from Deadlocks

    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,330
      Code examples0
      DifficultyIntermediate