Deadlock
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.
Deadlock Prevention:
● For a deadlock to occur, each of the four necessary conditions must hold:
● Mutual Exclusion
● Hold and Wait
● No Preemption
● Circular Wait
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.
● 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.
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
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.
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.
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?