Title: Introduction to Deadlock
1Introduction to Deadlock
- Lecture 21
- Klara Nahrstedt
2CS241 Administrative
3Content of This Lecture
- Goals
- Understanding deadlock
- How to avoid deadlocks
- Things covered in this lecture
- Deadlock concepts
- Deadlock conditions
4Resource (1)
- A resource is a commodity needed by a process.
- Resources can be either
- serially reusable e.g., CPU, memory, disk space,
I/O devices, files. acquire ? use ? release - Real life analogy?
- consumable produced by a process, needed by a
process e.g., messages, buffers of information,
interrupts. create ? acquire ? use Resource
ceases to exist after it has been used, so it is
not released. - Real life analogy?
5Resource (2)
- Resources can also be either
- preemptible e.g., CPU, central memory
- What does it mean?
- non-preemptible e.g., tape drives.
- Any other examples?
- And resources can be either
- shared among several processes or
- dedicated exclusively to a single process.
6Using Semaphore to Share Resource
Process Q() A.Down() B.Down() use
both resource B.Up() A.Up()
Process P() A.Down() B.Down() use
both resource B.Up() A.Up()
Is it OK?
7But Deadlock can Happen!
Process Q() B.Down() A.Down() use
both resources A.Up() B.Up()
Process P() A.Down() B.Down() use
both resources B.Up() A.Up()
DEADLOCK
8Deadlock
Mechanism for Deadlock Control
9Another Example - Remember Dining Philosophers?
10Dining Philosopher Challenge
- Think Eat
- N Philosophers circular table with N chopsticks
- To eat the Philosopher must first pickup two
chopsticks - ith Philosopher needs ith i1th chopstick
- Only put down chopstick when Philosopher has
finished eating - Devise a solution which satisfies mutual
exclusion but avoids starvation and deadlock
11Seems simple enough ?
- while(true)
- think()
- sem_wait(chopsticki)
- sem_wait(chopstick(i1) N)
- eat()
- sem_post(chopstick(i1) N)
- sem_post(chopsticki)
-
12 Deadlock (Each P. holds left fork)
- while(true)
- think()
- sem_wait(chopsticki)
- sem_wait(chopstick(i1) N)
- eat()
- sem_post(chopstick(i1) N)
- sem_post(chopsticki)
-
13Deadlock Definition
- What is a deadlock?
- A process is deadlocked if it is waiting for an
event that will never occur. - Typically, but not necessarily, more than one
process will be involved together in a deadlock
(the deadly embrace). - Is deadlock the same as starvation (or
indefinitely postponed)? - A process is indefinitely postponed if it is
delayed repeatedly over a long period of time
while the attention of the system is given to
other processes. I.e., logically the process may
proceed but the system never gives it the CPU.
14Conditions for Deadlock
- What conditions should exist in order to lead to
a deadlock - Real life analogy such as
- You take the monitor, I grab the keyboard
- Cars in intersection
15Necessary and Sufficient Conditions for Deadlock
- Mutual exclusion
- Processes claim exclusive control of the
resources they require - Wait-for condition
- Processes hold resources already allocated to
them while waiting for additional resources - No preemption condition
- Resources cannot be removed from the processes
holding them until used to completion - Circular wait condition
- A circular chain of processes exists in which
each process holds one or more resources that are
requested by the next process in the chain
16Mutual Exclusion
- Processes claim exclusive control of the
resources they require - How to break it?
17Wait-for Condition
- Processes hold resources already allocated to
them while waiting for additional resources - How to break it?
18(No Transcript)
19No Preemption Condition
- Resources cannot be removed from the processes
holding them until used to completion - How to break it?
20Its difficult to take a tape drive away from a
process that is busy writing a tape
21Circular Wait Condition
- A circular chain of processes exists in which
each process holds one or more resources that are
requested by the next process in the chain - How to break it?
22(No Transcript)
23Deadlock Modeling (1)
- Resource allocation graph
- Graph have two kinds of nodes
- Processes
- Resources
- Graphs arcs have two meanings
- Arc from resource node to process node process
holds resource (i.e., resource has been assigned
to process and process is holding it) - Arc from process node to resource node process
requests resource (i.e., process is currently
blocked waiting for that resource)
24Deadlock Modeling (2)
- Modeled with directed graphs
- 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
assign
request
25Deadlock Modeling (3)
A B
C
26Deadlock Modeling (4)
(o) (p)
(q)
- How deadlock can be avoided
27Resource Allocation Graph(multiple resource
types)
28Deadlock Model
29Strategies
- Strategies for dealing with Deadlocks
- detection and recovery
- dynamic avoidance
- careful resource allocation
- prevention
- negating one of the four necessary conditions
30Summary
- Principles of Deadlock
- Examples of Deadlock
- Deadlock Conditions
- Deadlock Modeling