0% found this document useful (0 votes)
91 views67 pages

Unit 3

This document discusses operating systems and deadlocks. It begins by defining a system model where processes request, use, and release resources. It then defines what a deadlock is using examples. The four necessary conditions for deadlock are explained: mutual exclusion, hold and wait, no preemption, and circular wait. Methods for handling deadlocks are discussed, including deadlock prevention, avoidance, detection, and recovery. Resource allocation graphs are introduced and it is explained how they can be used to identify deadlocks.
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)
91 views67 pages

Unit 3

This document discusses operating systems and deadlocks. It begins by defining a system model where processes request, use, and release resources. It then defines what a deadlock is using examples. The four necessary conditions for deadlock are explained: mutual exclusion, hold and wait, no preemption, and circular wait. Methods for handling deadlocks are discussed, including deadlock prevention, avoidance, detection, and recovery. Resource allocation graphs are introduced and it is explained how they can be used to identify deadlocks.
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/ 67

DTEL (Department for Technology Enhanced Learning)

The Centre for Technology enabled Teaching & Learning , N Y S S, India

Teaching Innovation - Entrepreneurial - Global 1


DEPARTMENT OF COMPUTER TECHNOLOGY
IV-SEMESTER
OPERATING SYSTEMS

UNIT NO.3
DEPARTMENT OF COMPUTER TECHNOLOGY
IV-SEMESTER
OPERATING SYSTEMS
Syllabus of Unit III
Deadlocks
System Model,
Deadlock characterization-necessary conditions,
Resource allocation graph (RAG),
Methods for handling deadlock
deadlock avoidance, deadlock detection, deadlock
prevention, Recovery from deadlock.
System Model

System consists of resources


A process in operating system uses resources in
the following way.
1) Requests a resource
2) Use the resource
3) Releases the resource
• Resource types R1, R2, . . ., Rm
CPU cycles, memory space, I/O devices
• Each resource type Ri has 1 or more instances

DTEL 4
The Deadlock Problem

A deadlock is a situation where a process or set of process


is blocked, waiting for some resource that is held by some
other waiting process.
Example #1
A system has 2 disk drives
P1 and P2 each hold one disk drive and
each needs the other one
Example #2
Semaphores A and B, initialized to 1
P0 P1
wait (A); wait(B)
wait (B); wait(A)

DTEL 5
DEADLOCKS The Deadlock Problem

• Traffic only in one direction


• The resource is a one-lane bridge
The Deadlock Problem
• If a deadlock occurs, it can be resolved if one car backs up
(preempt resources and rollback)
• Several cars may have to be backed up if a deadlock occurs
• Starvation is possible

DTEL 6
DEADLOCKS Deadlock Characterization

Necessary condition:
Deadlock can arise if four conditions hold simultaneously.
● Mutual exclusion: only one process at a time can use a
resource
● Hold and wait: a process holding at least one resource is
waiting to acquire additional resources held by other processes
● No preemption: a resource can be released only voluntarily
by the process holding it after that process has completed its
task
● Circular wait: 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 Pn is waiting for a resource that is held by P0
7

DTEL 7
DEADLOCKS Resource-Allocation Graph(RAG)
A deadlock is described in terms of a directed
graph G(V,E) called as a system Resource
Graph(RAG).

RAG consist of two sets-


1. A set of vertices V and
2. a set of edges E.
1.Set of vertices V is partitioned into two types:
● P = {P1, P2, …, Pn}, the set consisting of all
the processes in the system

● R = {R1, R2, …, Rm}, the set consisting of all


resource types in the system
2.Two types of edges E
● request edge – directed edge P1 → Rj
● assignment edge – directed edge Rj → Pi
8

DTEL 8
DEADLOCKS Resource-Allocation Graph(RAG)

DTEL 9
DEADLOCKS Resource Allocation Graph With A Deadlock

Example of a Resource Allocation Graph

Before P3 requested an After P3 requested an


instance of R2 instance of R2

10

