Chapter 6 Concurrency: Deadlock and Starvation

Slides:



Advertisements
Similar presentations
Chapter 7: Deadlocks.
Advertisements

Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
1 Concurrency: Deadlock and Starvation Chapter 6.
ICS Principles of Operating Systems Lectures 8 and 9 - Deadlocks Prof. Dmitri V. Kalashnikov dvk ics.uci.edu Slides © Prof. Nalini Venkatasubramanian.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Lecture 6 :Deadlocks. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate with each other Involves.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Concurrency: Deadlock and Starvation Chapter 6. Deadlock Permanent blocking of a set of processes that either compete for system resources or communicate.
DEADLOCK. Contents  Principles of deadlock  Deadlock prevention  Deadlock detection.
6. Deadlocks 6.1 Deadlocks with Reusable and Consumable Resources
Chapter 6 Concurrency: Deadlock and Starvation
Chapter 6 Concurrency: Deadlock and Starvation
Chapter 6 Concurrency: Deadlock and Starvation I
Chapter 7: Deadlocks. 7.2 Chapter Objectives To develop a description of deadlocks, which prevent sets of concurrent processes from completing their tasks.
Deadlock CSCI 444/544 Operating Systems Fall 2008.
Concurrency: Deadlock & Starvation
OS Spring 2004 Concurrency: Principles of Deadlock Operating Systems Spring 2004.
Witawas Srisa-an Chapter 6
CPSC 4650 Operating Systems Chapter 6 Deadlock and Starvation
OS Fall’02 Concurrency: Principles of Deadlock Operating Systems Fall 2002.
1 Concurrency: Deadlock and Starvation Chapter 6.
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition, Chapter 7: Deadlocks.
Chapter 6 Concurrency: Deadlock and Starvation
Deadlocks Gordon College Stephen Brinton. Deadlock Overview The Deadlock Problem System Model Deadlock Characterization Methods for Handling Deadlocks.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 7: Deadlock Dr. Mohamed Hefeeda.
What we will cover…  The Deadlock Problem  System Model  Deadlock Characterization  Methods for Handling Deadlocks  Deadlock Prevention  Deadlock.
Concurrency: Deadlock and Starvation Chapter 6. Goal and approach Deadlock and starvation Underlying principles Solutions? –Prevention –Detection –Avoidance.
1 Concurrency: Deadlock and Starvation Chapter 6.
Concurrency: Deadlock and Starvation
Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Deadlocks.
1 Announcements The fixing the bug part of Lab 4’s assignment 2 is now considered extra credit. Comments for the code should be on the parts you wrote.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
Cosc 4740 Chapter 6, Part 4 Deadlocks. The Deadlock Problem A set of blocked processes each holding a resource and waiting to acquire a resource held.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 7: Deadlocks.
CS6502 Operating Systems - Dr. J. Garrido Deadlock – Part 2 (Lecture 7a) CS5002 Operating Systems Dr. Jose M. Garrido.
CIS Operating Systems Deadlock Professor Qiang Zeng Fall 2015.
Deadlock Operating Systems: Internals and Design Principles.
Chapter 7 Deadlocks Page 2 Chapter 7: Deadlocks System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance.
Chapter 6 Concurrency: Deadlock and Starvation
Deadlocks System Model RAG Deadlock Characterization
Styresystemer og Multiprogrammering Block 3, 2005 Deadlocks Robert Glück.
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings.
Chapter 8 Deadlocks. Objective System Model Deadlock Characterization Methods for Handling Deadlocks Deadlock Prevention Deadlock Avoidance Deadlock Detection.
1 CS.217 Operating System By Ajarn..Sutapart Sappajak,METC,MSIT Chapter 6 Deadlocks Slide 1 Chapter 6 Deadlocks.
Silberschatz, Galvin and Gagne ©2009 Edited by Khoury, 2015 Operating System Concepts – 9 th Edition, Chapter 7: Deadlocks.
1 Deadlock.  System Model  Process must request a resource before using  Process must release the resource when done  Deadlock  A set of processes.
Informationsteknologi Monday, October 1, 2007Computer Systems/Operating Systems - Class 111 Today’s class Deadlock.
Lecture 6 Deadlock 1. Deadlock and Starvation Let S and Q be two semaphores initialized to 1 P 0 P 1 wait (S); wait (Q); wait (Q); wait (S);. signal (S);
Chapter 6 Concurrency: Deadlock and Starvation Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community.
Chapter 7: Deadlocks.
OPERATING SYSTEM CONCEPTS AND PRACTISE
Process Management Deadlocks.
Chapter 7: Deadlocks.
CSE 120 Principles of Operating
Concurrency: Deadlock and Starvation
ITEC 202 Operating Systems
Chapter 7: Deadlocks.
Chapter 7 Deadlock.
Chapter 7 Deadlocks.
Chapter 7: Deadlocks.
G.Anuradha Ref:- Galvin
Chapter 7: Deadlocks.
Deadlocks Session - 13.
DEADLOCK.
Chapter 7: Deadlocks.
Chapter 8: Deadlocks Deadlock Characterization
Presentation transcript:

