0% found this document useful (0 votes)
28 views32 pages

Chapter 05 Deadlocks Modified New 2023

Uploaded by

Henok Zeleke
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)
28 views32 pages

Chapter 05 Deadlocks Modified New 2023

Uploaded by

Henok Zeleke
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/ 32

Chapter 5- Deadlock

 What is deadlock?
 Necessary and Sufficient Deadlock Conditions
 Dealing with Deadlock Problem
 Deadlock Prevention
 Deadlock Avoidance
 Deadlock Detection
2
Deadlocks

 EXAMPLES IN REAL LIFE:


 "It takes money to make money“.
 You can't get a job without experience; you can't get experience
without a job.
 The egg and the chick problem.
 Bridge Crossing.
3
Deadlocks

 Deadlock is a situation where a set of processes are blocked because


each process is holding a resource and waiting for another resource
acquired by some other process.

 In an operating system, a deadlock occurs when a process or thread


enters a waiting state because a requested system resource is held by
another waiting process, which in turn is waiting for another resource
held by another waiting process.
4
Deadlocks

 The cause of deadlocks:


◦ Each process needs what another process has. This results from
sharing resources such as memory, devices, locks, links.
 Resource to be shared can be
 Preepmtable --- memory, locks, semaphores
 Non-preemptable --- Cd-Drive
 Under normal operation, a resource allocations proceed like this:
◦ 1. Request a resource (suspend until available if necessary )
◦ 2. Use the resource.
◦ 3. Release the resource.
5
Deadlock- Necessary Conditions

1. Mutual exclusion
◦ One or more than one resource must be held by a process in a non-
sharable (exclusive) mode.
2. Hold and Wait
◦ A process holds a resource while waiting for another resource.
3. No Preemption
◦ There is only voluntary release of a resource - nobody else can make a process
give up a resource.
4. Circular Wait
◦ Process A waits for Process B waits for Process C .... waits for Process A.
6
Deadlock- Resource Allocation
a visual example

 A utility program
◦ Copy a file from tape to disk
◦ Print the file to printer
 Resources
◦ Tape
◦ Disk
◦ Printer
 A deadlock
◦ A holds tape and disk, then requests for a printer
◦ B holds printer, then requests for tape and disk
7
Deadlock- Resource Allocation Graph

 A mathematical way to determine if a deadlock has, or may occur.


 G = ( V, E ) The graph contains nodes and edges.
 V – Nodes consist of processes = { P1, P2, P3, ...} and resource types { R1, R2, ...}
 E – Edges are ( Pi, Rj ) or ( Ri, Pj )
8
Deadlock- Resource Allocation Graph

 Set E contains …
 Set V contains….
 Set G contains….
 How many of instances of each resource are there?
 What is each process holding and requesting?
9
Deadlock- Resource Allocation Graph

 If the graph contains no cycles, then no process is deadlocked.


 If there is a cycle, then:
◦ a) If resource types have multiple instances, then deadlock MAY exist.
◦ b) If each resource type has 1 instance, then deadlock has occurred.
10
Deadlock- Resource Allocation Graph…
11
Deadlock- Resource Allocation
Graph…
 Exercise
 Construct a deadlock allocation graph for the following scenario.
 Is the system in dead lock, if so Why? And where?
 Process A hold R and wants S
 Process B holds nothing but wants T
 Process C holds nothing but wants S
 Process D holds U and wants S and T
 Process E holds T and wants V N.B. letters R-to-w are resources.
 Process F holds w and wants S
 Process G holds V and wants U
12
Deadlock- How to handle….General
Strategies
 Ignore the problem Most OS
use this.
 It is user’s fault
 Prevention
 Negate one of the four conditions

Allow
 Recovery deadlock
to
happen
 Fix the problem afterwards
 Detection and Dynamic Avoidance Ensure
deadlock
 Careful allocation never occurs
13
Deadlock… How to handle … Ignore the
problem

 The ostrich algorithm


“stick your head in the sand and pretend there is no problem at all”
 Different people reacts differently.

 What happens when the OS ignores the deadlock???


14
Deadlock- How to handle….. Ignore the
Problem…

 The OS kernel locks up  An application ran for a while


 Reboot and then hang
 Device driver locks up  Checkpoint the application
 Change the environment
 Remove the device
(reboot OS)
 Restart
 Restart from the previous
 An application hangs (“not checkpoint
responding”)
 Kill the application and restart
 Familiar with this?
Deadlock- How to handle….. 15
Prevention…

 Do not allow one of the four conditions to occur.


 Mutual exclusion:
◦ Automatically holds for printers and other non-sharables and allow for their
daemons.
◦ Allow only if it’s absolutely necessary.
◦ Shared entities (read only files) don't need mutual exclusion (and aren’t
susceptible to deadlock.)

 Hold and wait:


◦ Two ways of doing this—
◦ 1. Only request resources when have none and release before asking another.
◦ 2. Atomically acquire all resources at once ex. Using a single lock variable
◦ Utilization is low, starvation is possible. (dark side)
Deadlock- How to handle….. 16
Prevention…

 No preemption:
