0% found this document useful (0 votes)
23 views4 pages

The Deadlock Problem: Lock (A) Lock (B) Lock (B) Lock (A)

The document discusses the deadlock problem in computer systems. It describes several examples of deadlock including processes blocking on resources and philosophers blocking while eating. It also covers the concepts of resource allocation graphs, deadlock prevention, avoidance and detection techniques.

Uploaded by

hoang.van.tuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views4 pages

The Deadlock Problem: Lock (A) Lock (B) Lock (B) Lock (A)

The document discusses the deadlock problem in computer systems. It describes several examples of deadlock including processes blocking on resources and philosophers blocking while eating. It also covers the concepts of resource allocation graphs, deadlock prevention, avoidance and detection techniques.

Uploaded by

hoang.van.tuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

The deadlock problem

n A set of blocked processes each holding a resource and


waiting to acquire a resource held by another process in
the set.
Deadlocks n Example
n System has 2 tape drives.
n P1 and P2 each hold one tape drive and each needs another one.
n Example
Arvind Krishnamurthy n locks A and B

Spring 2001
P0 P1
lock (A); lock (B)
lock (B); lock (A)

The bridge-crossing example The dining philosophers problem


n Five philosophers around a table --- thinking or eating
n Five plates of spaghetti + five forks (placed between each plate)
n Each philosopher needs two forks to eat the spaghetti

void philosopher (int i) {


while (TRUE) {
n Traffic only in one direction.
think();
n Each section of a bridge can be viewed as a resource. take_fork (i);
n If a deadlock occurs, it can be resolved if one car backs up take_fork ((i+1) % 5);
(preempt resources and rollback). eat();
put_fork (i);
n Several cars may have to be backed up if a deadlock occurs. put_fork ((i+1) % 5);
n Starvation is possible. }
}

System model Resource-allocation graph


n Resource types R1, R2, . . ., Rm CPU cycles, memory space, I/O devices A set of vertices V and a set of edges E.
n Each resource type Ri has Wi instances.
n Each process utilizes a resource as follows: request, use, release n V is partitioned into two types:
n Deadlock can arise if four conditions hold simultaneously: n P = {P1, P2, …, Pn }, the set consisting of all the
processes in the system.
n Mutual exclusion: only one process at a time can use a resource.

n Hold and wait: a process holding at least one resource is waiting n R = {R1, R2, …, Rm }, the set consisting of all resource
to acquire additional resources held by other processes. types in the system.

n No preemption: a resource can be released only voluntarily by


the process holding it, after that process has completed its task. n request edge – directed edge P1 → Rj

n Circular wait: there exists a set {P0, P1, …, Pn} of waiting n assignment edge – directed edge Rj → Pi
processes such that Pi is waiting for a resource that is held by Pi+1,
Pn is waiting for a resource that is held by P0.

1
Resource Allocation Graph Deadlocked Resource Allocation

n Process

n Resource type with 4 instances

n Pi requests instance of Rj

Pi
Rj
n Pi is holding an instance of Rj
Pi
Rj

Graph with cycle but no deadlock Methods for handling deadlocks


n Ensure that the system will never enter a deadlock state.
(deadlock prevention and avoidance)
n problems: low device utilization, reduced throughput
n avoidance also requires prediction of resource needs

n Allow the system to enter a deadlock state and then


recover. (deadlock detection and recovery)
n costly; sometimes impossible to recover

nIf graph contains no cycles ⇒ no deadlock


n Ignore the problem and pretend that deadlocks never
occur in the system; used by most operating systems,
nIf graph contains a cycle ⇒ including UNIX.
n if only one instance per resource type, then deadlock.
n if several instances per resource type, possibility of deadlock.

Deadlock prevention (1) Deadlock prevention (2)


n No Preemption –
Restrain the ways request can be made.
n If a process that is holding some resources requests another
n Mutual Exclusion – resource that cannot be immediately allocated to it, then all
resources currently being held are released
n not required for sharable resources; must hold for
nonsharable resources. n Preempted resources are added to the list of resources for which
the process is waiting
n Process will be restarted only when it can regain its old resources,
n Hold and Wait – as well as the new ones that it is requesting
n must guarantee that whenever a process requests a
resource, it does not hold any other resources.
n Circular Wait –
n Require process to request and be allocated all its
resources before it begins execution (or allow process n impose a total ordering of all resource types, and require that each
to request resources only when the process has none) process requests resources in an increasing order of enumeration
n Low resource utilization; starvation possible.

2
Deadlock avoidance Safe state
n When a process requests an available resource, system must decide if
Requires that the system has some additional a priori information
immediate allocation leaves the system in a safe state.
available.

n System is in safe state if there exists a safe sequence of all processes.


n Each process declares the maximum number of resources
of each type that it may need. n Sequence <P1, P2, …, Pn > is safe if for each Pi, the resources that Pi
can still request can be satisfied by currently available resources +
n The deadlock-avoidance algorithm dynamically examines resources held by all the Pj, with j<i.
the resource-allocation state to ensure that there can n If Pi resource needs are not immediately available, then Pi can wait until all
never be a circular-wait condition. Pj have finished.
n When Pj is finished, Pi can obtain needed resources, execute, return
allocated resources, and terminate.
n Resource-allocation state is defined by the number of
n When Pi terminates, Pi+1 can obtain its needed resources, and so on.
available and allocated resources, and the maximum
demands of the processes.

Resource-allocation graph
Deadlock avoidance algorithm
n If a system is in safe state ⇒ no deadlocks. n Claim edge Pi → Rj indicated that process Pi may request
resource Rj ; represented by a dashed line.
n If a system is in unsafe state ⇒ possibility of deadlock.
n Claim edge converts to request edge when a process
n Avoidance ⇒ ensure that a system will never enter an requests a resource.
unsafe state.
n When a resource is released by a process, assignment
edge reconverts to a claim edge.

n Resources must be claimed a priori in the system.

Resource Allocation Graph Banker’s algorithm: a summary


n Multiple instances.
Safe state Unsafe state n Each process must a priori claim maximum use.
n When a process requests a resource it may have to wait.
n When a process gets all its resources it must return them in
a finite amount of time.

3
Data Structures for Banker’s Algo Banker’s algorithm: safety test
Let n = number of processes, and m = number of resources types.
1. Let Work and Finish be vectors of length m and n, respectively.
n Available: Vector of length m. If available [j] = k, there are k Initialize:
instances of resource type Rj available. Work := Available
Finish [i] = false for i =1,2, …, n.
n Max: n x m matrix. If Max [i,j] = k, then process Pi may
request at most k instances of resource type Rj.
2. Find an i such that both:
n Allocation: n x m matrix. If Allocation[i,j] = k then Pi is (a) Finish [i] = false
currently allocated k instances of Rj. (b) Needi ≤ Work
If no such i exists, go to step 4.
n Need: n x m matrix. If Need[i,j] = k, then Pi may need k
more instances of Rj to complete its task.
3. Work := Work + Allocationi
Finish[i] := true
Need [i,j] = Max[i,j] – Allocation [i,j]. go to step 2.

4. If Finish [i] = true for all i, then the system is in a safe state.

Resource Request Deadlock detection and recovery


Requesti = request vector for process Pi. If Requesti [j] = k then n Allow system to enter deadlock state
process Pi wants k instances of resource type Rj. n Detection algorithm
1. If Requesti ≤ Needi go to step 2. Otherwise, raise error condition, n use wait-for graph if single instance of each resource type
since process has exceeded its maximum claim. n Nodes are processes.

2. If Requesti ≤ Available, go to step 3. Otherwise Pi must wait, since n Pi → Pj if Pi is waiting for Pj .

resources are not available. n periodically searches for a cycle in the graph; when and how often
3. Pretend to allocate requested resources to Pi by modifying the state depends on:
as follows: n How often a deadlock is likely to occur?

Available := Available - Requesti; n How many processes will need to be rolled back

Allocationi := Allocationi + Requesti; n hard if multiple instances of each resource type


Needi := Needi – Requesti;; n Recovery scheme
• If safe ⇒ the resources are allocated to Pi. n process termination
• If unsafe ⇒ Pi must wait, and the old resource-allocation state n resource premption
is restored

Waits-For Graph Combined Approach


n Combine the three basic approaches
n prevention
n avoidance
n detection
allowing the use of the optimal approach for each of
resources in the system.

n Partition resources into hierarchically ordered classes.

n Use most appropriate technique for handling deadlocks


Corresponding wait-for graph
within each class.
Resource-Allocation Graph

You might also like