0% found this document useful (0 votes)
9 views

AOS Lecture 5

The document discusses deadlocks in operating systems, defining them as a state where processes are unable to proceed because each is waiting for a resource held by another. It outlines necessary conditions for deadlock, such as mutual exclusion and circular wait, and describes methods for deadlock avoidance, detection, and recovery. The document also differentiates between preemptable and nonpreemptable resources and explains strategies for resolving deadlocks through process termination or resource preemption.

Uploaded by

Ali Shan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

AOS Lecture 5

The document discusses deadlocks in operating systems, defining them as a state where processes are unable to proceed because each is waiting for a resource held by another. It outlines necessary conditions for deadlock, such as mutual exclusion and circular wait, and describes methods for deadlock avoidance, detection, and recovery. The document also differentiates between preemptable and nonpreemptable resources and explains strategies for resolving deadlocks through process termination or resource preemption.

Uploaded by

Ali Shan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Lecture 5 AOS

LECTURER M HAMAD EJAZ


Deadlocks

 A set of process is in a deadlock state if each process in the set is


waiting for an event that can be caused by only another process
in the set. In other words, each member of the set of deadlock
processes is waiting for a resource that can be released only by a
deadlock process. None of the processes can run, none of them
can release any resources, and none of them can be awakened.
 The resources may be either physical or logical. Examples of
physical resources are Printers, Hard Disc Drives, Memory Space,
and CPU Cycles. Examples of logical resources are Files,
Semaphores, and Monitors.
Example

 The simplest example of deadlock is where process 1 has been


allocated non-shareable resources A
 (say a Hard Disc drive) and process 2 has be allocated non-
sharable resource B (say a printer). Now, if it turns
 out that process 1 needs resource B (printer) to proceed and
process 2 needs resource A (Hard Disc drive) to
 proceed and these are the only two processes in the system, each
is blocked the other and all useful work in the
 system stops. This situation is termed deadlock. The system is in
deadlock state because each process holds a
 resource being requested by the other process neither process is
willing to release the resource it holds.
Preemptable and Nonpreemptable Resources

 Resources come in two flavors: preemptable and nonpreemptable.


 A preemptable resource is one that can be taken away from the process with no ill
effects. Memory is an example of a preemptable resource.
 A nonpreemptable resource is one that cannot be taken away from process (without
causing ill effect). For example, CD resources are not preemptable at an arbitrary
moment.
 Reallocating resources can resolve deadlocks that involve preemptable resources.
Deadlocks that involve nonpreemptable resources are difficult to deal with.

 Under the normal mode of operation, a process may utilize a resource in only the
following sequence:
 1. Request: If the request cannot be granted immediately, then the requesting
process must wait until it can acquire the resource.
 2. Use: The process can operate on the resource.
 3. Release: The process releases the resource.
Necessary Conditions for Deadlock

 Coffman (1971) identified four conditions that must hold simultaneously for
there to be a deadlock.
 1. Mutual Exclusion Condition: The resources involved are non-shareable.
 Explanation: At least one resource must be held in a non-shareable mode,
that is, only one process at a time claims exclusive control of the resource. If
another process requests that resource, the requesting
 process must be delayed until the resource has been released.
 2. Hold and Wait Condition: Requesting process hold already the resources
while waiting for requested resources.
 Explanation: There must exist a process that is holding a resource already
allocated to it while waiting for additional resource that are currently being
held by other processes.
Necessary Conditions for
Deadlock

3 No-Preemptive Condition: Resources already allocated to a process
cannot be preempted.
 Explanation: Resources cannot be removed from the processes are used to
completion or released
 voluntarily by the process holding it.
 4. Circular Wait Condition: The processes in the system form a circular list
or chain where each process
 in the list is waiting for a resource held by the next process in the list. There
exists a set {P0, P1, …, P0} of
 waiting processes such that P0 is waiting for a resource that is held by P1, P1 is
waiting for a resource that is
 held by P2, …, Pn–1 is waiting for a resource that is held by Pn, and P0 is
waiting for a resource that is held by P0.
DEADLOCK AVOIDANCE
 This approach to the deadlock problem anticipates deadlock before it actually