◦ Allow preemption - if a needed resource is held by another process, which is also
waiting on some resource, steal it. Otherwise wait.
◦ Release any resource already being held if the process can't get an additional
resource.
◦ Utilization is low, starvation is possible and sometimes impossible to pre-
emption. (dark side)

 Circular wait:
◦ Global Numbering of resources
◦ Only request in ascending order most of the time but descending also prevent
deadlock.
◦ Only one resource at a time for a process. If ask another– release the first one.
17
Deadlock- How to handle…..
Prevention…

 Each of these prevention techniques may cause a decrease in


utilization and/or cause starvation.
 For this reason, prevention isn't necessarily the best technique.
 Prevention is generally the easiest to implement.
18
Recovery…. Recovery through…
 Pre-emption
 Resources are preempted from the processes involved in the deadlock,
preempted resources are allocated to other processes so that there is a
possibility of recovering the system from deadlock. In this case, the system goes
into starvation.
 Some times manual intervention is possible
 Rollback
 Processes check pointed periodically
 State of a process can be written to file so that it can be restarted later.
 Memory image and resource states at different time intervals or points.
19
Recovery…. Recovery through…

 Through killing of processes


 killing all the process involved in the deadlock. Killing process one by one. After
killing each process check for deadlock again keep repeating the process till
system recover from deadlock.
 Rebooting and restart
 Kill processes in a cycle until the cycle breaks.
20
Detection

 Need an algorithm that determines if deadlock occurred.


 Also need a means of recovering from that deadlock.
 single instance of a resource type
 Wait-for graph == remove the resources from the usual graph and collapse edges.

 .
An edge from p(j) to p(i) implies that p(j) is waiting for p(i) to release

 Multiple instance of a resource type


 Use safe and unsafe state algorithm (Dijkstra’s-Banker’s algorithm)
21
Detection..

“Wait-for” Graph
22
Deadlock- How to handle…..
Deadlock Avoidance
 If we have prior knowledge of how resources will be requested,
it's possible to determine if we are entering an "unsafe" state.
Possible states are:
◦ Deadlock-No forward progress can be made.
◦ Unsafe state -A state that may allow deadlock.
◦ Safe state -A state is safe
◦ enough resources for the first to finish
 The rule is simple: If a request allocation would cause an unsafe state,
do not honor that request.
 NB: All deadlocks are unsafe, but all unsafe states are NOT deadlocks.
Deadlock- How to handle….. Deadlock 23
Avoidance.. Resource trajectory

° Trajectories move only


right and up!
• Programs do not run
backwards
• All paths must be Where state t can go to avoid deadlock?

horizontal or vertical,
never diagonal (if 1
CPU)
Joint state

Two process resource trajectories


24
Safe and Unsafe states

 Let's assume a very simple model:


 Each process declares its maximum needs.
 In a case where we have a total of 10 instances.
 In this case, algorithms exist that will ensure that no unsafe state is reached.

 Exercise-What if A requests and gets another resource?


25
Safe and Unsafe states..

 the system is not in a safe state!!!!


26
Deadlock- How to handle…. Deadlock
Avoidance… Safety Algorithm

Banker’s Algorithm
 Multiple instances.
 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
Deadlock- How to handle…. Deadlock
Avoidance… Safety Algorithm

 A method used to determine if a particular state is safe.


 It's safe if there exists a sequence of processes such that for all the processes,
there’s a way to avoid deadlock
 The algorithm uses these variables:
Need[I] – the remaining resource needs of each process.
Available/Work - Temporary variable – how many of the resource are
currently available.
Finish[I] – flag for each process showing we’ve analyzed that process or not.
need [I]= available + allocated[0] + .. + allocated[I-1] <- Sign of success
Deadlock- How to handle…. Deadlock 28
Avoidance… Safety Algorithm

1. Initialize Work[]= available[]


Initialize finish[i] = false, for i = 1,2,3,..n
2. Find an i such that:
finish[i] == false and need[i] <= available
If no such i exists, go to step 4.
3. Work[]= work[]+ allocation[i]
finish[i] = true
goto step 2
4. if finish[i] == true for all i, then the system is in a safe state.
Deadlock- How to handle…. Deadlock 29
Avoidance… Safety Algorithm

 Let’s do these examples:


Consider a system with: five
processes, P0-P4,
 Three resource types, A, B, C.
 Type A has 10 instances,
B has 5 instances,
C has 7 instances.
 At time T0 the following
snapshot of the system is
taken.
 Is the system in a safe state?
30
Deadlock- How to handle…. Deadlock
Avoidance… Safety Algorithm

Do these examples:
 Now try it again with only a slight change in the request by P1.
 P1 requests one additional resource of type A, and two more of type C.
 Request1 = (1,0,2).
 Is Request1 < available? And is the system in a safe state.
Exercise
 Can request for (3,3,0) by P4 be granted?
 Can request for (0,2,0) by P0 be granted?
Deadlock- How to handle…. Deadlock 31
Avoidance… Safety Algorithm
32

End of Chapter Five

You might also like