cs550 Operating Systems David Monismith Deadlock Four requirements for deadlock Hold and wait process holds resource while waiting for more resources Mutual Exclusion resource cant be shared ID: 472689
Download Presentation The PPT/PDF document "Deadlock" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Deadlock
cs550
Operating Systems
David
MonismithSlide2
Deadlock
Four requirements for deadlock
Hold and wait - process holds resource while waiting for more resources
Mutual Exclusion - resource can't be shared
Circular Wait - processes want something that
another process has.
No preemption - can't
interrupt a process's hold
on a resourceSlide3
Resource allocation graph
Circles
represent
processes
Rectangles
represent
resources
Dots
within the resource represent the number of available resources of that
type
Arrows
from resources (rectangle) to process (circle) indicates resource
ownership
Arrows
from process (circle)
to
resource (rectangle) indicates need/request Slide4
We can show deadlock is possible by finding an example of deadlock and showing all 4 requirements exist We can show deadlock does not exist by proving that one of the requirements
does
not
exist.
We can show deadlock does not exist by proving that one of the requirements does not
exist
.
ExampleSlide5
Example
:
Dining Philosophers
Five students eating at Joy Wok with
chopsticks
Only
five chopsticks available.
A
student needs two chopsticks to eat.
Chopsticks
are situated between students.
Students
can't move from their positions at the
table.
Students
alternate between thinking and eating.
Slide6
\ (S1) | (S2)
/
(S3) Rice (S4)
/ (S5)
\
A possible solution: Students always reach right first.
Example: Dining Philosophers (Contd..)
(S1)-Req.-->[R2]--Acq.-->(S2)--Req.-->[R3]--Acq.-->(S3)--Req.-->[R4]--Acq.-->(S4)--Req.--+ ^ | | |
+---------------------------------Req.----(S5)<----Acq.---[R5]<-----------------------------------------------+
Slide7
How to deal with deadlock?
Ignore it!
Detect and recover from it!
Avoid it! (make deadlock impossible)
Prevent it! (short circuit one of the four conditions preferably hold and wait or circular wait)
We need mutual exclusion and prefer not to preempt necessary resources
Slide8
Many
OSes
(if not all) allow for deadlock to occur. It is easy to reboot. Detection can also be used.
How to detect? Keep a version of the resource allocation graph available in OS memory.
Update graph based upon resource requests and releases
Asynchronous graph algorithms can be used to look for cycles on the RAG
Break the cycle!
(
we can terminate processes or preempt resources)
We can look for idle programs but this rarely works
Slide9
Deadlock
Deadlock will occur with the dining philosophers problem using the following
pseudocode
Acquire left fork (or right)
Acquire right fork
Start eating if possible
After eating, release forks
Do work (start thinking)
Prevention
Make one condition for deadlock impossible
Mutual exclusion
Hold and wait
Circular Wait
No preemption
Slide10
Our focus will be on hold and wait and circular wait
Two strategies exist for hold and wait
Process
must acquire all resources it needs before
starting
good
for batch
systems
Process
must release all resources before requesting new resources good for interactive systems Let the philosophers acquire forks only if both are available
Slide11
Pseudocode
w
hile
true
Acquire
semaphore
1.
Check
if both forks are
available
and
continue, otherwise wait.
2.
Attempt
to acquire the left fork
3.
Attempt
to acquire the right fork
4.
Eat
for a specified time
period
and
release forks
Release
semaphore
Think
for a specified time
period
end whileSlide12
We can also disallow circular wait
Need
to draw circular wait
here
We
can assign ordering to resources
Give
each fork a number that is unique
We
can prevent deadlock with ordering of forks Check the index values of the forks
Require the forks to always be acquired in order of the index values
Circular Wait Slide13
Philosopher code
Check if the index value of the left fork is less than the index value of the right fork. If true, do 2 then 3. Otherwise, do 3 then 2.
Attempt to acquire the left chopstick
Attempt to acquire the right chopstick .
Eat for specified time period
Release forks
Think for a specified time period
How could we program these in Java? In C?