Chapter 6 Concurrency: Deadlock and Starvation

Topics to Cover… Resources Deadlock Joint Process Diagrams Objectives Topics to Cover… Resources Deadlock Joint Process Diagrams Deadlock Conditions Circular Wait Resource Allocation Graph Handling Deadlock Avoidance Detection Recovery BYU CS 345 Concurrency

Resources Reusable Resources Used by one process at a time and not depleted by that use Processes obtain resources that they later release for reuse by other processes Processor time, I/O channels, main and secondary memory, files, databases, and semaphores Deadlock occurs if each process holds one resource and requests the other BYU CS 345 Concurrency Alex Milenkovich 3

Resources Consumable Resources Created (produced) and destroyed (consumed) by a process Interrupts, signals, messages, and information in I/O buffers Deadlock may occur if a Receive message is blocking May take a rare combination of events to cause deadlock BYU CS 345 Concurrency Alex Milenkovich 4

Deadlock System Model Deadlock Process must request a resource before using Process must release the resource when done Deadlock A set of processes is in a deadlock state when every process in the set is waiting for an event that can only be caused by another process in the set. BYU CS 345 Concurrency Alex Milenkovich 5

Quiz 6.1 How could deadlock occur when Answer… 200K bytes of memory is available for allocation by the system Process 1 needs 140K in 80K, 60K blocks Process 2 needs 150k in 70K, 80K blocks Answer… Process 1 Request 80K bytes … Request 60K bytes Process 2 … Request 70K bytes Request 80K bytes BYU CS 345 Concurrency Alex Milenkovich 6

Quiz 6.2 How could deadlock occur when Answer… Two processes need to communicate via send/receive messages Process 1 waits to hear from process 2 before sending data Process 2 proceeds after hearing from process 1 Answer… Process 1 Receive(P2) … Send(P2) Process 2 … Receive(P1) Send(P1) BYU CS 345 Concurrency Alex Milenkovich 7

Quiz 6.3 How is this deadlock?? Deadlock BYU CS 345 Concurrency Alex Milenkovich 8

Joint Process Diagram ? Diagrams Fatal Region P has A, Q has B Progress of Q 1 2 Release A A Required Both P and Q have A Release B B Required Both P and Q have B Get A P has B Q has A 5 ? 4 Get B 6 3 Fatal Region P has A, Q has B Deadlock! Progress of P Get A Get B Release A Release B A Required B Required BYU CS 345 Concurrency Alex Milenkovich 9

Joint Process Diagram ? Diagrams P has A Q has B Ok! P has A and B Progress of Q 1 2 P has A and B Q wants A Can’t get there! Release B B Required Both P and Q have B Release A Q has A wants B, P wants A? Can’t get there! A Required Both P and Q have A Get B ? 4 Get A 3 Progress of P Get A Get B Release A Release B A Required B Required BYU CS 345 Concurrency Alex Milenkovich 10

Joint Process Diagram Diagrams BYU CS 345 Concurrency Alex Milenkovich Progress of Q 1 2 3 Release A Both P and Q have A 6 A Required Release B Both P and Q have B Get A B Required 5 Get B 4 Progress of P Get A Release A Get B Release B A Required B Required BYU CS 345 Concurrency Alex Milenkovich 11

Necessary Conditions Mutual exclusion Hold-and-wait No preemption only one process may use a resource at a time. no process may access resource allocated to another. Hold-and-wait a process may hold allocated resources while awaiting assignment of other resources. No preemption no resource can be forcibly removed from a process holding it. BYU CS 345 Concurrency Alex Milenkovich 12

Conditions for Deadlock Necessary Conditions Conditions for Deadlock Circular wait a closed chain of processes exists, such that each process holds at least one resource needed by the next process in the chain consequence of the first three conditions Other conditions are necessary but not sufficient for deadlock - all four conditions must hold for deadlock Unresolvable circular wait is the definition of deadlock! BYU CS 345 Concurrency Alex Milenkovich 13