DTEL 10
DEADLOCKS Graph With A Cycle But No Deadlock

Process P4 may release its instance of resource type R2. That


resource can then be allocated to P3, thereby breaking the cycle.
11

DTEL 11
DEADLOCKS Relationship of cycles to deadlocks

Basic Facts

• If a resource allocation graph contains no cycles ⇒ no deadlock

• If a resource allocation graph contains a cycle and


• if only one instance exists per resource type then deadlock

• If a resource allocation graph contains a cycle and


• if several instances exists per resource type, possibility of
deadlock

12

DTEL 12
DEADLOCKS Methods for Handling Deadlocks

Generally speaking there are three ways of handling deadlocks:


▪ Deadlock prevention :Deadlock can be prevented from
occurring by preventing one of the condition to hold.
▪ Deadlock avoidance – Deadlock can be avoided by
maintaining safe sequence
▪ Deadlock recovery - Abort a process or preempt some
resources when deadlocks are detected.
▪ Allow deadlock to occur-(Ignore the problem all
together) - If deadlocks only occur once a year or so, it may
be better to simply let them happen and reboot as necessary
than to incur the constant overhead and system performance
penalties associated with deadlock prevention or detection.
This is the approach that both Windows and UNIX take.
13

DTEL 13
DEADLOCKS Deadlock Prevention
● Two methods:
1. Direct method: We need to prevent the occurrence of a
circular wait
2. Indirect method: We need to prevent occurrence of one of the
other 3 conditions
● Mutual Exclusion – The mutual-exclusion condition must hold
for non-sharable resources.
● Shared resources such as read-only files do not lead to
deadlocks.
● Unfortunately some resources, such as printers and tape
drives, require exclusive access by a single process

14

DTEL 14
LECTURE TOPIC:-DEADLOCKS Deadlock Prevention

Hold and Wait – We need to insure that 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
Require a process to request and be allocated all its
resources before it begins execution, or allow a process to
request resources only when the process has none
Result: Low resource utilization; starvation possible

15

DTEL 15
DEADLOCKS Deadlock Prevention

● No Preemption –
● If a process that is holding some resources requests another
resource that cannot be immediately allocated to it, then all
resources currently being held are released
● Preempted resources are added to the list of resources for
which the process is waiting
● A process will be restarted only when it can regain its old
resources, as well as the new ones that it is requesting
● Alternative technique-Explaination
● Circular Wait – impose a total ordering of all resource types,
and require that each process requests resources in an
increasing order of enumeration. For example:
F(tape drive) = 1
F(disk drive) = 5 16
F(printer) = 12
DTEL 16
DEADLOCKS Deadlock Avoidance

Requires that the system has some additional a priori information


available.
• Simplest and most useful model requires that each process declare the
maximum number of resources of each type that it may need

• There must be fixed number of resources to allocate

• The deadlock-avoidance algorithm dynamically examines the


resource-allocation state to ensure that there can never be a
circular-wait condition

• A resource-allocation state is defined by the number of


available and allocated resources, and the maximum
demands of the processes
17

DTEL 17
:-DEADLOCKS
Safe State

When a process requests an available resource, the system must


decide if immediate allocation leaves the system in a safe state
A system is in a safe state only if there exists a safe sequence
A sequence of processes <P1, P2, …, Pn> is a safe sequence for
the current allocation state if, for each Pi, the resource requests
that Pi can still make, can be satisfied by currently available
resources plus resources held by all Pj, with j < i.
That is:
If the 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
18

DTEL 18
DEADLOCKS Safe State Example
● Suppose there are 12 tape drives
max need current usage could ask for
p0 10 5 5
p1 4 2 2
p2 9 7 2
3 drives remain
● current state is safe because a safe sequence exists: <p1,p0,p2>
p1 can complete with current resources
p0 can complete with current+p1
p2 can complete with current +p1+p0

● if p2 requests 1 drive, then it must wait to avoid unsafe state.

19

DTEL 19
DEADLOCKS Safe State Example

