Presentation is loading. Please wait.

Presentation is loading. Please wait.

Deadlock cs550 Operating Systems David Monismith.

Similar presentations


Presentation on theme: "Deadlock cs550 Operating Systems David Monismith."— Presentation transcript:

1 Deadlock cs550 Operating Systems David Monismith

2 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 resource

3 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

4 o 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. o We can show deadlock does not exist by proving that one of the requirements does not exist. Example

5 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.

6 \ (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]<-----------------------------------------------+

7 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

8 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

9 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

10 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

11 Pseudocode while 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 while

12 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

13 Philosopher code 1. 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. 2. Attempt to acquire the left chopstick 3. Attempt to acquire the right chopstick. 4. Eat for specified time period 5. Release forks 6. Think for a specified time period How could we program these in Java? In C?


Download ppt "Deadlock cs550 Operating Systems David Monismith."

Similar presentations


Ads by Google