Circular Wait Circular Wait BYU CS 345 Concurrency Alex Milenkovich Resource A Process P1 Requests Process P2 Held by Held By Requests Resource B BYU CS 345 Concurrency Alex Milenkovich 14

Resource Allocation Graph Describing Deadlock Deadlocks can be described using resource allocation graph Vertices Active processes {P1, P2, … } Resources {R1, R2, … } Edges A directed edge from Pi to Rj Process Pi requested an instance of resource Rj A directed edge from Rj to Pi Resource Rj has been allocated to process Pi Process are circles, resources are rectangles BYU CS 345 Concurrency Alex Milenkovich 15

Resource Allocation Graph Process are circles, resources are rectangles A directed edge from Rj to Pi indicates resource Rj has been allocated to process Pi A directed edge from Pi to Rj indicates process Pi has requested an instance of resource Rj BYU CS 345 Concurrency Alex Milenkovich 16

Resource Allocation Graph Is there a cycle? If a graph contains no cycles, then no process in the system is deadlocked P1 P2 P3 If the graph contains a cycle, deadlock MAY exist R3 Is there deadlock? R4 BYU CS 345 Concurrency Alex Milenkovich 17

Deadlock? P2 Is there a cycle? Yes R1 P1 P3 Is there deadlock? Maybe Resource Allocation Graph Deadlock? P2 Is there a cycle? Yes R1 P1 P3 Is there deadlock? Maybe R2 P4 BYU CS 345 Concurrency Alex Milenkovich 18

Handling Deadlock Ensure the system will never deadlock deadlock prevention eliminate a condition avoidance algorithms Recover from deadlock detect deadlock and eliminate it Ignore deadlock system may hang so BYU CS 345 Concurrency Alex Milenkovich 19

Prevention by Elimination Handling Deadlock Prevention by Elimination Mutual Exclusion non-sharable resources We can’t eliminate this condition Hold and wait guarantee that when a process requests a resource, it does not hold any other resources two protocols system calls requesting resources precede all others a process can only request resources when it has none low resource utilization? BYU CS 345 Concurrency Alex Milenkovich 20

Eliminate No Preemption Handling Deadlock Eliminate No Preemption If a process holds resources and requests more that cannot be allocated, all its other resources are preempted If you can’t hold all, you can’t hold any process is restarted only when it can have all This works for resources whose state can be easily saved and restored later registers memory BYU CS 345 Concurrency Alex Milenkovich 21

Eliminate Circular Wait Handling Deadlock Eliminate Circular Wait Impose a total ordering of all resources Require that all processes request resources in increasing order. Whenever a process requests a resource, it must release all resources that are lower These two protocols eliminate circular wait BYU CS 345 Concurrency Alex Milenkovich 22

Deadlock Avoidance Allow general requests, but grant only when safe Assume we know the maximum requests (claims) for each process Process must state it needs Ie. max of 5 A objects, 3 B objects, 2 C objects. Do not need to use its max claims Ie. Ok to set max=5 and only use 3 Can make requests at any time and in any order Process Initiation Denial Track current allocations Assume all processes may make maximum requests at the same time Only start process if it can’t result in deadlock regardless of allocations BYU CS 345 Concurrency Alex Milenkovich 23

Resource Allocation Denial Avoidance Resource Allocation Denial Safe State – We can finish all processes by some scheduling sequence Example: Finish P1, P4, P2, P5, P3 Banker’s Algorithm (Dijkstra) Reject a request if it exceeds the processes’ declared maximum claims Grant a request if the new state would be safe Determining if a state is safe Find any process Pi for which we can meet it’s maximum requests Don't forget already allocated resources Mark Pi as “done”, add its resources to available resource pool State is safe if we can mark all processes as “done” Block a process if the resources are not currently available or the new state is not safe BYU CS 345 Concurrency Alex Milenkovich 24

Avoidance Example Claim 2 1 5 Allocation 2 1 5 Allocation 2 4 3 1 P4 1 P3 5 P2 P1 C B A Allocation 2 P4 1 P3 5 P2 P1 C B A Allocation 2 4 P4 3 1 P3 P2 P1 C B A C - A A B C P1 3 2 P2 6 1 P3 4 P4 Resource A B C 9 3 6 Available A B C 1 2 Are we in a safe state? Yes! BYU CS 345 Concurrency

