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

OS Chapter 3 Deadlock

The document discusses deadlocks in computing systems. It defines deadlocks, describes the four conditions required for deadlocks, and discusses various strategies for dealing with deadlocks including detection and recovery, avoidance through careful resource allocation, and prevention by negating one of the four conditions.

Uploaded by

habtamuasefa563
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)
29 views

OS Chapter 3 Deadlock

The document discusses deadlocks in computing systems. It defines deadlocks, describes the four conditions required for deadlocks, and discusses various strategies for dealing with deadlocks including detection and recovery, avoidance through careful resource allocation, and prevention by negating one of the four conditions.

Uploaded by

habtamuasefa563
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/ 38

DEADLOCK

Contents
2

Ω Resource
Ω Introduction to deadlocks
Ω The Ostrich algorithm
Ω Deadlock detection and recovery
Ω Deadlock avoidance
Ω Deadlock prevention
Ω Other issues
Resources
3

 A resource is anything that must be acquired, used, and


released over the course of time.
 Examples of computer resources:-
 printers
 tape drives

 slots in system’s internal tables

 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.
… Cont’d
4

 Depend on content of using resource can be:-


 Preemptable resources

 can be taken away from a process with no ill effects


• Memory
 Non-preemptable resources

 is one that cannot be taken away


 will cause the process to fail if taken away
• Disk
• Table record on transaction
… Cont’d
5

 Abstract sequence of events :-


a) may required to use a resource,
 request the resource
 use the resource
 release the resource
b) Must wait if request is denied,
 requesting process may be blocked
 may fail with error code
Introduction to Deadlocks
6

 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

 No progress
Four Conditions for Deadlock
7

1. Mutual exclusion condition


 each resource assigned to 1 process or is available.
2. Hold and wait condition
 process holding resources can request additional.
3. No preemption condition
 previously granted resources cannot forcibly taken away
4. Circular wait condition
 must be a circular chain of 2 or more processes.
 Each of the processes are waiting for resource held by next
member of the chain.
 All four of these conditions must be present for a resource
deadlock to occur.
 If one of them is absent, no resource deadlock is possible.
Deadlock Modeling
8

 Modeled with directed graphs (Resource by square and process by circle).

√ 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 (Cycle
is C – T – D – U - C)
… Cont’d
9

 Strategies for dealing with Deadlocks:-


1. just ignore the problem altogether
2. detection and recovery
3. dynamic avoidance
# careful resource allocation
4. prevention
# negating one of the four necessary conditions
How deadlock will occur?
10
How deadlock can be avoided
11
Strategies to Dealing with Deadlocks
12

 Just ignore the problem


 Deadlock prevention
 Deadlock detection and recovery
 Deadlock avoidance
The Ostrich Algorithm
13

 Best algorithm for deadlock ignorance.


 Pretend/Imagine there is no problem
 Reasonable if:-
 deadlocks occur very rarely

 cost of prevention is high

 UNIX and Windows takes this approach


 It is a trade off between:
 convenience

 correctness
Detection with One Resource of
14
Each Type
 Consider a system with seven processes, A though G, and six
resources, R through W.
… Cont’d
15

 Note the resource ownership and requests


 A cycle can be found within the graph, denoting deadlock
Detection With Multiple Resources Of Each
Type
16

Data structures needed by deadlock detection algorithm

 Cij is the number of instances of resource j that are held by process i.


 Similarly, Rij is the number of instances of resource j that Pi wants.
… Cont’d
17

An example for the deadlock detection algorithm


… Cont’d
 The first one cannot be satisfied because there is no CD drive
available.
 The second cannot be satisfied either, because there is no
scanner free.
 The third one can be satisfied, so process 3 runs and eventually
returns all its resources, giving
A = (2 2 2 0)
 At this point process 2 can run and return its resources, giving
A = (4 2 2 1)
 Now the remaining process can run. There is no deadlock in the
system.
Recovery from Deadlock
19

a) Recovery through preemption


 temporarily take a resource away from its current owner and
give it to another process.
 depends on nature of the resource.

 Recovering this way is frequently difficult or impossible(i.e. loss


of data or failure).
b) Recovery through rollback
 checkpoint a process periodically(its state is written to a file so
that it can be restarted later contains memory usage, resource
state…)
 use this saved state

 restart the process if it is found deadlocked


… Cont’d
20

c) 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.


Deadlock Avoidance
21

a) Resource Trajectories
 It is based on the concept of safe state

Two process resource trajectories


… Cont’d
22

 At point t, B is requesting a resource.


 The system must decide whether to grant it or not.
 If the grant is made, the system will enter an unsafe region and
eventually deadlocked.
 To avoid the deadlock, B should be suspended until A has
requested and released the plotter.
b) Safe and Unsafe States
23

 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 their maximum number of resources immediately.

