0% found this document useful (0 votes)
7 views9 pages

Deadlock

Deallocking system tencno

Uploaded by

ibrahimiliyas.ki
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)
7 views9 pages

Deadlock

Deallocking system tencno

Uploaded by

ibrahimiliyas.ki
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/ 9

DEADLOCK

In a multiprogramming environment, several processes may compete for a finite number


of resources. A process requests resources; if the resources are not available at that
time, the process enters a waiting state. Sometimes, a waiting process is never again
able to change state, because the resources it has requested are held by other waiting
processes. This situation is called a deadlock.

System Model:
● A system consists of a finite number of resources to be
distributed among a number of competing processes
● The resources are partitioned into several types, each
consisting of some number of identical instances.
● If a process requests an instance of a resource type, the allocation of any
instance of the type will satisfy the request.
A process may utilize a resource in the following sequence:
● Request: The process requests the resource. If the request cannot be granted
immediately (for example, if the resource is being used by another process), then
the requesting process must wait until it can acquire the resource.
● Use: The process can operate on the resource (for example, if the resource is a
printer, the process can print on the printer).
● Release: The process releases the resource.

Necessary condition for Deadlock:


1. Mutual exclusion: At least one resource must be held in a non-sharable
mode; that is, only one process at a time can use the resource. If another
process requests that resource, the requesting process must be delayed until the
resource has been released.
2. Hold and wait: A process must be holding at least one resource and waiting
to acquire additional resources that are currently being held by other processes.
3. No preemption: Resources cannot be preempted; that is, a resource can be
released only voluntarily by the process holding it, after that process has
completed its task.
4. Circular wait: A set { P0 , Pl, ... , P11 } of waiting processes must exist such
that Po is waiting for a resource held by P1, P1 is waiting for a resource held by
P2, ... , Pn-1 is waiting for a resource held by Pn, and Pn is waiting for a resource
held by Po.

Resources Allocation Graph:


● Deadlocks can be described more precisely in terms of a directed graph called
Resource Allocation Graph.
● The set of vertices V is partitioned into two different types of nodes:P { P1, P2, ...
, Pn}, the set of all the active processes in the system, and R {R1, R2, ... , Rm},
the set of all resource types in the system.
● Directed edge Pi →Rj is called a request edge.It signifies that process Pi
requested an instance of resource type Rj and is currently waiting for that
resource.
● A directed edge Rj→Pi is called an assignment edge. it signifies that an
instance of resource type Rj has been allocated to process Pi.

● Pictorially, it represents each process Pi as a circle and each resource


type Rj as a rectangle. Since resource type Rj may have more than one
instance, it represents each such instance as a dot within the rectangle.

Deadlock Handling Methods:


● We can use a protocol to prevent or avoid deadlocks, ensuring that the system
will never enter a deadlocked state.
● We can allow the system to enter a deadlocked state, detect it, and recover.
● We can ignore the problem altogether and pretend that deadlocks never occur in
the system.
Deadlocks can be handled in three different ways:
● Deadlock Prevention
● Deadlock Avoidance
● Deadlock Detection and Recovery

Deadlock Prevention:
● For a deadlock to occur, each of the four necessary conditions must hold:

● Mutual Exclusion
● Hold and Wait
● No Preemption
● Circular Wait

● By ensuring that at least one of these conditions cannot hold, we can


prevent the occurrence of a deadlock.

Mutual Exclusion:
● The mutual-exclusion condition must hold for non-sharable resources. For
example, a printer cannot be simultaneously shared by several processes.
● Sharable resources do not require mutually exclusive access and thus cannot be
involved in a deadlock.
● Example : Read-only files are sharable.
● In general, however, we cannot prevent deadlocks by denying the
mutual-exclusion condition, because some resources are intrinsically
non-sharable.

Hold And Wait:


To ensure that the hold-and-wait condition never occurs in the system,
we must guarantee that, whenever a process requests a resource, it does
not hold any other resources.

● One protocol that can be used requires each process to request and be allocated all its
resources before it begins execution.
● system calls requesting resources for a process precede all other system calls.
● An alternative protocol: A process may request some resources and use them. Before
it can request any additional resources, however, it must release all the resources that it
is currently allocated.
Both these protocols have two main disadvantages:
1. resource utilization may be low, since resources may be allocated
but unused for a long period.
2. starvation is possible.

No Preemption:
To ensure pre-emption of resources the
following two protocols can be used:
1. First Protocol
● If a process is holding some resources and requests another resource that
cannot be immediately allocated to it ( the process must wait),
● then all resources the process is currently holding are preempted (
implicitly released).
● The preempted resources are added to the list of resources for which the
process is waiting.
● The process will be restarted only when it can regain its old resources, as
well as the new ones that it is requesting.
2. Second Protocol
● if a process requests some resources, and they are available allocate
them.
● If they are not available and are allocated to some other process that is
waiting for additional resources.
● Pre-empt the desired resources from the waiting process and allocate
them to the requesting process.
● If the resources are neither available nor held by a waiting process, the
requesting process must wait.

A process can be restarted only when it is allocated the new


resources it is requesting and recovers any resources that
were pre-empted while it was waiting.

Circular Wait:
One way to ensure that circular wait never holds:
● impose a total ordering of all resource types and
● require that each process requests resources in an increasing order of
enumeration.

