Basics of Deadlock
What is a deadlock?
Consider two processes, P1 and P2, each requiring two resources, R1 and R2, to execute. Currently, P1 is holding R1, and P2 is holding R2. However, neither P1 nor P2 can continue their execution without acquiring both resources. Since P1 is waiting for R2 (held by P2), and P2 is waiting for R1 (held by P1), this creates a deadlock situation where both processes are stuck, and unable to proceed.
How does a process or thread utilize a resource?
- Request: The process or thread first checks if the resource (e.g., R) is available. This is often done by attempting to acquire a lock or checking the status of the resource.
- If the resource is locked: This means another process or thread is currently using it, so the requesting process must wait.
- If the resource is not locked: The process can acquire the lock, indicating that it now has control over the resource.
- Use: Once the resource is acquired, the process or thread uses it to perform its task. During this time, the resource remains locked to prevent other processes or threads from accessing it simultaneously.
- Release: After completing the task, the process or thread releases the resource. This typically involves unlocking the resource and making it available for other processes or threads to use.
Necessary conditions for deadlock
1. Mutual Exclusion: In the mutual exclusion condition, only one process or thread can use a resource at a time. If a resource is already being used by one process, any other process that requests it must wait until the resource is released.
2. Hold and wait: This condition occurs when a process is holding at least one resource and is waiting to acquire additional resources that are currently being held by other processes.
3. No Preemption: This condition means that once a resource (like R1) has been allocated to a process (like P1), it cannot be forcibly taken away by the operating system or another process. The process that holds the resource will only release it when it has completed its task with that resource.
4. Circular wait: This condition occurs when a group of processes are each waiting for a resource that is held by the next process in the sequence. The "circle" means that the last process in the chain is waiting for a resource held by the first process, creating a loop of dependencies.
Resource allocation block
Methods to handle deadlock
1. Deadlock Prevention or Avoidance:
- Objective: Ensure that the system never enters a deadlock state.
- Prevention: The system uses strategies to prevent one or more of the necessary conditions for deadlock (mutual exclusion, hold and wait, no preemption, circular wait) from occurring.
2. Deadlock Detection and Recovery:
- Objective: Allow the system to enter a deadlock state but have mechanisms to detect it and recover from it.
- Detection: The system periodically checks for deadlocks by analyzing the resource allocation graph or using algorithms designed for detecting cycles in the graph.
3. Ostrich Algorithm:
- Objective: Ignore the problem of deadlocks.
- Approach: It assumes that the application programmer is responsible for ensuring that deadlocks do not occur in their code.


Comments
Post a Comment