SlideShare a Scribd company logo
Deadlocks
Week 6
Deadlocks
• Resource
• Introduction to deadlocks
• The ostrich algorithm
• Deadlock detection and recovery
• Deadlock avoidance
• Deadlock prevention
• Other issues
3
Resources
• Examples of computer resources
– Printers
– tape drives
– Tables (systems internal table)
– Semaphores ( critical section)
• Processes need access to resources in reasonable order
• Suppose a process holds resource A and requests
resource B
– at same time another process holds B and requests A
– both are blocked and remain so……… this situation is called
DEADLOCK
I/O device
( hardware)
4
Resources (1)
• Deadlocks occur when …
– processes are granted exclusive access to devices
– we refer to these devices generally as resources.
 Resourses :
 A resource Can be hardware device ( eg. Tape drive, printer) ,
 a piece of information ( a locked record in a database )
 For some resources several identical instances are available ( 3 tape
drive)
• A resource is any thing that must be acquired , used. And
released over the course of time
Resources (2)
• Preemptable resources
– can be taken away from a process with no ill
effects
– eg : Memory ( by swapping )
• Nonpreemptable resources
– will cause the process to fail if taken away
– eg: CD ROM ( result garbled CD – Computational failure)
In general deadlock involve with preemptable resourses can
be solved by reallocationg resourses from one process to
another .
Our discussion focus on nonpreemtable resourses
6
Backing Store
(a) Paging to static swap area
(b) Backing up pages dynamically
7
Resources (2)
• Sequence of events required to use a
resource
1. request the resource
2. use the resource
3. release the resource
• Must wait if request is denied
– requesting process may be blocked ( eg:semaphore)
– may fail with error code ( error notice)
 Request can ne placed by request system call( I/O device, tables ect.)
 For some kind of resources ( critical section resources) the user
process can manage using semaphores
8
Introduction to Deadlocks
• Formal definition :
A set of processes is deadlocked if each process in the set is waiting
for an event that only another process in the set can cause
• Usually the event is release of a currently held resource
• None of the processes can …
– run
– release resources
– be awakened
This kind of deadlock is called Resource Deadlock( this is probably
the most common kind )
9
Four Conditions for Resource Deadlock
1. Mutual exclusion condition
• each resource assigned to 1 process or is available
1. Hold and wait condition
• process holding resources can request additional
1. No preemption condition
• previously granted resources cannot forcibly taken away
1. Circular wait condition
• must be a circular chain of 2 or more processes
• each is waiting for resource held by next member of the
chain
• The four condition relates to a policy that a
system can have /not have
 Can a given resource be assigned to more than one
process at once ?
 Can a process hold a resource and ask for another?
 Can resource be preemted?
 Can circular wait exist?