● If a system is in safe state  no deadlocks

● If a system is in unsafe state  possibility of


deadlock

● Avoidance  ensure that a system will never enter


an unsafe state

20

DTEL 20
DEADLOCKS Safe, Unsafe , Deadlock State

21

DTEL 21
LECTURE 1:-DEADLOCKS The Deadlock Problem

● For a single instance of a resource type, use a


resource-allocation graph (RAG)

● For multiple instances of a resource type, use the


banker’s algorithm

22

DTEL 22
DEADLOCKS Resource-Allocation Graph Scheme

23

DTEL 23
DEADLOCKS Resource-Allocation Graph with Claim Edges

24

DTEL 24
DEADLOCKS Resource-Allocation Graph Algorithm

● Suppose that process Pi requests a resource Rj

● The request can be granted only if converting the


request edge to an assignment edge does not result in
the formation of a cycle in the resource allocation graph

25

DTEL 25
DEADLOCKS Banker’s Algorithm (To avoid deadlock)

● Banker’s Algorithm is used majorly in the banking system to


avoid deadlock. It helps you to identify whether a loan will
be given or not.

● This algorithm is used to test for safely simulating the


allocation for determining the maximum amount available
for all resources. It also checks for all the possible activities
before determining whether allocation should be continued
or not.
26

DTEL 26
DEADLOCKS Banker’s Algorithm (To avoid deadlock)

● Used when there exists multiple instances of a resource


type

● Each process must a priori claim maximum use

● When a process requests a resource, it may have to wait

● When a process gets all its resources, it must return them in


a finite amount of time

27

DTEL 27
DEADLOCKS Banker’s Algorithm (To avoid deadlock)

Data Structures for the Banker’s Algorithm


Notation used:
Let n = number of processes, and
m = number of resources types
● Available: It indicates the no. of available resource of each
type.
● Max: It indicates the max demand of resources of each type

● Allocation: It indicates the no. of resources of each type


currently allocated to a process.

● Need: It indicates the no. of remaining resources of each


type needed by a process. ( need = max – allocation)
28

DTEL 28
DEADLOCKS Banker’s Algorithm (To avoid deadlock)

Data Structures for the Banker’s Algorithm


Notation used:
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] 29

DTEL 29
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
This algorithm implements using two sub algorithms:
i) Safety Algorithm
ii) Resource-request algo
i) Safety Algorithm (used to check whether the system is in safe state or not)
1. Let Work and Finish be vectors of length m and n, respectively. Initialize:
Work = Available (no. of available resources)
Finish [ i ] == false for i = 1,2, …, n. (execution not finished)
2. Find an i such that both
Finish [ i ] == false
Need i ≤ Work/Available
If no such i exists, go to step 4. (It means, we need to find an unfinished process whose
needs can be satisfied by the available resource s)
3. Work = Work + Allocation (Resources freed when process completes!)
Finish[ i ] == true
go to step 2.
4. If Finish [ i ] == true for all i, then the system is in a safe state. 30

That means if all processes are finished, then the system is in safe state. 30
DEADLOCKS Banker’s Algorithm (To avoid deadlock)

Resource-Request Algorithm for Process Pi


It is mainly used to determine whether requests can be
safely granted or not.
Requesti = request vector for Pi .
Requesti [j] = k means process Pi wants k instances of
resource type Rj.

1. If Requesti ≤ Needi go to step 2. Otherwise, error ( process


exceeded its maximum claim).
2. If Requesti ≤ Available, go to step 3. Otherwise Pi must wait,
(resources not available).
3. “Allocate” requested resources to Pi as follows:
Available = Available - Requesti
Alloci = Alloci + Requesti
Needi = Needi – Requesti
If safe ⇒ the resources are allocated to Pi. 31
If unsafe ⇒ restore the old resource-allocation state and block Pi
31
DEADLOCKS Banker’s Algorithm (To avoid deadlock)

2. Resource Request Algorithm

32