A process can initially request any number of instances of a resource type -say, Ri. After
that, the process can request instances of resource type Rj if and only if F(Rj) >
F(Ri).Where F(Rj) is the numeric order of resources Rj.

Deadlock Avoidance:
● Simplest and most useful model requires that each process declare the
maximum number of resources of each type that it may need
● The deadlock-avoidance algorithm dynamically examines the resource-allocation
state to ensure that there can never be a circular-wait condition
● Resource-allocation state is defined by the number of available and allocated
resources, and the maximum demands of the processes.

Safe State:
● When a process requests an available resource, system must decide if
immediate allocation leaves the system in a safe state
● System is in safe state if there exists a sequence <P1, P2, …, Pn> of ALL the
processes in the systems such that for each Pi, the resources that Pi can still
request can be satisfied by currently available resources + resources held by all
the Pj, with j < I

That is:
● If Pi resource needs are not immediately available, then Pi can wait until all Pj
have finished
● When Pj is finished, Pi can obtain needed resources, execute, return allocated
resources, and terminate
● When Pi terminates, Pi +1 can obtain its needed resources, and so on

Resource-Allocation Graph Scheme:


● Claim edge Pi→ Rj indicated that process Pj may request resource Rj;
represented by a dashed line
● Claim edge converts to request edge when a process requests a resource
● Request edge converted to an assignment edge when the resource is allocated
to the process
● When a resource is released by a process,assignment edge reconverts to a
claim edge
● Resources must be claimed a priori in the system

Resources Allocation Graph

Banker's Algorithm:
● When a new process enters the system, it must declare the maximum number of
instances of each resource type that it may need.
● The requirement may not exceed the total number of resources in the system.
● When a user requests a set of resources, the system must determine whether
the allocation of these resources will leave the system in a safe state.
● If it will, the resources are allocated; otherwise, the process must wait until some
other process releases enough resources.

Data structures need to implement the banker's algorithm:


1. Available: A vector of length m indicates the number of available resources of
each type.
• If Available[j] = k, then k instances of resource type Rj are available.
2. Max: An n x m matrix defines the maximum demand of each process.
• If Max[i] [j] = k, then process Pi may request at most k instances of resource
type Rj.
3. Allocation: An n x m matrix defines the number of resources of each type
currently allocated to each process.
• If Allocation[i][j] = k, then process Pi is currently allocated k instances of
resource type Rj.
4. Need: An n x m matrix indicates the remaining resource need of each process.
• If Need[i][j] = k, then process Pi may need k more instances of resource type Ri
to complete its task.
•Need[i][j]=Max[i][j]-Allocation [i][j].

Safety Algorithms:
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available
Finish [i] = false for i = 0, 1, …, n- 1
2. Find an i such that both:
(a) Finish [i] = false
(b) Needi<=Work
If no such i exists, go to step4
3. Work = Work + Allocation i
Finish[i] = true
go to step 2
4. If Finish [i] == true for all i, then the system is in a safe state.

Deadlock Detection:
● System does not use either a deadlock-prevention or a
deadlock avoidance algorithm, and may enter in to a deadlock state
● system may provide:
• An algorithm that examines the state of the system to determine whether a
deadlock has occurred
• An algorithm to recover from the deadlock.

Single instance of each Resource Type:


● Deadlock detection algorithm use a variant of resource allocation graph called
wait for graph.
● Edge Pi→Pj implies Pi is waiting for Pj to release a resource
● An edge Pi→Pj in a wait-for graph is obtained by merging two edges Pi→Rq and
Rq→Pj in the corresponding resource allocation graph
Single Instance of Each Resource Type:
● a deadlock exists in the system if and only if the wait-for graph contains a cycle.
● Periodically invoke an algorithm that searches for a cycle in the graph.
● An algorithm to detect a cycle in a graph requires an order of n² operations,
where n is the number of vertices in the graph.

Detection Algorithm:
1. Let Work and Finish be vectors of length m and n, respectively Initialize:
(a) Work = Available
(b) For i = 1,2, …, n, if Allocation i =! 0, then
Finish[i]=false;otherwise,
Finish[i] = true.
2. Find an index i such that both:
(a) Finish[i] == false
(b) Request i<=Work
If no such i exists, go to step 4.
3. Work = Work + Allocation i
Finish[i] = true
go to step 2
4. If Finish[i] == false, for some i, 1<=i<=n, then the system is in deadlock state.
Moreover, if Finish[i] == false, then Pi is deadlocked.
Recovery from Deadlock : Process Termination
● Abort all deadlocked processes.
● Abort one process at a time until the deadlock cycle is eliminated.
● In which order should we choose to abort?
• Priority of the process.
• How long process has computed, and how much longer to completion.
• Resources the process has used.
• Resources process needs to complete.
• How many processes will need to be terminated.
• Is process interactive or batch?

Recovery from Deadlock : Resource Preemption


● Selecting a victim – determine the order of preemption to minimize cost. Cost
factors may include such parameters as the number of resources a deadlocked
process is holding and the amount of time the process has thus far consumed
during its execution.
● Rollback – We must roll back the process to some safe state and restart it from
that state.
• Since, in general, it is difficult to determine what a safe state is, the simplest
solution is a total rollback: abort the process and then restart it.
• Although it is more effective to roll back the process only as far as necessary
to break the deadlock, this method requires the system to keep more information
about the state of all running processes return to some safe state,restart process
for that state.

You might also like