Total number of resource =10

(a) (b) (c) (d) (e)

Demonstrate that the state in (a) is safe


… Cont’d
√ The state of Fig. (a) is safe because there exists a sequence of
allocations that allows all processes to complete.
√ The scheduler can simply run B exclusively, until it asks for and
gets two more instances of the resource, state of Fig. (b).
√ When B completes, state of Fig. (c).
√ Then the scheduler can run C, Fig. (d). When C completes, Fig. (e).
√ Now A can get the six instances of the resource it needs and also
complete.
√ Thus, the state of Fig. (a) is safe because the system, by careful
scheduling, can avoid deadlock.
… Cont’d
25

Total number of resource =10

(a) (b) (c) (d)


Demonstrate that the state in b is not safe
 From a safe state the system can guarantee that all processes will finish;
 from an unsafe state, no such guarantee can be given.
c) The Banker's Algorithm for a Single
26
Resource
 Uses resource allocation and release rule to prevent deadlock from
occurring.

(a) (b) (c)


 Three resource allocation states
a) safe
b) safe
c) unsafe
d) Banker's Algorithm for Multiple Resources
27

E= Existing resource ,
P= possessed resource ,
A =Available resource ,

Example of banker's algorithm with multiple resources


… Cont’d
 The current state is safe.
 Suppose that process D now makes a request for the printer.
√ can be granted because the resulting state is still safe
£ process D can finish,
£ and then processes A or E,
£ followed by the rest which either B or C.
£ after giving B one of the two remaining printers,
£ E wants the last printer.
£ Granting that request would reduce the vector of available
resources to (1 0 0 0), which leads to deadlock, so E’s request
must be deferred.
Deadlock Prevention
29

 deadlock avoidance is essentially impossible,


⁜ because it requires information about future requests,
which is not know.
⁜ at least one of the four conditions is never satisfied, then
deadlocks will be structurally impossible.
1. Attacking Mutual Exclusion Condition
 If no resource were ever assigned exclusively to a single process,
we would never have deadlocks.
 Some devices (such as printer) can be spooled
√ only the printer daemon uses printer resource
√ thus deadlock for printer eliminated

 Not all devices can be spooled


√ Principle: avoid assigning resource when not absolutely necessary
√ as few processes as possible actually claim the resource
2. Attacking the Hold and Wait Condition
30

 Require processes to request resources before starting


 a process never has to wait for what it needs

 Problems
 may not know required resources at start of run
 also ties up resources other processes could be using

 Variation:
 process must give up all resources
 then request all immediately needed
3. Attacking the No Preemption Condition
31

 This is not a viable option


 Consider a process given the printer
 halfway through its job

 now forcibly take away printer


4. Attacking the Circular Wait Condition
32

 One way:- a process is entitled only to a single resource at any


moment. If it needs a second one, it must release the first one.
 Another way:-provide a global numbering of all the resources.

(a) Normally ordered resources

(b) A resource graph


… Cont’d
33

Summary of approaches to deadlock prevention


Other Issues
Two-Phase Locking
34

 Phase One
 process tries to lock all records it needs, one at a time.
 some record is needed that is already locked, the process just
releases all its locks and starts the first phase all over.
(no real work done in phase one)
 If phase one succeeds, it starts second phase,
 performing updates
 releasing locks
 Note similarity to requesting all resources at once, However, this
strategy is not applicable in general.
 Algorithm works where programmer can arrange things
 program can be stopped at phase 1, restarted.
Communication Deadlock
 Another kind of deadlock can occur in communication systems (e.g.,
networks), in which two or more processes communicate by sending
messages.
 A common arrangement is that process A sends a request message to
process B, and then blocks until B sends back a reply message.
 Communication deadlocks cannot be prevented by ordering the
resources (since there are not) or avoided by careful scheduling (since
there are no moments when a request could be postponed).
 The technique to break communication deadlocks is timeouts.
 whenever a message is sent to which a reply is expected a timer is also started.
 If the timer goes off before the reply arrives:-
 the sender of the message assumes that the message has been lost
and
 sends it again (and again and again if needed).
Livelock
 In some situations, polling (busy waiting) is used to enter a critical
region or access a resource.
 Let pair of processes (process A and process B)using two
resources.
 To be polite, they give up the lock they are currently holding and
try again.
 If the processes wait for required resource by spooling rather
than blocking, this situation is called livelock.
 Thus we do not have a deadlock (because no process is blocked)
but we have something functionally equivalent to deadlock; will
make no further progress.
Starvation
37

 Algorithm to allocate a resource


 may be to give to shortest job first

 Works great for multiple short jobs in a system

 May cause long job to be postponed indefinitely


 even though not blocked

 Solution:
 First-come, first-serve policy
Any Question?
38

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy