OS_Deadlockpdf
OS_Deadlockpdf
• Deadlock
• Deadlock Problem & Example- Bridge Crossing Problem
• The cause, System Model & Normal Resource Allocation Operation
• Necessary conditions for deadlock
• Methods for handling Deadlock
• Resource Allocation Graph (RAG)
• Deadlock Prevention
• Deadlock Avoidance
• Recovery from Deadlock
Deadlock:
• A deadlock is a situation wherein
Resource-1 two or more competing actions are
each waiting for the other to finish,
Assigned to Waiting for and thus neither ever does. It is
often seen in a paradox like the
"chicken or the egg".
Process-1 Process-2
• It is a condition in which two
computer programs sharing the
Waiting for Assigned to same resource are effectively
preventing each other from
Resource-2 accessing the resource, resulting in
both programs ceasing to function.
Deadlock Problem:
Deadlock refers to a specific condition when two or more processes are
each waiting for each other to release a resource, or more than two
processes are waiting for resources in a circular chain.
Example
• Client applications using the database may require exclusive access to
a table.
• Two client applications C1 and C2.
• If C1 holds lock on table, attempts to obtain on the other i.e. already
held by C2.
• This may lead to deadlock if the C2 application then attempts to
obtain the lock that is held by the C1 application.
Example- Bridge Crossing
System Model:
A system consists of a finite no. of resources for no. of competing processes:
✓ Resource types R1, R2, . . ., Rm
✓ CPU cycles, memory space, files and I/O devices
Normal Resource Allocation Operation
Each resource type Ri has Wi instances. Each process utilizes a resource
as follows:
Pi Pi Pi
Rj Rj
• If the graph contains no
cycles, then no process is R3 Assigned to P3
deadlocked.
• If there is a cycle, then:
➢ If resource types have multiple
P2 Requests P3
instances per resource type,
then possibility of deadlock
MAY exist.
➢ If each resource type has one
instance per resource type,
then deadlock has occurred.
Resource allocation graph
Resource Allocation Graph with a Resource Allocation Graph with a Cycle
Deadlock But No Deadlock
Deadlock Prevention
Do not allow one of the four conditions to occur.
1. Mutual exclusion:
▪ Automatically holds for printers and other non-sharables.
▪ Shared entities (read only files) don't need mutual exclusion (and aren’t
susceptible to deadlock.)
▪ Prevention not possible, since some devices are intrinsically non-sharable
2. Hold and wait:
▪ Collect all resources before execution.
▪ A particular resource can only be requested when no others are being held. A
sequence of resources is always collected beginning with the same one.
▪ Utilization is low, starvation possible.
Deadlock Prevention (contd..)
Do not allow one of the four conditions to occur.
3. No pre-emption:
▪ Release any resource already being held if the process can't get an additional
resource.
▪ Allow pre-emption - if a needed resource is held by another process, which is
also waiting on some resource, steal it. Otherwise wait.
4. Circular wait:
▪ Number resources and only request in ascending order.
• When a process gets all of its resources it must return them in a finite
amount of time.
Data Structures for Banker’s Algorithm
• Let n = number of processes and m = number of resources types
• Available: Vector of length m. If Available[j] = k, there are k instances
of resource type Rj available.
• Max: n x m matrix. If Max [i,j] = k, then process Pi may request at
most k instances of resource type Rj.
• Allocation: n x m matrix. If Allocation[i,j] = k then Pi is currently
allocated k instances of Rj.
• Need: n x m matrix. If Need[i,j] = k, then Pi may need k more
instances of Rj to complete its task.
• Need [i,j] = Max[i,j] – Allocation [i,j].
Safety Algorithm:
• Let Work and Finish be vectors of length m and n, respectively.
Initialize:
• Work = Available
• Finish [i] = false for i = 0, 1, …, n- 1.
• Find and i such that both:
• (a) Finish [i] = false
• (b) Needi Work
• If no such i exists, go to step 4.
• Work = Work + Allocationi
Finish[i] = true
go to step 2.
• If Finish [i] == true for all i, then the system is in a safe state.
Resource Request Algorithm
Process Pi
Request = request vector for process Pi. If Requesti[j] = k then process Pi wants k
instances of resource type Rj.
• If Requesti Needi go to step 2. Otherwise, raise error condition, since process
has exceeded its maximum claim.
• If Requesti Available, go to step 3. Otherwise Pi must wait, since resources are
not available.
• Pretend to allocate requested resources to Pi by modifying the state as follows:
Available = Available – Request;
Allocationi = Allocationi + Requesti;
Needi = Needi – Requesti;
• If safe the resources are allocated to Pi.
• If unsafe Pi must wait, and the old resource-allocation state is restored
Deadlock Detection:
• Allow system to enter deadlock state
• Detection algorithm
• Recovery scheme
• Rollback – return to some safe state, restart process for that state.