DEADLOCK
Objectives:
To illustrate how deadlock can occur when mutex locks are used
To Define the four necessary conditions that characterize deadlock
To identify a deadlock situation in a resource allocation graph
To evaluate the four different approaches for preventing deadlocks
To apply the banker’s algorithm for deadlock avoidance
To apply the deadlock detection algorithm Learning Outcomes:
Understand what deadlock is and how it can occur when giving mutually exclusive access to multiple resources.
Understand several approaches to mitigating the issue of deadlock in operating systems.
Prerequisites:
In an operating system, a deadlock occurs when a process or thread enters a waiting state because a requested system resource is held by another waiting process, which in turn is waiting for another resource held by another waiting process.
Plan for the lecture delivery
1. Teaching aid both Blackboard and Presentation Via LCD 2. Topic should be start with Definition, System Model as follows:
What is Deadlock?
A process requests resources, if the resources are not available at the time, the process enters a wait state.
Waiting process may never again change state, because the resources they have requested are held by other waiting processes.
This situation is called a Deadlock The Deadlock Problem
A set of blocked processes each holding a resource and waiting to acquire a resource held by another process in the set
Example
System has 2 disk drives
P1 and P2 each hold one disk drive and each needs another one System Model
A system consists of a finite number of resources to be distributed amount number of competing processes.
Examples of resource types
Memory Space
CPU Cycles,
Files
I/O Devices (Such as printers and tape drivers)
Each process utilizes a resource as follows:
request
use
release
A Process must request a resource before using it, and must release the resources after using it.
A process may request as many resources as it requires to carryout its designated task.
Number of resources requested may not exceed the total number of resources available in the system. (Example : A process cannot request three printers if the system has only two)
A System table records whether each resource is free or allocated, and, if resource is allocated, to which process.
If a Process requests a resource that is currently allocated to another process, it can be added to a queue of processes waiting for this resources.
Example
A system with one printer and one tape drive. Suppose that process Pi is holding the tape drive and process Pj is holding the printer.
If Pi requests the printer and Pj requests the tape drive a deadlock occurs.
3. Practical Reality of the topic can be explained with the following:
Example of a Resource Allocation Graph
The resource allocation graph shows in figure depicts the following The sets P,R and E
P = {P1, P2, P3}
R = {R1,R2,R3,R4}
E = { P1 R1, P2 R3, R1 P2, R2 P2, R2 P1, R3 P3}
Resource instance
one instance of resource type R1 Two instance of resource type R2 One instance of resource type R3 Three instances of resource type R4 Resource Allocation Graph With A Deadlock
The resource allocation graph shows in figure depicts the following P1 R1 P2 R3 P3 R2 P1
P2 R3 P3 R2 P2
Process P1, P2 and P3 are deadlock. Process P2 Is waiting for the resource R3, which is held by Process P3.
Process P3, on the other hand, is waiting for either Process P1 or P2 to release resource R2
In addition, process P1 is waiting for process P2 to release Resource R1
4. Comparison between Deadlock Avoidance, Prevention & Recovery:
Deadlock is one of the major disputes encountered in operating system. It occurs due to ineffective allocation of resources to the processes. To prevent the system from deadlock, existing algorithms and techniques for deadlock prevention, recovery and avoidance have been analyzed
Deadlock Prevention
Hold and Wait – must guarantee that whenever a process requests a resource, it does not hold any other resources
Example: A process that copies data from a tape drive to a disk file, sorts the disk file and then prints the results to a printer.
The process must then again request the disk file and the printer. After Copying the disk file to the printer, it releases theses two resources and terminates.
Disadvantage of this method is
Low resource utilization; starvation possible
No Preemption –
The Third necessary condition is that there be no preemption of resources that have already been allocated.
If a process is holding some resources and requests another resources that cannot be immediately allocated to it, then all resources currently being held are
preempted.
If process requests some resources, we first check whether they are available. If they are, we allocate them.
If they are not available, we check whether they are allocated to some other process that is waiting for additional resources If so, we preempt the desired resources from the waiting processes and allocate them to the requesting process.
Circular Wait
The final condition for deadlocks is the circular-wait condition.
One way to ensure that this condition never holds is to impose a total ordering of all resource types, and to require that each process requests resources in an increasing order
Let R = {R1, R2,…RM} be the set of resource type. We assign to each resource type a unique integer number, which allows us to compare two resources and to determine whether one precedes another in our ordering.
We define one-to-one function F: R N, where N is the set of natural number.
F (tape drive ) = 1 F (disk drive ) = 5 F (Printer ) = 12
Since the resources are not available.
5. Conclusion:
The best deadlock control method depends on the database environment, if the probability is low, deadlock detection is recommended, if probability is high, deadlock prevention is recommended and if response time is not high on the system priority list deadlock avoidance might be employed.
6. Points in nutshell:
We can deal with the deadlock problem in one of three ways:
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
7. Points for understanding
Request: If the request cannot be granted immediately, then the requesting process must wait until it can acquire the resource.
Use: The process can operate on the resources
Release: The process release the resource after complete the process
8. Sample Question & Answer:
1.A system has 12 magnetic tape drives and 3 processes : P0, P1, and P2. Process P0 requires 10 tape drives, P1 requires 4 and P2 requires 9 tape drives.
Process P0
P1 P2
Maximum needs (process-wise: P0 through P2 top to bottom) 10
4 9
Currently allocated (process-wise) 5
2 2
Which of the following sequence is a safe sequence?
a) P0, P1, P2 b) P1, P2, P0 c) P2, P0, P1 d) P1, P0, P2 Ans: d
2. For a Hold and wait condition to prevail ____________
a) A process must be not be holding a resource, but waiting for one to be freed, and then request to acquire it
b) A process must be holding at least one resource and waiting to acquire additional resources that are being held by other processes
c) A process must hold at least one resource and not be waiting to acquire additional resources
d) None of the mentioned Ans: b
3,Deadlock prevention is a set of methods ____________
a) to ensure that at least one of the necessary conditions cannot hold b) to ensure that all of the necessary conditions do not hold
c) to decide if the requested resources for a process have to be given or not
d) to recover from a deadlock Ans:a
4.Each request requires that the system consider the _____________ to decide whether the current request can be satisfied or must wait to avoid a future possible deadlock.
a) resources currently available
b) processes that have previously been in the system c) resources currently allocated to each process d) future requests and releases of each process Ans: a
5.Given a priori information about the ________ number of resources of each type that maybe requested for each process, it is possible to construct an algorithm that ensures that the system will never enter a deadlock state.
a) minimum b) average c) maximum d) approximate Ans:c
9. Application of deadlock
One connection performs a select and the other connection performs updates or deletes on the same tables. This can create application deadlocks. For example:
Connection A holds a shared lock on a page. As long as there are rows pending from Adaptive Server, a shared lock is kept on the current page.
Connection B requests an exclusive lock on the same pages and then waits.
The application waits for Connection B to succeed before invoking the logic needed to remove the shared lock. But this never happens.
Since Connection A never requests a lock that is held by Connection B, this is not a server-side deadlock.