DTEL 32
DEADLOCKS Banker’s Algorithm (To avoid deadlock)
Example of Banker’s Algorithm (safety Algorithm)
5 processes P0 through P4
3 resource types A (10 units), B (5 units), and C (7 units).

Allocation Max Available/work


A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2
P1 2 0 0 3 2 2
P2 3 0 2 9 0 2
P3 2 1 1 2 2 2
P4 0 0 2 4 3 3
a) Find need matrix
b) Is the system in safe state? (if Yes find safe sequence)
c) If the request from process P1 arrives for 1 0 2 can request be
granted immediately. 33

DTEL 33
DEADLOCKS Banker’s Algorithm (To avoid deadlock)

Example of Banker’s Algorithm (safety Algorithm)

Allocation Max Available/work Need


A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 P1 2 0 0
3 2 2 P2 3 0 2 9 0 2
P3 2 1 1 2 2 2 P4 0 0
2 4 3 3

34

DTEL 34
DEADLOCKS Banker’s Algorithm (To avoid deadlock)

Need [i] = Max [i] - Allocation [i]

Need for P0: (7, 5, 3) - (0, 1, 0) =


Need for P1: (3, 2, 2) - (2, 0, 0) =
Need for P2: (9, 0, 2) - (3, 0, 2) =
Need for P3: (2, 2, 2) - (2, 1, 1) =
Need for P4: (4, 3, 3) - (0, 0, 2) =

35

DTEL 35
DEADLOCKS Banker’s Algorithm (To avoid deadlock)

Need [i] = Max [i] - Allocation [i]

Need for P0: (7, 5, 3) - (0, 1, 0) = 7, 4, 3


Need for P1: (3, 2, 2) - (2, 0, 0) = 1, 2, 2
Need for P2: (9, 0, 2) - (3, 0, 2) = 6, 0, 0
Need for P3: (2, 2, 2) - (2, 1, 1) = 0, 1, 1
Need for P4: (4, 3, 3) - (0, 0, 2) = 4, 3, 1

36

DTEL 36
DEADLOCKS Banker’s Algorithm (To avoid deadlock)

Need = Max – Allocation


. Process Need
A B C
P0 7 4 3
P1 1 2 2
P2 6 0 0
P3 0 1 1
P4 4 3 1

37

DTEL 37
DEADLOCKS Banker’s Algorithm (To avoid deadlock)

Example of Banker’s Algorithm (safety Algorithm)

Allocation Max Available/work Need


A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 743
P1 2 0 0 3 2 2 122
P2 3 0 2 9 0 2 600
P3 2 1 1 2 2 2 011
P4 0 0 2 4 3 3 431

38

DTEL 38
Apply the Banker's Algorithm:

Available Resources of A, B and C are 3, 3, and 2.


Now we check if each type of resource request is available for each
process.
: For Process P0: ( i =0 )
Need <= Available
7, 4, 3 <= 3, 3, 2 condition is false. ( So we cannot allocate the
resources to P0)
So, we examine another process, P1.
: For Process P1: ( i = 1)
Need <= Available
1, 2, 2 <= 3, 3, 2 condition true (so we can allocate resources to
P1)
New available = available + Allocation
(3, 3, 2) + (2, 0, 0) => 5, 3, 2 (new work value)
Similarly, we examine another process P3.
: For Process P2: ( i = 2 )
Need <= Available(new work value)
6, 0, 0 < = 5, 3, 2 condition is false. 39
Similarly, we examine another process
DTEL 39
: For Process P3: ( i = 3)
Need <= Available
0, 1, 1 <= 5, 3, 2 condition is true
New Available resource = Available + Allocation
5, 3, 2 + 2, 1, 1 => 7, 4, 3
Similarly, we examine another process P4.
: For Process P4: ( i = 4 )
Need <= Available
4, 3, 1 <= 7, 4, 3 condition is true
New available resource = Available + Allocation
7, 4, 3 + 0, 0, 2 => 7, 4, 5
Now, we again examine each type of resource request for processes P0
and P2.
: For Process P0: ( i =0)
Need <= Available
7, 4, 3 <= 7, 4, 5 condition is true
New Available Resource = Available + Allocation
7, 4, 5 + 0, 1, 0 => 7, 5, 5
So, we examine another process P2.
: For Process P2: ( i = 2 ) 40
P3 Need <= Available
6, 0, 0 <= 7, 5, 5 condition is true 40
New Available Resource = Available + Allocation
Banker’s Algorithm (To avoid deadlock)