Avoidance Quiz 6.4a Carpentry Company XYZ has 4 employees, 9 clamps, 2 drills, and 2 bottles of glue. Chair 4 clamps, 1 drill Desk 6 clamps, 1 drill, 1 glue Picture Frame 4 clamps, 1 drill, 1 glue Book Case 6 clamps, 1 drill, 1 glue BYU CS 345 Concurrency

Avoidance Quiz 6.4a Claim Allotted 1 4 P4 2 P3 P2 P1 3 C B A Needed Clamp Drill Glue P1 4 1 P2 6 P3 P4 Clamp Drill Glue P1 1 P2 2 P3 P4 Resource Clamp Drill Glue 9 2 Available Clamp Drill Glue 2 1 P1 needs a drill. Is it OK to give it to him (ie. would we still be in a safe state)? Yes! BYU CS 345 Concurrency

Avoidance Quiz 6.4b Claim Allotted 1 4 P4 2 P3 P2 P1 3 C B A Needed Clamp Drill Glue P1 4 1 P2 6 P3 P4 Clamp Drill Glue P1 1 P2 2 P3 P4 Resource Clamp Drill Glue 9 2 Available Clamp Drill Glue 2 1 P4 needs a glue. Would you give it to him (still safe state)? No! BYU CS 345 Concurrency

Quiz 6.4c If you give P4 the glue, are we then deadlocked? Claim Avoidance Quiz 6.4c Claim Allotted 1 4 P4 2 P3 P2 P1 3 C B A Needed Clamp Drill Glue P1 4 1 P2 6 P3 P4 Clamp Drill Glue P1 1 P2 2 P3 P4 Resource Clamp Drill Glue 9 2 Available Clamp Drill Glue 2 If you give P4 the glue, are we then deadlocked? No! BYU CS 345 Concurrency

Deadlock Detection Avoidance methods tend to limit access to resources Instead, grant arbitrary requests and watch for deadlock Can vary how often we check Early detection vs. overhead of checks Algorithm (Stallings, Figure 6.9) Preparation: Create table of process requests, current allocations Note available resources Mark processes with no resources Mark any process whose requests can be met (requests £ available resources) Include resources from that process as ‘available’ (this process can finish) If multiple processes available, pick any If any processes cannot be marked, they are part of a deadlock BYU CS 345 Concurrency Alex Milenkovich 30

Detection Example Mark P4 (not holding anything someone else wants) Request Allocation Resource A B C D E P1 1 P2 P3 P4 A B C D E P1 1 P2 P3 P4 A B C D E 2 1 Available A B C D E 1 1 E D C B A Temporary Available 1 Mark P4 (not holding anything someone else wants) Mark P3, new available: 0 0 0 1 1 Cannot mark P1 or P2, so we are deadlocked BYU CS 345 Concurrency

Quiz 6.4 Are we deadlocked? Requests Allocation Resource 2 1 1 2 3 7 2 Detection Quiz 6.4 Requests Allocation Resource A B C P1 P2 2 P3 P4 1 P5 A B C P1 1 P2 2 P3 3 P4 P5 A B C 7 2 6 Available A B C C B A Temporary Available Are we deadlocked? BYU CS 345 Concurrency

Deadlock Detection Questions? Does it really work? How often should you run a detection process? How often is deadlock likely to occur? How expensive is the detection process? BYU CS 345 Concurrency Alex Milenkovich 33

Deadlock Recovery Several possible approaches Abort all deadlocked processes Simple but common Back up processes to a previously saved checkpoint, then restart Assumes we have checkpoints and a rollback mechanism Runs risk of repeating deadlock Assumes that the deadlock has enough timing dependencies it won’t happen Selectively abort processes until deadlock broken Preempt resources until deadlock broken Must roll back process to checkpoint prior to acquiring key resource BYU CS 345 Concurrency Alex Milenkovich 34

Deadlock Recovery Process Termination Resource Preemption Kill them all One at a time Consider priority Time computing Who has most resources Resource Preemption Who gets preempted Do you consider process rollback and starvation BYU CS 345 Concurrency Alex Milenkovich 35

Recovery Mixed Strategy May group resources into classes, have a different deadlock strategy for each class Swap Space Prevent deadlocks by requiring all space to be allocated at once Avoidance also possible Tapes/Files Avoidance can be effective here Prevention by ordering resources also possible Main Memory Preemption a good approach Internal Resources (channels, etc.) Prevention by ordering resources Can use linear ordering between classes BYU CS 345 Concurrency Alex Milenkovich 36

BYU CS 345 Concurrency