11
Deadlock Modeling (1)
• Modeled with directed graphs
( 1972 Holt showed how the four condition can be modeled)
– resource R assigned to process A
– process B is requesting/waiting for resource S
– process C and D are in deadlock over resources T and U
12
Deadlock Modeling (2)
Resource allocation graphs
• Resource allocation
modeled by directed
graphs
• Example 1:
– Resource R assigned to
process A
• Example 2:
– Process B is requesting /
waiting for resource S
• Example 3:
– Process C holds T, waiting for
U
– Process D holds U, waiting
for T
R
A
S
B
U
T
DC
13How deadlock occurs
A B C
Deadlock Modeling (3)
14
Deadlock Modeling (5)
How deadlock can be avoided
(o) (p) (q)
Four Strategies dealing with dead lock
1. Just ignore the problem . May be if you ignore it, it will
ignore you
2.Detection & recovery. Let deadlock happen, detect them
and take action
3.Dynamic avoidance by careful resource allocation
4.Prevention, by structurally negating one of the four
condition.
16
The Ostrich Algorithm
• Pretend there is no problem
“Hide your head in the sand and pretend there is no problem at all “
• Reasonable if
– deadlocks occur very rarely
– cost of prevention is high
• It is a trade off between
– convenience
– correctness
2.Detection & recovery
• The system does not prevent deadlock from
occurring .Instead …………….
-it lets them occur
-tries to detect when this happens
-and take some action to recover after that.
18
Detecting deadlocks using graphs
An example : Detection with One Resource of Each Type
•Process holdings and requests in the table and in the graph
(they’re equivalent)
•Graph contains a cycle => deadlock!
– Easy to pick out by looking at it (in this case)
– Need to mechanically detect deadlock
•Not all processes are deadlocked (A, C, F not in deadlock)
R A
S
F
W
C
Process Holds Wants
A R S
B T
C S
D U S,T
E T V
F W S
G V U
ED
G
B
T
VU
19
Deadlock detection algorithm
• General idea: try to find
cycles in the resource
allocation graph
• Algorithm: depth-first
search at each node
– Mark arcs as they’re
traversed
– Build list of visited nodes
– If node to be added is
already on the list, a cycle
exists!
• Cycle == deadlock
For each node N in the graph {
Set L = empty list
unmark all arcs
Traverse (N,L)
}
If no deadlock reported by now, there isn’t any
define Traverse (C,L) {
If C in L, report deadlock!
Add C to L
For each unmarked arc from C {
Mark the arc
Set A = arc destination
/* NOTE: L is a
local variable */
Traverse (A,L)
}
}
20
Resources with multiple instances
• Previous algorithm only works if there’s one
instance of each resource
• If there are multiple instances of each
resource, we need a different method
– Track current usage and requests for each process
– To detect deadlock, try to find a scenario where
all processes can finish
– If no such scenario exists, we have deadlock
Example
•E=(4, 2, 3, 1 ) A=(2, 1, 0, 0)
( Resource in existence) ( Resource Available)
•3 processes
0 0 1 0 2 0 0 1
c= 2 0 0 1 R= 1 0 1 0
0 1 2 0 2 1 0 0
current allocation matrix Request matrix
-p3 can run and return all its resources giving A=(2 , 2, 2 ,0 )
- At this point p2 can run A( 4 2 2 1)
-Now P1 can run no dead lock in the system
Tape , plotters, scanner, Cd rom Tape , plotters, scanner, Cd rom
Chapter 3 22
Deadlock detection algorithm
A B C D
Avail 2 3 0 1
Process A B C D
1 0 3 0 0
2 1 0 1 1
3 0 2 1 0
4 2 2 3 0
Process A B C D
1 3 2 1 0
2 2 2 0 0
3 3 5 3 1
4 0 4 1 1
HoldWant
current=avail;
for (j = 0; j < N; j++) {
for (k=0; k<N; k++) {
if (finished[k])
continue;
if (want[k] < current) {
finished[k] = 1;
current += hold[k];
break;
}
if (k==N) {
printf “Deadlock!n”;
// finished[k]==0 means process is in
// the deadlock
break;
}
}
Note: want[j],hold[j],current,avail are arrays!
23
Recovery from Deadlock (1)
• Recovery through preemption
– take a resource from some other process
– depends on nature of the resource
• Recovery through rollback
– checkpoint a process periodically
– use this saved state
– restart the process if it is found deadlocked
24
Recovery from Deadlock (2)
• Recovery through killing processes
– crudest but simplest way to break a deadlock
– kill one of the processes in the deadlock cycle
– the other processes get its resources
– choose process that can be rerun from the
beginning
Dead Lock Avoidance
• Question
Is there an algorithm that can avoid deadlock
by making the right choice at right time????
Ans: Yes – we can avoid dead lock , but only if
certain information is available in advance
The main Algorithm for avoiding algorithm is
based on safe state
26
Deadlock Avoidance
Resource Trajectories
Two process resource trajectories
Safe & Unsafe Stataes
• At any Instant of time we have E, A , C, A
• A state is said to be safe if there is some scheduling order in
which every process can run to completion even if all of them
suddenly request there maximum number of resources
immediate
Example: Three processes A, B, C , & one resource type ( 10
Instances ( copies))
Safe and unsafe states
Has Max
A 3 9
B 2 4
C 2 7
Free: 3
Has Max
A 3 9
B 4 4
C 2 7
Free: 1
Has Max
A 3 9
B 0 -
C 2 7
Free: 5
Has Max
A 3 9
B 0 -
C 7 7
Free: 0
Has Max
A 3 9
B 0 -
C 0 -
Free: 7
Demonstration that the first state is safe
Has Max
A 3 9
B 2 4
C 2 7
Free: 3
Has Max
A 4 9
B 2 4
C 2 7
Free: 2
Has Max
A 4 9
B 4 4
C 2 7
Free: 0
Has Max
A 4 9
B 0 -
C 2 7
Free: 4
Demonstration that the second state is unsafe
Banker's Algorithm for a single resource
( Dijkstra
• It is an extension of deadlock detection algorithm ( One
resource of each type)
• It is modeled one the way a small time banker who is dealing
with customer to whom he has to granted credits
• Example : Four customers . To whom hes has granted a
certain number of credits ( Iunit- 1K$)
(in our case customers are process , cdret – resources ( say tape
driver) and the banker is the OS
Banker's Algorithm for a single resource ( Dijkstra)
Has Max
A 0 6
B 0 5
C 0 4
D 0 7
Free: 10
Has Max
A 1 6
B 1 5
C 2 4
D 4 7
Free: 2
Has Max
A 1 6
B 2 5
C 2 4
D 4 7
Free: 1
• Bankers’ algorithm: before granting a request, ensure that a sequence exists
that will allow all processes to complete
– Use previous methods to find such a sequence
– If a sequence exists, allow the requests
– If there’s no such sequence, deny the request
• Can be slow: must be done on each request!
Any sequence finishes C,B,A,D finishes Deadlock (unsafe state)
Process D can finish , then A or E followed by rge rest
Example of banker's algorithm with multiple resources
Banker's Algorithm for multiple
resources
C
R
Chapter 3 32CS 1550, cs.pitt.edu (originaly
modified by Ethan L. Miller and
Preventing deadlock
• Deadlock can be completely prevented!
• Ensure that at least one of the conditions for
deadlock never occurs
– Mutual exclusion
– Circular wait
– Hold & wait
– No preemption
• Not always possible…
Chapter 3 33
Eliminating mutual exclusion
• Some devices (such as printer) can be spooled
– Only the printer daemon uses printer resource
– This eliminates deadlock for printer
• Not all devices can be spooled
• Principle:
– Avoid assigning resource when not absolutely
necessary
– As few processes as possible actually claim the
resource
34
Attacking “hold and wait”
• Require processes to request resources before starting
– A process never has to wait for what it needs
• This can present problems
– A process may not know required resources at start of run
– This also ties up resources other processes could be using
• Processes will tend to be conservative and request resources
they might need
• Variation: a process must give up all resources before making a new
request
– Process is then granted all prior resources as well as the new ones
– Problem: what if someone grabs the resources in the meantime—
how can the process save its state?
Chapter 3 35CS 1550, cs.pitt.edu (originaly
modified by Ethan L. Miller and
Attacking “no preemption”
• This is not usually a viable option
• Consider a process given the printer
– Halfway through its job, take away the printer
– Confusion ensues!
• May work for some resources
– Forcibly take away memory pages, suspending the process
– Process may be able to resume with no ill effects
Chapter 3 36CS 1550, cs.pitt.edu (originaly
modified by Ethan L. Miller and
Attacking “circular wait”
• Assign an order to
resources
• Always acquire
resources in numerical
order
– Need not acquire them
all at once!
• Circular wait is
prevented
– A process holding
resource n can’t wait for
resource m
if m < n
A
1
B
C
D
23
37
Deadlock prevention: summary
• Mutual exclusion
– Spool everything
• Hold and wait
– Request all resources initially
• No preemption
– Take resources away
• Circular wait
– Order resources numerically

More Related Content

What's hot

Interrupts
InterruptsInterrupts
Interrupts
guest2e9811e
 
Graph data structure
Graph data structureGraph data structure
Graph data structure
Tech_MX
 
2 PHASE COMMIT PROTOCOL
2 PHASE COMMIT PROTOCOL2 PHASE COMMIT PROTOCOL
2 PHASE COMMIT PROTOCOL
KABILESH RAMAR
 
Operating system 23 process synchronization
Operating system 23 process synchronizationOperating system 23 process synchronization
Operating system 23 process synchronization
Vaibhav Khanna
 
Chapter 2 (final)
Chapter 2 (final)Chapter 2 (final)
Chapter 2 (final)
Nateshwar Kamlesh
 
instruction format and addressing modes
instruction format and addressing modesinstruction format and addressing modes
instruction format and addressing modes
RamaPrabha24
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
Jeanie Arnoco
 
Cs8493 unit 4
Cs8493 unit 4Cs8493 unit 4
Cs8493 unit 4
Kathirvel Ayyaswamy
 
Basic concept of process
Basic concept of processBasic concept of process
Basic concept of process
Nabin Dahal
 
Deadlock Avoidance in Operating System
Deadlock Avoidance in Operating SystemDeadlock Avoidance in Operating System
Deadlock Avoidance in Operating System
Mohammad Hafiz-Al-Masud
 
Semaphores
SemaphoresSemaphores
Semaphores
Mohd Arif
 
AI3391 Session 13 searching with Non-Deterministic Actions and partial observ...
AI3391 Session 13 searching with Non-Deterministic Actions and partial observ...AI3391 Session 13 searching with Non-Deterministic Actions and partial observ...
AI3391 Session 13 searching with Non-Deterministic Actions and partial observ...
Asst.prof M.Gokilavani
 
Interrupts in 8085
Interrupts in 8085Interrupts in 8085
Interrupts in 8085
Hetauda City College
 
Deadlock in database
Deadlock in databaseDeadlock in database
Deadlock in database
Tayyab Hussain
 
Computer Organization Lecture Notes
Computer Organization Lecture NotesComputer Organization Lecture Notes
Computer Organization Lecture Notes
FellowBuddy.com
 
Magnetic Disk
Magnetic Disk Magnetic Disk
Magnetic Disk
kalpita surve
 
Chapter 7 - Deadlocks
Chapter 7 - DeadlocksChapter 7 - Deadlocks
Chapter 7 - Deadlocks
Wayne Jones Jnr
 
Basic Computer Organization and Design
Basic  Computer  Organization  and  DesignBasic  Computer  Organization  and  Design
Basic Computer Organization and Design
Aksum Institute of Technology(AIT, @Letsgo)
 
Theory of Computation Unit 1
Theory of Computation Unit 1Theory of Computation Unit 1
Theory of Computation Unit 1
Jena Catherine Bel D
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
Ahmed Khateeb
 

What's hot (20)

Interrupts
InterruptsInterrupts
Interrupts
 
Graph data structure
Graph data structureGraph data structure
Graph data structure
 
2 PHASE COMMIT PROTOCOL
2 PHASE COMMIT PROTOCOL2 PHASE COMMIT PROTOCOL
2 PHASE COMMIT PROTOCOL
 
Operating system 23 process synchronization
Operating system 23 process synchronizationOperating system 23 process synchronization
Operating system 23 process synchronization
 
Chapter 2 (final)
Chapter 2 (final)Chapter 2 (final)
Chapter 2 (final)
 
instruction format and addressing modes
instruction format and addressing modesinstruction format and addressing modes
instruction format and addressing modes
 
Quick sort-Data Structure
Quick sort-Data StructureQuick sort-Data Structure
Quick sort-Data Structure
 
Cs8493 unit 4
Cs8493 unit 4Cs8493 unit 4
Cs8493 unit 4
 
Basic concept of process
Basic concept of processBasic concept of process
Basic concept of process
 
Deadlock Avoidance in Operating System
Deadlock Avoidance in Operating SystemDeadlock Avoidance in Operating System
Deadlock Avoidance in Operating System
 
Semaphores
SemaphoresSemaphores
Semaphores
 
AI3391 Session 13 searching with Non-Deterministic Actions and partial observ...
AI3391 Session 13 searching with Non-Deterministic Actions and partial observ...AI3391 Session 13 searching with Non-Deterministic Actions and partial observ...
AI3391 Session 13 searching with Non-Deterministic Actions and partial observ...
 
Interrupts in 8085
Interrupts in 8085Interrupts in 8085
Interrupts in 8085
 
Deadlock in database
Deadlock in databaseDeadlock in database
Deadlock in database
 
Computer Organization Lecture Notes
Computer Organization Lecture NotesComputer Organization Lecture Notes
Computer Organization Lecture Notes
 
Magnetic Disk
Magnetic Disk Magnetic Disk
Magnetic Disk
 
Chapter 7 - Deadlocks
Chapter 7 - DeadlocksChapter 7 - Deadlocks
Chapter 7 - Deadlocks
 
Basic Computer Organization and Design
Basic  Computer  Organization  and  DesignBasic  Computer  Organization  and  Design
Basic Computer Organization and Design
 
Theory of Computation Unit 1
Theory of Computation Unit 1Theory of Computation Unit 1
Theory of Computation Unit 1
 
Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)Infix to Prefix (Conversion, Evaluation, Code)
Infix to Prefix (Conversion, Evaluation, Code)
 

Viewers also liked

Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
Sara Ali
 
Operating System Deadlock Galvin
Operating System  Deadlock GalvinOperating System  Deadlock Galvin
Operating System Deadlock Galvin
Sonali Chauhan
 
Ch9: Memory Management
Ch9: Memory ManagementCh9: Memory Management
Ch9: Memory Management
Ahmar Hashmi
 
Mch7 deadlock
Mch7 deadlockMch7 deadlock
Mch7 deadlock
wahab13
 
Deadlock
DeadlockDeadlock
Deadlock
Abhinaw Rai
 
Deadlock
DeadlockDeadlock
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
Midhun Sankar
 
Distributed deadlock
Distributed deadlockDistributed deadlock
Distributed deadlock
Md. Mahedi Mahfuj
 
Os Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual MemoryOs Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual Memory
sgpraju
 
Memory management
Memory managementMemory management
Memory management
Muhammad Fayyaz
 
Basic Computer Organization and Design
Basic Computer Organization and DesignBasic Computer Organization and Design
Basic Computer Organization and Design
mekind
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
Amir Payberah
 
Microprogram Control
Microprogram Control Microprogram Control
Microprogram Control
Anuj Modi
 
Memory management
Memory managementMemory management
Memory management
Rajni Sirohi
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
Jussi Pohjolainen
 

Viewers also liked (15)

Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
 
Operating System Deadlock Galvin
Operating System  Deadlock GalvinOperating System  Deadlock Galvin
Operating System Deadlock Galvin
 
Ch9: Memory Management
Ch9: Memory ManagementCh9: Memory Management
Ch9: Memory Management
 
Mch7 deadlock
Mch7 deadlockMch7 deadlock
Mch7 deadlock
 
Deadlock
DeadlockDeadlock
Deadlock
 
Deadlock
DeadlockDeadlock
Deadlock
 
Deadlocks in operating system
Deadlocks in operating systemDeadlocks in operating system
Deadlocks in operating system
 
Distributed deadlock
Distributed deadlockDistributed deadlock
Distributed deadlock
 
Os Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual MemoryOs Swapping, Paging, Segmentation and Virtual Memory
Os Swapping, Paging, Segmentation and Virtual Memory
 
Memory management
Memory managementMemory management
Memory management
 
Basic Computer Organization and Design
Basic Computer Organization and DesignBasic Computer Organization and Design
Basic Computer Organization and Design
 
Deadlocks
DeadlocksDeadlocks
Deadlocks
 
Microprogram Control
Microprogram Control Microprogram Control
Microprogram Control
 
Memory management
Memory managementMemory management
Memory management
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 

Similar to Deadlocks

OS-Part-06.pdf
OS-Part-06.pdfOS-Part-06.pdf
OS-Part-06.pdf
NguyenTienDungK17HL
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
sheraz7288
 
3 (2).ppt
3 (2).ppt3 (2).ppt
3 (2).ppt
amadayshwan
 
Section07-Deadlocks (1).ppt
Section07-Deadlocks (1).pptSection07-Deadlocks (1).ppt
Section07-Deadlocks (1).ppt
amadayshwan
 
Section07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.pptSection07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.ppt
jbri1395
 
3 (1) [Autosaved].ppt
3 (1) [Autosaved].ppt3 (1) [Autosaved].ppt
3 (1) [Autosaved].ppt
amadayshwan
 
chapter06-new.pptx
chapter06-new.pptxchapter06-new.pptx
chapter06-new.pptx
Umesh Hengaju
 
Section07-Deadlocks.pdf
Section07-Deadlocks.pdfSection07-Deadlocks.pdf
Section07-Deadlocks.pdf
MogilicharlaPavanKal
 
Gp1242 007 oer ppt
Gp1242 007 oer pptGp1242 007 oer ppt
Gp1242 007 oer ppt
Nivedita Kasturi
 
Deadlocks Part- III.pdf
Deadlocks Part- III.pdfDeadlocks Part- III.pdf
Deadlocks Part- III.pdf
Harika Pudugosula
 
Deadlock
DeadlockDeadlock
Deadlock
Farhat Shaikh
 
Os case study word
Os case study wordOs case study word
Os case study word
Dhol Yash
 
Deadlock
DeadlockDeadlock
Deadlock
Mohd Arif
 
OS Module-3 (2).pptx
OS Module-3 (2).pptxOS Module-3 (2).pptx
OS Module-3 (2).pptx
KokilaK25
 
Deadlock
DeadlockDeadlock
Deadlock
Mayuri Verma
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
Google
 
Deadlock (1).ppt
Deadlock (1).pptDeadlock (1).ppt
Deadlock (1).ppt
Nusaike Mufthie
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
ushabarad142
 
Deadlocks Part- I.pdf
Deadlocks Part- I.pdfDeadlocks Part- I.pdf
Deadlocks Part- I.pdf
Harika Pudugosula
 
OS - Unit 3 Deadlock (Bankers Algorithm).pptx
OS - Unit 3 Deadlock (Bankers Algorithm).pptxOS - Unit 3 Deadlock (Bankers Algorithm).pptx
OS - Unit 3 Deadlock (Bankers Algorithm).pptx
GovindJha93
 

Similar to Deadlocks (20)

OS-Part-06.pdf
OS-Part-06.pdfOS-Part-06.pdf
OS-Part-06.pdf
 
7308346-Deadlock.pptx
7308346-Deadlock.pptx7308346-Deadlock.pptx
7308346-Deadlock.pptx
 
3 (2).ppt
3 (2).ppt3 (2).ppt
3 (2).ppt
 
Section07-Deadlocks (1).ppt
Section07-Deadlocks (1).pptSection07-Deadlocks (1).ppt
Section07-Deadlocks (1).ppt
 
Section07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.pptSection07-Deadlocks_operating_system.ppt
Section07-Deadlocks_operating_system.ppt
 
3 (1) [Autosaved].ppt
3 (1) [Autosaved].ppt3 (1) [Autosaved].ppt
3 (1) [Autosaved].ppt
 
chapter06-new.pptx
chapter06-new.pptxchapter06-new.pptx
chapter06-new.pptx
 
Section07-Deadlocks.pdf
Section07-Deadlocks.pdfSection07-Deadlocks.pdf
Section07-Deadlocks.pdf
 
Gp1242 007 oer ppt
Gp1242 007 oer pptGp1242 007 oer ppt
Gp1242 007 oer ppt
 
Deadlocks Part- III.pdf
Deadlocks Part- III.pdfDeadlocks Part- III.pdf
Deadlocks Part- III.pdf
 
Deadlock
DeadlockDeadlock
Deadlock
 
Os case study word
Os case study wordOs case study word
Os case study word
 
Deadlock
DeadlockDeadlock
Deadlock
 
OS Module-3 (2).pptx
OS Module-3 (2).pptxOS Module-3 (2).pptx
OS Module-3 (2).pptx
 
Deadlock
DeadlockDeadlock
Deadlock
 
Chapter 03
Chapter 03Chapter 03
Chapter 03
 
Deadlock (1).ppt
Deadlock (1).pptDeadlock (1).ppt
Deadlock (1).ppt
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Deadlocks Part- I.pdf
Deadlocks Part- I.pdfDeadlocks Part- I.pdf
Deadlocks Part- I.pdf
 
OS - Unit 3 Deadlock (Bankers Algorithm).pptx
OS - Unit 3 Deadlock (Bankers Algorithm).pptxOS - Unit 3 Deadlock (Bankers Algorithm).pptx
OS - Unit 3 Deadlock (Bankers Algorithm).pptx
 

Recently uploaded

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
TechSoup
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
IreneSebastianRueco1
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
PECB
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
camakaiclarkmusic
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
Bisnar Chase Personal Injury Attorneys
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
adhitya5119
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
David Douglas School District
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
Dr. Shivangi Singh Parihar
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
Celine George
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
mulvey2
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
National Information Standards Organization (NISO)
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 

Recently uploaded (20)

Introduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp NetworkIntroduction to AI for Nonprofits with Tapp Network
Introduction to AI for Nonprofits with Tapp Network
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
RPMS TEMPLATE FOR SCHOOL YEAR 2023-2024 FOR TEACHER 1 TO TEACHER 3
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 
Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.Types of Herbal Cosmetics its standardization.
Types of Herbal Cosmetics its standardization.
 
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
ISO/IEC 27001, ISO/IEC 42001, and GDPR: Best Practices for Implementation and...
 
CACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdfCACJapan - GROUP Presentation 1- Wk 4.pdf
CACJapan - GROUP Presentation 1- Wk 4.pdf
 
Top five deadliest dog breeds in America
Top five deadliest dog breeds in AmericaTop five deadliest dog breeds in America
Top five deadliest dog breeds in America
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Advanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docxAdvanced Java[Extra Concepts, Not Difficult].docx
Advanced Java[Extra Concepts, Not Difficult].docx
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
Pride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School DistrictPride Month Slides 2024 David Douglas School District
Pride Month Slides 2024 David Douglas School District
 
PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.PCOS corelations and management through Ayurveda.
PCOS corelations and management through Ayurveda.
 
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptxChapter 4 - Islamic Financial Institutions in Malaysia.pptx
Chapter 4 - Islamic Financial Institutions in Malaysia.pptx
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
How to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold MethodHow to Build a Module in Odoo 17 Using the Scaffold Method
How to Build a Module in Odoo 17 Using the Scaffold Method
 
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptxC1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
C1 Rubenstein AP HuG xxxxxxxxxxxxxx.pptx
 
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
Pollock and Snow "DEIA in the Scholarly Landscape, Session One: Setting Expec...
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 

Deadlocks

  • 2. Deadlocks • Resource • Introduction to deadlocks • The ostrich algorithm • Deadlock detection and recovery • Deadlock avoidance • Deadlock prevention • Other issues
  • 3. 3 Resources • Examples of computer resources – Printers – tape drives – Tables (systems internal table) – Semaphores ( critical section) • Processes need access to resources in reasonable order • Suppose a process holds resource A and requests resource B – at same time another process holds B and requests A – both are blocked and remain so……… this situation is called DEADLOCK I/O device ( hardware)
  • 4. 4 Resources (1) • Deadlocks occur when … – processes are granted exclusive access to devices – we refer to these devices generally as resources.  Resourses :  A resource Can be hardware device ( eg. Tape drive, printer) ,  a piece of information ( a locked record in a database )  For some resources several identical instances are available ( 3 tape drive) • A resource is any thing that must be acquired , used. And released over the course of time
  • 5. Resources (2) • Preemptable resources – can be taken away from a process with no ill effects – eg : Memory ( by swapping ) • Nonpreemptable resources – will cause the process to fail if taken away – eg: CD ROM ( result garbled CD – Computational failure) In general deadlock involve with preemptable resourses can be solved by reallocationg resourses from one process to another . Our discussion focus on nonpreemtable resourses
  • 6. 6 Backing Store (a) Paging to static swap area (b) Backing up pages dynamically
  • 7. 7 Resources (2) • Sequence of events required to use a resource 1. request the resource 2. use the resource 3. release the resource • Must wait if request is denied – requesting process may be blocked ( eg:semaphore) – may fail with error code ( error notice)  Request can ne placed by request system call( I/O device, tables ect.)  For some kind of resources ( critical section resources) the user process can manage using semaphores
  • 8. 8 Introduction to Deadlocks • Formal definition : A set of processes is deadlocked if each process in the set is waiting for an event that only another process in the set can cause • Usually the event is release of a currently held resource • None of the processes can … – run – release resources – be awakened This kind of deadlock is called Resource Deadlock( this is probably the most common kind )
  • 9. 9 Four Conditions for Resource Deadlock 1. Mutual exclusion condition • each resource assigned to 1 process or is available 1. Hold and wait condition • process holding resources can request additional 1. No preemption condition • previously granted resources cannot forcibly taken away 1. Circular wait condition • must be a circular chain of 2 or more processes • each is waiting for resource held by next member of the chain
  • 10. • The four condition relates to a policy that a system can have /not have  Can a given resource be assigned to more than one process at once ?  Can a process hold a resource and ask for another?  Can resource be preemted?  Can circular wait exist?
  • 11. 11 Deadlock Modeling (1) • Modeled with directed graphs ( 1972 Holt showed how the four condition can be modeled) – resource R assigned to process A – process B is requesting/waiting for resource S – process C and D are in deadlock over resources T and U
  • 12. 12 Deadlock Modeling (2) Resource allocation graphs • Resource allocation modeled by directed graphs • Example 1: – Resource R assigned to process A • Example 2: – Process B is requesting / waiting for resource S • Example 3: – Process C holds T, waiting for U – Process D holds U, waiting for T R A S B U T DC
  • 13. 13How deadlock occurs A B C Deadlock Modeling (3)
  • 14. 14 Deadlock Modeling (5) How deadlock can be avoided (o) (p) (q)
  • 15. Four Strategies dealing with dead lock 1. Just ignore the problem . May be if you ignore it, it will ignore you 2.Detection & recovery. Let deadlock happen, detect them and take action 3.Dynamic avoidance by careful resource allocation 4.Prevention, by structurally negating one of the four condition.
  • 16. 16 The Ostrich Algorithm • Pretend there is no problem “Hide your head in the sand and pretend there is no problem at all “ • Reasonable if – deadlocks occur very rarely – cost of prevention is high • It is a trade off between – convenience – correctness
  • 17. 2.Detection & recovery • The system does not prevent deadlock from occurring .Instead ……………. -it lets them occur -tries to detect when this happens -and take some action to recover after that.
  • 18. 18 Detecting deadlocks using graphs An example : Detection with One Resource of Each Type •Process holdings and requests in the table and in the graph (they’re equivalent) •Graph contains a cycle => deadlock! – Easy to pick out by looking at it (in this case) – Need to mechanically detect deadlock •Not all processes are deadlocked (A, C, F not in deadlock) R A S F W C Process Holds Wants A R S B T C S D U S,T E T V F W S G V U ED G B T VU
  • 19. 19 Deadlock detection algorithm • General idea: try to find cycles in the resource allocation graph • Algorithm: depth-first search at each node – Mark arcs as they’re traversed – Build list of visited nodes – If node to be added is already on the list, a cycle exists! • Cycle == deadlock For each node N in the graph { Set L = empty list unmark all arcs Traverse (N,L) } If no deadlock reported by now, there isn’t any define Traverse (C,L) { If C in L, report deadlock! Add C to L For each unmarked arc from C { Mark the arc Set A = arc destination /* NOTE: L is a local variable */ Traverse (A,L) } }
  • 20. 20 Resources with multiple instances • Previous algorithm only works if there’s one instance of each resource • If there are multiple instances of each resource, we need a different method – Track current usage and requests for each process – To detect deadlock, try to find a scenario where all processes can finish – If no such scenario exists, we have deadlock
  • 21. Example •E=(4, 2, 3, 1 ) A=(2, 1, 0, 0) ( Resource in existence) ( Resource Available) •3 processes 0 0 1 0 2 0 0 1 c= 2 0 0 1 R= 1 0 1 0 0 1 2 0 2 1 0 0 current allocation matrix Request matrix -p3 can run and return all its resources giving A=(2 , 2, 2 ,0 ) - At this point p2 can run A( 4 2 2 1) -Now P1 can run no dead lock in the system Tape , plotters, scanner, Cd rom Tape , plotters, scanner, Cd rom
  • 22. Chapter 3 22 Deadlock detection algorithm A B C D Avail 2 3 0 1 Process A B C D 1 0 3 0 0 2 1 0 1 1 3 0 2 1 0 4 2 2 3 0 Process A B C D 1 3 2 1 0 2 2 2 0 0 3 3 5 3 1 4 0 4 1 1 HoldWant current=avail; for (j = 0; j < N; j++) { for (k=0; k<N; k++) { if (finished[k]) continue; if (want[k] < current) { finished[k] = 1; current += hold[k]; break; } if (k==N) { printf “Deadlock!n”; // finished[k]==0 means process is in // the deadlock break; } } Note: want[j],hold[j],current,avail are arrays!
  • 23. 23 Recovery from Deadlock (1) • Recovery through preemption – take a resource from some other process – depends on nature of the resource • Recovery through rollback – checkpoint a process periodically – use this saved state – restart the process if it is found deadlocked
  • 24. 24 Recovery from Deadlock (2) • Recovery through killing processes – crudest but simplest way to break a deadlock – kill one of the processes in the deadlock cycle – the other processes get its resources – choose process that can be rerun from the beginning
  • 25. Dead Lock Avoidance • Question Is there an algorithm that can avoid deadlock by making the right choice at right time???? Ans: Yes – we can avoid dead lock , but only if certain information is available in advance The main Algorithm for avoiding algorithm is based on safe state
  • 26. 26 Deadlock Avoidance Resource Trajectories Two process resource trajectories
  • 27. Safe & Unsafe Stataes • At any Instant of time we have E, A , C, A • A state is said to be safe if there is some scheduling order in which every process can run to completion even if all of them suddenly request there maximum number of resources immediate Example: Three processes A, B, C , & one resource type ( 10 Instances ( copies))
  • 28. Safe and unsafe states Has Max A 3 9 B 2 4 C 2 7 Free: 3 Has Max A 3 9 B 4 4 C 2 7 Free: 1 Has Max A 3 9 B 0 - C 2 7 Free: 5 Has Max A 3 9 B 0 - C 7 7 Free: 0 Has Max A 3 9 B 0 - C 0 - Free: 7 Demonstration that the first state is safe Has Max A 3 9 B 2 4 C 2 7 Free: 3 Has Max A 4 9 B 2 4 C 2 7 Free: 2 Has Max A 4 9 B 4 4 C 2 7 Free: 0 Has Max A 4 9 B 0 - C 2 7 Free: 4 Demonstration that the second state is unsafe
  • 29. Banker's Algorithm for a single resource ( Dijkstra • It is an extension of deadlock detection algorithm ( One resource of each type) • It is modeled one the way a small time banker who is dealing with customer to whom he has to granted credits • Example : Four customers . To whom hes has granted a certain number of credits ( Iunit- 1K$) (in our case customers are process , cdret – resources ( say tape driver) and the banker is the OS
  • 30. Banker's Algorithm for a single resource ( Dijkstra) Has Max A 0 6 B 0 5 C 0 4 D 0 7 Free: 10 Has Max A 1 6 B 1 5 C 2 4 D 4 7 Free: 2 Has Max A 1 6 B 2 5 C 2 4 D 4 7 Free: 1 • Bankers’ algorithm: before granting a request, ensure that a sequence exists that will allow all processes to complete – Use previous methods to find such a sequence – If a sequence exists, allow the requests – If there’s no such sequence, deny the request • Can be slow: must be done on each request! Any sequence finishes C,B,A,D finishes Deadlock (unsafe state)
  • 31. Process D can finish , then A or E followed by rge rest Example of banker's algorithm with multiple resources Banker's Algorithm for multiple resources C R
  • 32. Chapter 3 32CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Preventing deadlock • Deadlock can be completely prevented! • Ensure that at least one of the conditions for deadlock never occurs – Mutual exclusion – Circular wait – Hold & wait – No preemption • Not always possible…
  • 33. Chapter 3 33 Eliminating mutual exclusion • Some devices (such as printer) can be spooled – Only the printer daemon uses printer resource – This eliminates deadlock for printer • Not all devices can be spooled • Principle: – Avoid assigning resource when not absolutely necessary – As few processes as possible actually claim the resource
  • 34. 34 Attacking “hold and wait” • Require processes to request resources before starting – A process never has to wait for what it needs • This can present problems – A process may not know required resources at start of run – This also ties up resources other processes could be using • Processes will tend to be conservative and request resources they might need • Variation: a process must give up all resources before making a new request – Process is then granted all prior resources as well as the new ones – Problem: what if someone grabs the resources in the meantime— how can the process save its state?
  • 35. Chapter 3 35CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Attacking “no preemption” • This is not usually a viable option • Consider a process given the printer – Halfway through its job, take away the printer – Confusion ensues! • May work for some resources – Forcibly take away memory pages, suspending the process – Process may be able to resume with no ill effects
  • 36. Chapter 3 36CS 1550, cs.pitt.edu (originaly modified by Ethan L. Miller and Attacking “circular wait” • Assign an order to resources • Always acquire resources in numerical order – Need not acquire them all at once! • Circular wait is prevented – A process holding resource n can’t wait for resource m if m < n A 1 B C D 23
  • 37. 37 Deadlock prevention: summary • Mutual exclusion – Spool everything • Hold and wait – Request all resources initially • No preemption – Take resources away • Circular wait – Order resources numerically