Hence, we execute the banker's algorithm to find


the safe state and the safe sequence like
P1, P3, P4, P0 and P2.

41

DTEL 41
Banker’s Algorithm (To avoid deadlock)

Banker’s Algorithm (using Resource Request Algorithm)

Allocation Max Available/work Need


A B C A B C A B C
P0 0 1 0 7 5 3 3 3 2 743
P1 2 0 0 3 2 2 122
P2 3 0 2 9 0 2 600
P3 2 1 1 2 2 2 011
P4 0 0 2 4 3 3 431
If P1 requests (1,0,2 ) determine if it can be granted
immediately
P1 = R ( 1 0 2 )
42

DTEL 42
DEADLOCKS Banker’s Algorithm (To avoid deadlock)

If P1 requests (1,0,2 ) determine if it can be granted


immediately
P1 = R ( 1 0 2 )
Need(P1)=Max – Allocation
322 -200
=122
1) Request<=need
1 0 2<=1 2 2
2) Request<= available
1 0 2<=3 3 2
43

DTEL 43
DEADLOCKS Banker’s Algorithm (To avoid deadlock)

Banker’s Algorithm (using Resource Request Algorithm)

3) Available = avail - request


332–102=230
Allocation = allocation + request
200+102=302
Need = need – request
122–102=020
These values need to be updated in new table

44

DTEL 44
DEADLOCKS Banker’s Algorithm (To avoid deadlock)

New Table

Allocation Max Available/work Need


A B C A B C A B C
P0 0 1 0 7 5 3 2 3 0 743
P1 3 0 2 3 2 2 020
P2 3 0 2 9 0 2 600
P3 2 1 1 2 2 2 011
P4 0 0 2 4 3 3 431

Need < = Available


For this table apply banker’s algorithm and find the new set
sequence 45

DTEL 45
DEADLOCKS Banker’s Algorithm (To avoid deadlock)

P0 = 7 4 3 <= 2 3 0 not true


P1 = 0 2 0<= 2 3 0 true (w = w + allocation)
= 230+302 =5 32
P2 = 6 0 0 <=5 3 2 not true
P3 = 0 1 1 <= 5 3 2 true (w = w + allocation )
= 5 3 2 + 2 1 1 =7 4 3
P4 = 4 3 3<= 7 4 3 true (w = w + allocation)
=743+00 2=745
P0 = 7 4 3 <= 7 4 5 true (w= w+ allocation)
=745+010=755
P2 = 6 0 0 <= 7 5 5 true (w =w + allocation)
=755+302=1057
Safe sequence = ( P1 , P3 , P4 , P0 , P2 )

Yes P1 requests (1,0,2 ) can be granted immediately 46

DTEL 46
Deadlock Detection

● Deadlock detection and recovery is the process of detecting and


resolving deadlocks in an operating system.
● A deadlock occurs when two or more processes are blocked,
waiting for each other to release the resources they need
There are two main approaches to deadlock detection and
recovery:
1.Prevention:
To prevent deadlocks from occurring by ensuring that the system is
always in a safe state, where deadlocks cannot occur. This is achieved
through resource allocation algorithms such as the Banker’s
Algorithm.
1.Detection and Recovery:
If deadlocks do occur, the operating system must detect and resolve
them. Deadlock detection algorithms, such as the Wait-For Graph,
are used to identify deadlocks, and recovery algorithms, such as
the Rollback and Abort algorithm, are used to resolve them. The
recovery algorithm releases the resources held by one or more
processes, allowing the system to continue to make progress.
47
Deadlock Detection