occurs. This approach
 employs an algorithm to access the possibility that deadlock could occur and
acting accordingly. If the necessary
 conditions for a deadlock are in place, it is still possible to avoid deadlock by being
careful when resources are
 allocated. It employs the most famous deadlock avoidance algorithm that is the
Banker’s algorithm.
 A deadlock-avoidance algorithm dynamically examines the resource-allocation
state to ensure that a
 circular wait condition can never exist. The resource-allocation state is defined by
the number of available and
 allocated resources, and the maximum demands of the processes.
Safe and Unsafe States
 A system is said to be in a Safe State, if there is a safe execution sequence. An
execution sequence is
 an ordering for process execution such that each process runs until it terminates
or blocked and all request for
 resources are immediately granted if the resource is available.
 A system is said to be in an Unsafe State, if there is no safe execution
sequence. An unsafe state may
 not be deadlocked, but there is at least one sequence of requests from processes
that would make the system
 deadlocked.
Diagram

(Relation between Safe, Unsafe and Deadlocked States)


DEADLOCK DETECTION
 If a system does not employ either a deadlock-prevention or
a deadlock avoidance algorithm, then a deadlock situation
may occur. In this environment, the system must provide:
 • An algorithm that examines the state of the system to
determine whether a deadlock has occurred.
• An algorithm to recover from the deadlock.
 According to number of instances in each resource type, the
Deadlock Detection algorithm can be classified into two
categories as follows:
Single Instance of Each Resource Type

 :Ifall resources have only a single instance,


then it can define a deadlock detection
algorithm that uses a variant of the resource-
allocation graph (is called a wait-for graph). A
wait–for graph can be draw by removing the
nodes of type resource and collapsing the
appropriate edges from the resource-allocation
graph
Several Instances of a Resource Type

 The following deadlock-detection algorithm is applicable to severalinstance


of a resource type. The algorithm employs several time-varying data
structures:
 Available: A vector of length m indicates the number of available resources
of each type.
 Allocation: An n x m matrix defines the number of resources of each type
currently allocated to each process.
 Request: An n x m matrix indicates the current request of each process. If
Request[i,j] = k, then process Pi is
 requesting k more instances of resource type Rj.
Several Instances of a Resource Type
 The detection algorithm is described as follows:
 1. Let Work and Finish be vectors of length m and n, respectively.
Initialize, Work := Available. For i = 1, 2, ..., n,
 if Allocationi != 0, then Finish[i] :=false; otherwise, Finish[i] := true.
 2. Find an index i such that both
 a. Finish[i] =false. such i exists, go to step 4.
 3. Work := Work + Allocationi
 Finish[i] := true
 go to step 2.
 4. If Finish[i] = false, for some i, 1 < i < n, then the system is in a
deadlock state.
 if Finish[i] =false, then process Pi is deadlocked.
 This algorithm requires an order of m x n2 operations to detect whether
the system is in a deadlocked state.
RECOVERY FROM DEADLOCK

 When a detection algorithm determines that a deadlock exists,


then the system or operator is responsible for handling deadlock
problem. There are two options for breaking a deadlock.
 1. Process Termination
 2. Resource preemption
Process Termination

 There are two method to eliminate deadlocks by terminating a


process as follows:
 1. Abort all deadlocked processes: This method will break the
deadlock cycle clearly by terminating all process. This method is
cost effective. And it removes the partial computations
completed by the processes.
 2. Abort one process at a time until the deadlock cycle is
eliminated: This method terminates one process at a time, and
invokes a deadlock-detection algorithm to determine whether
any processes are still deadlocked.
Resource Preemption

 In resource preemption, the operator or system preempts some


resources from processes and give
 these resources to other processes until the deadlock cycle is
broken. If preemption is required to deal with deadlocks, then
three issues need to be addressed:
1. Selecting a victim: The system or operator selects which
resources and which processes are to be preempted based on cost
factor.
2. Rollback: The system or operator must roll back the process to
some safe state and restart it from that state.
3. Starvation: The system or operator should ensure that
resources will not always be preempted from the same process?

You might also like