● Deadlock detection and recovery is the process of


detecting and resolving deadlocks in an operating system.

● For deadlock detection, the system must


provide
● An algorithm that examines the state of the

system to detect whether a deadlock has


occurred
● And an algorithm to recover from the
deadlock

48

DTEL 48
Deadlock Detection

49

DTEL 49
LECTURE 1:-DEADLOCKS Deadlock Detection

Single Instance of Each Resource Type


● Requires the creation and maintenance of a wait-for graph
● Consists of a variant of the resource-allocation graph (RAG)
● The graph is obtained by removing the resource nodes from a
resource-allocation graph and collapsing the appropriate edges
● Consequently; all nodes are processes
● Pi → Pj if Pi is waiting for Pj.

● Periodically invoke an algorithm that searches for a cycle in


the graph
● If there is a cycle, there exists a deadlock

● An algorithm to detect a cycle in a graph requires an order

of n2 operations, where n is the number of vertices in the


graph 50

50
LECTURE 1:-DEADLOCKS Deadlock Detection
Resource-Allocation Graph and Wait-for Graph

Resource-Allocation Corresponding wait-for


Graph graph
51

DTEL 51
LECTURE 1:-DEADLOCKS Deadlock Detection

Required 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 [ij] = k, then process Pi is requesting k more
instances of resource type. Rj.
52

DTEL 52
LECTURE 1:-DEADLOCKS Deadlock Detection

Let, Work and Finish be vectors of length m and n, respectively


Initialize:
(a) Work = Available
(b)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
(b) Requesti ≤ Work
If no 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


deadlock state. Moreover, if Finish[i] == false, then Pi is 53
deadlocked.
DTEL 53
LECTURE 1:-DEADLOCKS Deadlock Detection

● Five processes P0 through P4; three resource types


A (7 instances), B (2 instances), and C (6 instances).
● Snapshot at time T0:
Allocation Request Available/work
A B C A BC ABC
P0 0 1 0 0 0 0 0 0 0
P1 2 0 0 2 0 2
P2 3 0 3 0 0 0
P3 2 1 1 1 0 0
P4 0 0 2 0 0 2
● Sequence <P0, P2, P3, P1, P4> will result in Finish[i] = true
for all i.
54

DTEL 54
LECTURE 1:-DEADLOCKS Deadlock Detection

● P2 requests an additional instance of type C.


Request
ABC
P0 0 0 0
P1 2 0 2
P2 0 0 1
P3 1 0 0
P4 0 0 2
● State of system?
● Can reclaim resources held by process P0, but insufficient resources to
fulfill other processes; requests.
● Deadlock exists, consisting of processes P1, P2, P3, and P4.
55

DTEL 55
LECTURE 1:-DEADLOCKS Recovery From Deadlock

In order to recover the system from


deadlocks, either OS considers resources or
processes.

56

DTEL 56
LECTURE 1:-DEADLOCKS Recovery From Deadlock

in order to recover the system from deadlocks, either


OS considers resources or processes.

For Resource
Preempt the resource
We can snatch one of the resources from the owner of the resource
(process) and give it to the other process with the expectation that it will
complete the execution and will release this resource sooner. It is going to
be a bit difficult.
Rollback to a safe state
System passes through various states to get into the deadlock state. The
operating system canrollback the system to the previous safe state. For
this purpose, OS needs to implement check pointing at every state.
The moment, we get into deadlock, we will rollback all the allocations to
get into the previous safe state.

57

DTEL 57
LECTURE 1:-DEADLOCKS Recovery From Deadlock

In order to recover the system from deadlocks, either


OS considers resources or processes. ( Four methods
)For Process

Kill a process
Killing a process can solve our problem but the bigger concern
is to decide which process to kill. Generally, Operating system
kills a process which has done least amount of work until
now.

Kill all process


This is not a suggestible approach but can be implemented if
the problem becomes very serious. Killing all process will
lead to inefficiency in the system because all the processes will
execute again from starting. 58

DTEL 58
LECTURE 1:-DEADLOCKS Recovery From Deadlock

● Two Approaches
● Process termination
● Terminate all the deadlock processes
● Terminate processes one by one until the
deadlock is broken

● Resource preemption

59

DTEL 59
LECTURE 1:-DEADLOCKS Recovery From Deadlock
● Abort all deadlocked processes
● This approach will break the deadlock, but at great expense

● Abort one process at a time until the deadlock cycle is eliminated


● This approach incurs considerable overhead, since, after each
process is aborted, a deadlock-detection algorithm must be
re-invoked to determine whether any processes are still deadlocked

● Many factors may affect which process is chosen for termination


● What is the priority of the process?
● How long has the process run so far and how much longer will the
process need to run before completing its task?
● How many and what type of resources has the process used?
● How many more resources does the process need in order to finish
its task?
● How many processes will need to be terminated?
● Is the process interactive or batch? 60
● n
DTEL 60
LECTURE 1:-DEADLOCKS Recovery From Deadlock
Resource Preemption
● With this approach, we successively preempt some resources from
processes and give these resources to other processes until the deadlock
cycle is broken
● When preemption is required to deal with deadlocks, then
three issues need to be addressed:
● Selecting a victim – Which resources and which processes are to be
preempted?

● Rollback – If we preempt a resource from a process, what should be


done with that process?

● Starvation – How do we ensure that starvation will not occur? That is,
how can we guarantee that resources will not always be preempted
from the same process?
61

DTEL 61
LECTURE 1:-DEADLOCKS
Deadlock Ignorance
Ignore the problem as it does not exit.
As handling deadlock is expensive and need lots of
codes which will decrease the performance. so
deadlock are ignored for less critical jobs.
UNIX and WINDOWS , operating system ignore the
deadlock.
If there is deadlock occured in the system, then the
OS will just ignore the deadlock and reboot the
system in order to function well.
This method of solving any problem is called Ostrich
Algorithm
62

DTEL 62
LECTURE 1:-DEADLOCKS
Deadlock Ignorance
Ostrich Algorithm: it is based on the amazing
behavior of the bird Ostrich
During the storm, the Ostrich stick his head in the
sand and pretend there is no storm
This Ostrich algorithm is most widely used technique
in order to ignore the deadlock and also it is used for
the single end users systems.

63

DTEL 63
LECTURE 1:-DEADLOCKS
Deadlock Ignorance
Ostrich Algorithm:
It pretends there is no deadlock in the system.
It is simply assumed that the deadlock will never
occur.
It is reasonable to use if,
deadlock occur very rarely
the cost of their prevention would be high
The UNIX and Windows o.s are utilizing this approach

64

DTEL 64
LECTURE 1:-DEADLOCKS
Deadlock Ignorance
Using ostrich algorithm is one of the method of
dealing with deadlocks.
This will not be a good idea to solve the deadlock
problem, hence the drawbacks are more, without
finding solution to deadlock problems.
The methods for handling deadlocks,
Deadlock detection
Deadlock recovery
Deadlock avoidance
Deadlock prevention

65

DTEL 65
LECTURE 1:-DEADLOCKS Summary
● Four necessary conditions must hold in the system for a deadlock
to occur
● Mutual exclusion
● Hold and wait
● No preemption
● Circular wait
● Four principal methods for dealing with deadlocks
● Use some protocol to (1) prevent or (2) avoid deadlocks, ensuring that the
system will never enter a deadlock state
● Allow the system to enter a deadlock state, (3) detect it, and then recover
Recover by process termination or resource preemption
● (4) Do nothing; ignore the problem altogether and pretend that deadlocks
never occur in the system (used by Windows and Unix)
● To prevent deadlocks, we can ensure that at least one of the four
necessary conditions never holds
66

DTEL 66
THANK YOU

DTEL 67

You might also like